Wednesday, 25 May 2016

Conditional Structures

Q: Senior Salesperson is paid Rs.400 a week, and a Junior Salesperson is paid Rs.275 a week. Write a program that accepts as input a salesperson's status in the character variable status. If status is 's' or 'S' , the senior person's salary should be displayed, if status is 'j' or 'J', the Junior salesperson's salary should be displayed, otherwise display error message.

Program:
      #include<iostream.h>
      #include<conio.h>
      void main()
      {
         char status;
         int senior=400, junior=500;
         clrscr();
         cout<<" Enter Status:";
         cin>>status;
         if(status='s' || status='S')
             cout<<"Senior Salesperson's salary is: "<<senior;
         else if(status='j' || status='J')
             cout<<"Junior Salesperon's salary is: "<<junior;
         else
             cout<<"Selected status is wrong, Try Again!";
          getche();
         }





Q: Write a program to get three numbers from the user for integer variables a,b and c. if a is not zero, find out whether it is the common divisor of b and c.

Program:
      #include<iostream.h>
      #include<conio.h>
      void main()
      {
        int a,b,c;
        clrscr();
        cout<<"Enter the value of a,b and c:";
        cin>>a>>b>>c;
        if(a==0)
          cout<<"Divisor can not be zero!";
        else
        {
             if(b%a==0 && c%a==0)
                   cout<<a<<" is a common divisor of " <<b<<" and "<<c<<endl;
             else
                   cout<<a<<" is not a common divisor of "<<b<<" and "<<c<<endl;
          }
          getche();
          }



Q: Write a program that contains an if statement that may be used to compute the area of a square ( area = side*side) or a triangle ( area=1/2*base*height) after prompting the user to type the first character of the figure name ( S or T)

Program:
      #include<iostream.h>
      #include<conio.h>
      void main()
      {
           char op;
           int side, base, height;
           float area;
           clrscr();
           cout<<"Enter choice (S for Square, T for Triangle) ";
           cin>>op;
           if(op=='S)
           {
                 cout<<"Enter Side:";
                 cin>>side;
                 area=side*side;
                 cout<<" Area of Square is: "<<area;
             }
             else if (op=='T')
             {
                 cout<<"Enter base:";
                 cin>>base;
                 cout<<"Enter height:";
                 cin>>height;
                 area=base*height*0.5;
                 cout<<"Area of Triangle is: "<<area;
             }
             getche();
             }



Q: Write a program that inputs a value and type of conversion. The program should then output the value after conversion. The program should include the following conversions.
1 inch= 2.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = 0.4536 kilograms

Make sure the program accepts only valid choices for type of conversion to perform

Program:
      #include<iostream.h>
     #include<conio.h>
     void main()
     {
          float value;
          char con;
          clrscr();
          cout<<"Enter conversion type:\n";
          cout<<"C for Centimeters \n";
          cout<<"L for Litres\n";
          cout<<"K for Kilometers \n";
          cout<<"G for Kilograms \n";
          cin>>con;
          cout<<"Enter a value:";
          cin>>value;
          switch(con)
          {
                  case 'C':
                  case 'c':
                        cout<<"Value: "<<value*2.54;
                        break;
                  case 'L':
                  case'l':
                        cout<<"Value:"<<value*3.785;
                        break;
                  case 'K':
                  case'k':
                        cout<<"Value: "<<value*1.609;
                        break;
                  case 'G':
                  case 'g':
                        cout<<"Value: "<<value*0.4536;
                        break;
                 default;
                        cout<<"Invalid conversion type!";
          }
          getche();
          }

1 comment: