Skip to content

Instantly share code, notes, and snippets.

@theoctober19th
Created July 13, 2019 07:16
Show Gist options
  • Save theoctober19th/537ddb66e1bcbe24f95f97f06dc55335 to your computer and use it in GitHub Desktop.
Save theoctober19th/537ddb66e1bcbe24f95f97f06dc55335 to your computer and use it in GitHub Desktop.
LAB 2 Solutions
#include <iostream>
using namespace std;
float Add(float a, float b, float c){
return a+b+c;
}
int main() {
float a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout << "The sum of " << a << ", " << b << " and " << c " is " << Add(a,b,c);
return 0;
}
#include <iostream>
using namespace std;
float AreaOfCircle(float radius, float PI = 3.14){
return PI*radius*radius;
}
int main() {
float rad, PI;
cout << "Enter the radius of a circle: ";
cin >> rad;
cout << "Using default value, area of circle is: " << AreaOfCircle(rad) << endl;
cout << "Enter the value of PI to be used: ";
cin >> PI;
cout << "Using your value of PI, area of circle is: " << AreaOfCircle(rad, PI) << endl;
return 0;
}
#include <iostream>
using namespace std;
void displayEvenNumbers(int n1, int n2 = 1000){
if(n2 < n1){
cout << "n1 should be smaller than n2";
}
for(int i=n1; i<n2; i++){
if(i %2==0){
cout << i << " ";
}
}
}
int main() {
int n1, n2;
cout << "Enter the limits: ";
cin >> n1 >> n2;
cout << "Even numbers from " << n1 << " to " << n2 << " are: " << endl;
displayEvenNumbers(n1, n2);
cout << endl;
cout << "Using default value of n2=1000: " << endl;
displayEvenNumbers(n1);
return 0;
}
#include <iostream>
using namespace std;
int Increase(int num, int n = 10){
return num + n;
}
int main() {
int num, n;
cout << "Enter the value of num: ";
cin >> num;
cout << "Using default value of n=10, Increase(num) = " << Increase(num) << endl;
cout << "Enter the value of n: ";
cin >> n;
cout << "Using the given value of n, Increase(num, n) = " << Increase(num, n) << endl;
return 0;
}
#include <iostream>
using namespace std;
void ReadData(float *a, float *b, float *c){
cout << "Enter three numbers: ";
cin >> *a >> *b >> *c;
}
float AddData(float a, float b, float c){
return a+b+c;
}
void DisplayData(float result){
cout << "The sum is: " << result << endl;
}
int main() {
float a, b, c;
ReadData(&a, &b, &c);
float sum = AddData(a, b, c);
DisplayData(sum);
return 0;
}
#include <iostream>
using namespace std;
long factorial(int x){
return x == 0 ? 1 : x*factorial(x-1);
}
long permutation(int n, int r){
return factorial(n) / factorial(n-r);
}
int main() {
int n, r;
cout << "Enter values of n and r: ";
cin >> n >> r;
long perm = permutation(n,r);
cout << "P(" << n << "," << r << ") = " << perm << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
float power(float m, int n){
return pow(m, n);
}
int main() {
float m;
int n;
cout << "Enter values of m and n: ";
cin >> m >> n;
float p = power(m,n);
cout << m << "^" << n << " = " << p << endl;
return 0;
}
#include <iostream>
#define ASCII_a 97
#define ASCII_z 122
#define ASCII_A 65
#define ASCII_Z 90
using namespace std;
void upperOrLower(char *c){
if(*c >= ASCII_a && *c <= ASCII_z){
*c -= 32;
}
else if(*c >= ASCII_A && *c <= ASCII_Z){
*c += 32;
}
}
int main() {
char c = '0';
cout << "Enter a character: ";
cin >> c;
upperOrLower(&c);
cout << "Result = " << c << endl;
return 0;
}
#include <iostream>
using namespace std;
void TempConversion(float *tempr){
*tempr = 1.8 * (*tempr) + 32;
}
int main() {
float tempC;
cout << "Enter temperature in Celcius: ";
cin >> tempC;
TempConversion(&tempC);
cout << "Temperature in Fahrenheit= " << tempC << endl;
return 0;
}
#include <iostream>
using namespace std;
//area of triangle
float area(float b, float h){
return 0.5*b*h;
}
//area of square
float area(float a){
return a*a;
}
int main() {
float a, b, h;
cout << "Enter base and height of triangle: ";
cin >> b >> h;
cout << "Area of triangle= " << area(b, h) << endl;
cout << "Enter side of the square: ";
cin >> a;
cout << "Area of square= " << area(a);
return 0;
}
#include <iostream>
using namespace std;
float Average(float a, float b){
cout << "Average() function with float parameters called." << endl;
return (a+b)/2.0;;
}
float Average(int a, int b){
cout << "Average() function with int parameters called." << endl;
return (a+b)/2.0;
}
int main() {
float a, b, avgWeight;
cout << "Enter the weights of two students: ";
cin >> a >> b;
avgWeight = Average(a, b);
cout << "Average weight = " << avgWeight << endl;
int x, y;
float avgAge;
cout << "Enter the ages of two students: ";
cin >> x >> y;
avgAge = Average(x, y);
cout << "Average age = " << avgAge << endl;
return 0;
}
#include <iostream>
using namespace std;
inline float areaOfCircle(float radius){
return 3.14*radius*radius;
}
int main() {
float rad;
cout << "Enter the radius of a circle: ";
cin >> rad;
cout << "Area of circle is: " << areaOfCircle(rad) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment