2.1: Write a program to display the following output using a single cout statement
Maths = 90
Physics = 77
Chemistry = 69
Solution:
Physics = 77
Chemistry = 69
2.3:Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.
Solution:
WELL DONE
WELL DONE
WELL DONE
WELL DONE
WELL DONE
2.5: Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius
Solution:
Temperature in Celsius = 40.555557
2.6:Redo Exercise 2.5 using a class called temp and member functions.
Solution:
Temperature in Celsius = 44.444443
Maths = 90
Physics = 77
Chemistry = 69
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
| #include#includeint main(){ char *sub[]={"Maths","Physics","Chemestry"}; int mark[]={90,77,69}; for(int i=0;i<3 code="" i=""> {cout< } return 0;} |
output
Maths = 90Physics = 77
Chemistry = 69
2.2: Write a program to read two numbers from the keyboard and display the larger value on the screen.
Solution:
larger value = 20
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #include#includeint main(){ float a,b; cout<<" Enter two values :"< cin>>a>>b; if(a>b) cout<<" larger value = "< else cout<<" larger value = "< return 0;} |
output
Enter two values : 10 20larger value = 20
2.3:Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Solution:#include#includeint main(){ int n; char *str; str="WELL DONE"; cout<<" Enter an integer value "; cin>>n; for(int i=0;i { cout< } return 0;} |
output
Enter an integer value 5WELL DONE
WELL DONE
WELL DONE
WELL DONE
WELL DONE
2.4: Write a program to read the values a, b and c and display x, where
x = a / b –c.
Test the program for the following values:
(a) a = 250, b = 85, c = 25
(b) a = 300, b = 70, c = 70
Solution:
x=a/(b-c) = 4.166667
x= infinity
x = a / b –c.
Test the program for the following values:
(a) a = 250, b = 85, c = 25
(b) a = 300, b = 70, c = 70
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #include#includeint main(){ float a,b,c,x; cout<<" Enter the value of a,b, &c :"< cin>>a>>b>>c; if((b-c)!=0) { x=a/(b-c); cout<<" x=a/(b-c) = "< } else { cout<<" x= infinity "< } return 0;} |
During First Run:
output
Enter the value of a,b, &c : 250 85 25x=a/(b-c) = 4.166667
During Second Run:
output
Enter the value of a,b, &c : 300 70 70x= infinity
2.5: Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
| #include#includeint main(){ float f,theta; cout<<" Enter the temperature in Feranhite scale : "; cin>>f; theta=((f-32)/9)*5; cout<<" Temperature in Celsius = "< return 0;} |
output
Enter the temperature in Feranhite scale : 105Temperature in Celsius = 40.555557
2.6:Redo Exercise 2.5 using a class called temp and member functions.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #include#includeclass temp{ float f,theta;public: float conversion(float f);};float temp::conversion(float f){ theta=((f-32)/9)*5; return theta;}int main(){ temp t; float f; cout<<" Enter temperature in Farenheite scale :"< cin>>f; cout<<" Temperature in Celsius scale = "< return 0;} |
output
Enter the temperature in Feranhite scale : 112Temperature in Celsius = 44.444443
No comments:
Post a Comment