New Trending Topics and Study Material

Breaking

Tag

Tricks & Tips (17) News (5) PHP (5) Android (3) SEO (1) free paytm cash (1)

C++

2.1: Write a program to display the following output using a single cout statement

Maths = 90
Physics = 77
Chemistry = 69
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include
#include
int main()
{
           char *sub[]={"Maths","Physics","Chemestry"};
           int mark[]={90,77,69};
           for(int i=0;i<3 code="" i="">
            {
cout<"="<
             }
    return 0;
}

output

Maths = 90
Physics = 77
Chemistry = 69


2.2: Write a program to read two numbers from the keyboard and display the larger value on the screen.

Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include
#include
int 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 20
larger 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
#include
int 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 5
WELL 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: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include
#include
int 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 25
x=a/(b-c) = 4.166667

During Second Run:

output

Enter the value of a,b, &c : 300 70 70
x= 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
#include
int 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 : 105
Temperature 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
#include
class 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 : 112
Temperature in Celsius = 44.444443









No comments:

Post a Comment