Sunday, 27 February 2011

EXERCISE FOP IN BLOG

EXERCISE  IN BLOG.


QUESTION.


1. Write correct C++ syntax & for each statement below:

a) Initializing variable Pi with the value 3.14 (constant)
b) Declare a variable named Parameter with double data type (declare variable)
c) Give instruction that allowed user to input data (input)
d) Input number of computer using variable (input assign to variable value)

2. Solve the question below. Show the working.

a) 5 * 2 % 3 + 25 / 5
b) a = 5, b = 6
!((a < 3) && (a == 3) || (b > 9))

3. Identify syntax errors in the following program.

include<iostream.h>
main()
{
float allowance = 300.00, salary;

cout<<"Input Salary = ";
cin>>salary;

TSalary = salary + Allowance;

cout<<"Salary is= "<< salary;
}

4. Write a program that will calculate the monthly salary for an employee that where Saturday and Sunday are considered as non-working days.

5. Write a program that calculates the average of 5 numbers that can be input by user.

6. Write a program that will calculate the area of rectangular, triangle and circle.



ANSWER.


1. a) Initializing variable Pi with the value 3.14 (constant).


   //declare
   float Pi=3.14


   b)Declare a variable named Parameter with double data type (declare variable)


   //declare Parameter
   double Parameter=5.776198


    c)Give instruction that allowed user to input data (input)


    //input
     cout<<"Enter data=" ;
     cin>>data;


   d)Input number of computer using variable (input assign to variable value)


      //declare nc=number of computer
      int nc;

    //Input
    cout<<”Enter number of computer=” ;
    cin>>nc;


2. a) 5 * 2 % 3 + 25 / 5

    = [10%3]+25/5
    = 1+5
    = 6

   b)  a = 5, b = 6
       ! ((a < 3) && (a == 3) || (b > 9))

    = ! [(0) && (0) || (0)]
    =1


3Identify syntax errors in the following program.








4. Write a program that will calculate the monthly salary for an employee that where                              Saturday and Sunday are considered as non-working days.






5.Write a program that calculates the average of 5 numbers that can be input by user.



6.Write a program that will calculate the area of rectangular, triangle and circle.


  
#include <iostream.h>
main()
{
      //declare
      float b,h,r,w,area,;
      float pi=3.14;
      int selection;
      //out
      cout<<"Please choose you choice"<<endl;
      cout<<"1.Rectangular";
      cout<<"\n2.Triangle";
      cout<<"\n3.Circle"<<endl;
      cin>>selection;
      switch (selection)
      {
             case 1: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the width"<<endl;
                     cin>>w;
                     //process
                     area = w*h;
                     cout<<"The area is:"<<area;
                     break;
             case 2: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the base"<<endl;
                     cin>>b;
                     //process
                     area = 0.5*b*h;
                     cout<<"The area is:"<<area;
                     break;
             case 3: cout<<"Please input the radius"<<endl;
                     cin>>r;
                     //process
                     area = pi*r*r;
                     cout<<"The area is:"<<area;
                     break;
             default :cout<<"Invalid Selection";
             return 0;
             }
}

No comments:

Post a Comment