Monday, February 28, 2011

Exercises (23/02/2011)

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 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. Write correct C++ syntax & for each statement below :

a) const pi = 3.14

b) double parameter;

c) cout>>"Enter your data: ";
cin<< data;

d) int num;
num = 3;
cout>>"Number= "<< num;


2. Solve the question below. Show the working.

a) 5 * 2 % 3 + 25 / 5
10 % 3 + 25 / 5
1 + 25 / 5
1 + 5
= 6

b) a=5 , b=6
!((a<3) && (a==3) || (b>a))
!((5<3) && (5==3) || (6>5))
!((0) && (0) || (0))
!(0) || (0)
!(0)
= 1 or T (True)


3. Identify syntax errors in the following program.

i) #include < iostream.h >

ii) float TSalary;

iii) cout<<"input_salary=";

iv) return 0;

v) }


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


#include < iostream.h >
main()
{

//declare
float allowance=500.00, salary=40, tsalary, day;

cout<<"Muhammad FIrdaus Bin Hasmi" << endl;
cout<<"05-201006-00003" << endl;

//input
cout<<"\nease input total days work = ";
cin>>day;

//process
tsalary = (salary*day)+allowance;

//output
cout<<"The total salary for this month is = RM " << tsalary << endl << endl;

return 0;
}





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


#include < iostream.h >
main()
{

//declaire variable
float num1, num2, num3, num4, num5, average;

cout<<"Muhammad Firdaus Bin Hasmi";
cout<<"05-201006-00003";

//input 1
cout<<"Enter Number 1 = ";
cin>>num1;

//input 2
cout<<"Enter Number 2 = ";
cin>>num2;

//input 3
cout<<"Enter number 3 = ";
cin>>num3;

//input 4
cout<<"Enter Number 4 = ";
cin>>num4;

//input 5
cout<<"Enter number 5 = ";
cin>>num5;

//formula
average=(num1+num2+num3+num4+num5);

//output
cout<<"The answer is = " << average <
return 0;
}




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


#include < iostream.h >
main()
{
//declaire
float b, h, r, w, area;
float pi=3.14;
int selection;

//out
cout<<"Muhammad FIrdaus Bin Hasmi" << endl;
cout<<"05-201006-00003" << endl;

cout<<"\nPlease 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 = ";
cin>>h;
cout<<"Please input the width = ";
cin>>w;
//process
area = w*h;
cout<<"The area is = " << area << endl < break;

case 2: cout<<"Please input the height = ";
cin>>h;
cout<<"Please input the base = ";
cin>>b;
//process
area = 0.5*b*h;
cout<<"The area is:" << area << endl << endl;
break;

case 3: cout<<"Please input the radius = " << endl;
cin>>r;
//process
area = pi*r*r;
cout<<"The area is:" << area << endl << endl;
break;

default :cout<<"Invalid Selection";
}


return 0;
}

No comments:

Post a Comment