Fundamentals of Computer Programming C++ Programs

 


Q1. Add the code on the doted lines in the following program to generate the output given after the source code.

Sol:

#include<iostream>

using namespace std;

int main()

{

          int x=100;

          float y=6.0;

          float sum;

          float product;

          float m;

          sum=x + y;

          cout << "The Sum of x and y is " <<sum<< endl;

          product=x*y;

          cout<< "The product of x and y is " <<product<<endl;

          m=x+y+100;

          cout<< "x+y+100="<<m<<endl;

          return 0;

}

 

 

Q2. Find the output of the following on your notebook then verify it through a C++ Program.

Sol:

#include<iostream>

using namespace std;

int main()

{

          int x,y,z,a,b,c,d,e,f,g;

          x=7+4*2%6*4;

          cout<<"1."<<x<<endl;

          y=(7+4)*6%5*4;

          cout<<"2."<<y<<endl;

          z=7+((4*6)%5)*4;

          cout<<"3."<<z<<endl;

          a=4+6*2;

          cout<<"4."<<a<<endl;

          b=3+9/3*6;

          cout<<"5."<<b<<endl;

          c=6+(10-5)*2+10;

          cout<<"6."<<c<<endl;

          d=-(20/6)+(12-10)*5/2;

          cout<<"7."<<d<<endl;

          e=(20-10) / (6+2) / (3/2);

          cout<<"8."<<e<<endl;

          f=-(20/6.7)+(12-10.0)*5.0/2;

          cout<<"9."<<f<<endl;

          g=(20-10.0) - (6.6+2.2) / (3/2);

          cout<<"10."<<g<<endl;

          return 0;

}

 

 

Q.3 assume that int x=3, y=2, z=1; are the values for all of the following expressions, then calculate the value of x after each statement

Sol:

#include<iostream>

using namespace std;

int main()

{

          int x=3,y=2, z=1;

          x+=y;

          cout<<"1."<<x<<endl;

          x-=y;

          cout<<"2."<<x<<endl;

          x+=y+z;

          cout<<"3."<<x<<endl;

          x/=y*z;

          cout<<"4."<<x<<endl;

          x*=y+z;

          cout<<"5."<<x<<endl;

          x*=y-6+z-2;

          cout<<"6."<<x<<endl;

          x%=4-y;

          cout<<"7."<<x<<endl;

          x+=3*5;

          cout<<"8."<<x<<endl;

          return 0;   

}

 

Q4. Assume int x=0, y=4, z=10 for all the statements

Calculate the value of the variables used in each statement.

Sol:

#include<iostream>

using namespace std;

int main()

{

          int x=0, y=4, z=10;

          x=y++;       

          cout<<"1."<<x<<endl;

          x=++y;

          cout<<"2."<<x<<endl;

          x=y--;

          cout<<"3."<<x<<endl;

          x= --y;

          cout<<"4."<<x<<endl;

          x=6-y++;

          cout<<"5."<<x<<endl;

          x=6- ++y;

          cout<<"6."<<x<<endl;

          x=6+y--;

          cout<<"7."<<x<<endl;

          x=6+ --y;

          cout<<"8."<<x<<endl;

          x= y++ + ++z;

          cout<<"9."<<x<<endl;

          x=y++ - --z;

          cout<<"10."<<x<<endl;

          x= --y + z--;

          cout<<"11."<<x<<endl;

          return 0;

}

Q5. Write a program that finds the sum of the individual digits in a three digits number.

Sol:

#include <iostream>

using namespace std;

int main()

{

    int num1, num2, r, sum=0;

    cout << " Input a number: ";

    cin >> num1;

        num2 = num1;

    while (num1 > 0)

    {

       r = num1 % 10;

        num1 = num1 / 10;

        sum = sum + r;

    }

    cout << " The sum of digits of " << num2 << " is: " << sum << endl;

    return 0;

}

Q6. Write a program that converts the number of minutes to hours and minutes.

Sol:

#include <iostream>

using namespace std;

int main()

{

          int  h, m, s;

          cout<<"Enter numbers of minutes:";

          cin>>m;

          h=m/60;

          cout<<"Hour "<<h<<endl;

          s=m%60;

          cout<<"and "<<s<<" minutes"<<endl;

          return 0;

}

Q7. Write a program that inputs a character and displays whether it is vowel or consonant using switch statement.

Sol:

#include <iostream>

using namespace std;

int main()

{

    char ch;

    cout<<"Enter any Alpabet\n";

    cin>>ch;

          switch(ch)

          {

    case 'a':

    cout<<ch<<" is a vowel";

    break;

 

    case 'e':

    cout<<ch<<" is a vowel";

    break;

 

    case 'i':

    cout<<ch<<" is a vowel";

    break;

 

    case 'o':

    cout<<ch<<" is a vowel";

    break;

 

    case 'u':

    cout<<ch<<" is a vowel";

    break;

    //for upper case//

 

    case 'A':

    cout<<ch<<" is a vowel";

    break;

 

    case 'E':

    cout<<ch<<" is a vowel";

    break;

 

    case 'I':

    cout<<ch<<" is a vowel";

    break;

 

    case 'O':

    cout<<ch<<" is a vowel";

    break;

 

    case 'U':

    cout<<ch<<" is a vowel";

    break;

 

    default:

    cout<<ch<<" is a consonant";

    break;

    }

          return 0;

    }

Q8. Write a program that inputs radius. It calculates area of circle if user enters1 as choice. It calculates circumference if the user enters 2 as choice. It displays error message in case of any other choice.

Sol:

#include<iostream>

using namespace std;

#define pi 3.1416

int main()

{

          float r,aoc,coc;

          cout<<"Enter the radius:"<<endl;

          cin>>r;

          int choice;

          cout<<"Enter the choice \n1 for aoc\n2 for coc"<<endl;

          cin>>choice;

          if(choice==1)

          {

                   aoc=3.1416*r*r;

                   cout<<"area of circle"<<aoc<<endl;

          }

          else if(choice==2)

          {

                   coc=2*3.1416*r;

                   cout<<"area of circumference"<<coc<<endl;

                  

          }

          else

          cout<<"something went wrong"<<endl;

          return 0;

}

Comments

Popular posts from this blog

Basics of Object Oriented Programming(OOP) Programs