[go: up one dir, main page]

0% found this document useful (0 votes)
3 views7 pages

Computing Lab 2

The document contains a series of C++ programming exercises from a lab, each demonstrating different programming concepts such as variable exchange, user input, mathematical calculations, and conversions. Exercises include swapping values, displaying user information, calculating circle circumference, temperature conversion, distance between points, solving quadratic equations, performing arithmetic operations, checking parity, calculating averages, and converting days into years and weeks. Each exercise is presented with code snippets and comments explaining the purpose of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Computing Lab 2

The document contains a series of C++ programming exercises from a lab, each demonstrating different programming concepts such as variable exchange, user input, mathematical calculations, and conversions. Exercises include swapping values, displaying user information, calculating circle circumference, temperature conversion, distance between points, solving quadratic equations, performing arithmetic operations, checking parity, calculating averages, and converting days into years and weeks. Each exercise is presented with code snippets and comments explaining the purpose of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 2

Exc 4
//Exc 4: exchange value
#include <iostream>

using namespace std;

int main() {
int A,B;
cout<< "Enter A,B\n";
cin>>A>>B;
cout<<"A before: "<<A<<"\n";
cout<<"B before: "<<B<<"\n";

A = A+B;
B = A-B;
A = A-B;
cout<< "A after: "<<A<<"\n";
cout<<"B after: "<<B<<"\n";
return 0;
}

Exc 5
//Exc 5: display info
#include <iostream>
#include <string>

using namespace std;

int main() {

Lab 2 1
string name, sex;
float height, weight;
int age;

cout<< "Pleasse fill in your name, age, sex (male/female), hei


cin>> name>>age>>sex>>height>>weight;
cout<<"Hello "<<name<<"\n";
cout<<"You are "<<age<<" year(s) old\n";
cout<<"You are "<<sex<<"\n";
cout<<"You are "<<height<<"m in height and "<<weight<<" kg in

return 0;
}

Exc 6
//Exc 6: Circle Circumference
#include <iostream>
#include <string>

using namespace std;

int main() {
const float PI = 3.1416;
float r, C;
cout<<"Enter radius: ";
cin >> r;
C = 2*PI*r;
cout<<"\nCircle circumference is: "<<C;
return 0;
}

Exc 7

Lab 2 2
//Exc 7: Temperature conversion
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
float f,c;
cout<<"Enter Fareheit temperature: ";
cin>>f;
c = (5.0/9)*(f-32);
cout<<"the equivalent Celcius temperature is: "<<fixed<<setpre
return 0;
}

Exc 8
//Exc 8: Distance
#include <iostream>
#include <cmath>

using namespace std;

int main() {
float x_A, y_A, x_B, y_B, dis;
cout<<"Enter coordinates (x and y) for A: ";
cin>>x_A>> y_A;
cout<<"Enter coordinates (x and y) for B: ";
cin>>x_B>>y_B;
dis = sqrt(pow(x_A-x_B,2)+pow(y_A-y_B,2));
cout<< "Distance between A and B: "<<dis;

Lab 2 3
return 0;
}

Exc 9
//Exc 9: Quadratic equation
#include <iostream>
#include <cmath>

using namespace std;

int main() {
float a,b,c, del;
cout<<"Enter a,b,c: ";
cin>>a>>b>>c;

del = b*b-4*a*c;

float x1, x2;


x1 = (-b-sqrt(del))/(2*a);
x2 = (-b+sqrt(del))/(2*a);

cout<<"Fuction has roots x1 = "<<x1<<" and x2 = "<<x2;

return 0;
}

Exc 10
//Exc 10: Arithmetic Operation
#include <iostream>
#include <iomanip>

using namespace std;

Lab 2 4
int main() {
int a, b;
cout<<"Enter a,b: ";
cin>>a>>b;

//addition
int sum = a+b;
cout<<"a + b = "<<sum<<"\n";
//subtraction
int sub = a-b;
cout<<"a - b = "<<sub<<"\n";

//multiplication
int prod = a*b;
cout<<"a * b = "<<prod<<"\n";
//division
float div = float(a)/b;
cout<<"a / b = "<<fixed<<setprecision(2)<<div<<"\n";

return 0;
}

Exc 11
//Exc 11: Parity
#include <iostream>
#include <cmath>

using namespace std;

int main() {
int n;
cout<<"enter your integer: ";
cin>>n;

Lab 2 5
cout<<"number n is "<<((n%2)? "odd": "even");
return 0;
}

Exc 12
//Exc 12: Average of 5
#include <iostream>
#include <cmath>

using namespace std;

int main() {
float a =0.5;
int b;

cout<<"enter 5 intergers: ";


while (a != floor(a)){
cin>>b;
a +=b - 0.1;
}
cout<<"Average: "<<a/5;
return 0;
}

Exc 13
//Exc 13: Days Conversion
#include <iostream>
#include <cmath>

using namespace std;

Lab 2 6
int main() {
const int y_d = 365;
const int w_d = 7;

int days, years, weeks;


cout<<"Enter number of days: ";
cin>>days;

years = days / y_d;


days %= y_d;
weeks = days / w_d;
days %= w_d;
cout<<"It is equivalent to "<<years<<" year(s) + "<<weeks<<" w
return 0;
}

Lab 2 7

You might also like