Exercise 3
1. State whether the below variables declaration is valid or not? If it’s invalid give a reason.
Define/declare variables Yes/No Reason
i. char _ic_number; Yes
ii. int RMX; No Invalid data type
iii. bool test; Yes
iv. float 1number; No Invalid variable name because start will
number
v. unsigned int myaccount; Yes
vi. long saving = 1000; Yes
vii. char mycharacter = ‘?’; Yes
2. State which data type is suitable.
Define/declare variables Data Type
i. 500 int (other integer data type also acceptable)
ii. 78000 long (unsigned int or unsigned long also
acceptable)
iii. 78.86 float (other floating point data type also
acceptable)
iv. A char
v. & char
vi. false bool
vii. 1000000000000 float (other floating point data type also
acceptable)
3. Write a C++ statement to accomplish each of the following task.
a. Declare/define variable(s) named as test and exam to be type of float.
float test;
float exam;
or
float test, exam;
b. Write C++ statement(s) to print value in variable test and exam.
cin>>test;
cin>>exam;
or
cin>>test>>exam;
c. Assign C to collect, where collect is a variable of char data type.
collect = ‘C’;
4. For the following C++ program what is the output ?
#include <iostream.h>
void main()
{
float average;
int reminder, score1, score2, score3,score4;
score1=11;
score2=12;
score3=13;
score4=10;
score2=15;
cout<<”score1 = “<<score1<<endl;
cout<<”score2 = “<<score2<<endl;
cout<<”score3 = “<<score3<<endl;
cout<<”score4 = “<<score4<<endl;
}
Output
score1 = 11
score2 = 15
score3 = 13
score4 = 10
5. Write a complete C++ program based on the given instruction.
a. Declare/define variable(s) named as num1 and num2 to be type of int.
b. Assign 10 to num1.
c. Assign 15 to num2.
d. Write C++ statement(s) to print value in variable num1 and num2.
Run this program in jGrasp and print the output
#include <iostream.h>
void main()
{
int num1, num2;
num1 = 10;
num2 = 15;
cout<<"num1 = "<<num1<<endl;
cout<<"num2 = "<<num2;
}
Output
6. Write a program to calculate and display an average of four numbers.
#include <iostream.h>
void main()
{
float num1, num2,num3,num4,average;
cout<<"Enter 4 numbers : ";
cin>>num1>>num2>>num3>>num4;
average = (num1+num2+num3+num4)/4;
cout<<"average = "<<average;
}