PLUS ONE
COMPUTER APPLICATION / COMPUTER SCIENCE
First Year Computer Application(Commerce) Previous Questions Chapter wise ..
Chapter 7. Control Statements
1. Rewrite the following C++ statement using if … else cout <<( n % 2 == 0?”EVEN” : “ODD”);
Ans: if(n%2 == 0)
cout<<”EVEN”;
else
cout<<”ODD”;
2. Rewrite the following C++ code using if . . . else statement.
large = (n1 > n2) ? n1 : n2 ;
Ans. if(n1>n2)
cout<<n1;
else
cout<<n2;
3. Explain elements of looping statements with suitable examples.
for loop:
Syntax:
for (initialization ; test expression; update statement)
{
body of the loop;
}
eg.
for ( n=1; n< = 10; ++n)
cout << n
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the for loop. After execution of loop body update expression is executed ++n. it will repeated until test expression become false.
while loop:
Syntax
Initialization of loop control variable
while ( test expression)
{
body of the loop;
updating of loop control variable;
}
eg.
n=1;
while( n< = 10)
{
cout << n ;
++n;
}
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
do--while loop:
Syntax
Initialization of loop control variable;
do
{
body of the loop;
updating of loop control variable;
}
( test expression);
eg.
n=1;
do
{
cout << n ;
++n;
}
while(n<= 10);
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
4. Write a C++ program to find the sum of squares of first 10 odd numbers.
5. Write a C++ program to check whether a given year is a leap year or not.
#include<iostream>
6. Write a C++ program print first 10 even natural numbers.
7. Write output of the following C++ program
# include <iostream>
using namespace std;
int main ()
{
Int a, b, c ;
a = b = 1 ;
c = 2;
if (a + b > c))
cout<<”\n RED”;
else if(a + b < c)
cout<<”\n GREEN”;
else
cout<<”\n BLUE”;
return 0;
}
Output:
BLUE
8. Explain the various iteration statements in C++ with syntax and example
Iteration statements are used to perform repeated execution of a set of one or more statements in a program. They are also known as looping statements.
A looping statement has four parts
a). initialization of control variable
b). test expression
c). update statement for control variable
d). body of the loop
eg. for loop, while loop, do while loop
9.Identify the name of the following operators in C++.
&&,||,!
&& ---->AND Operator
|| ----------> OR Operator
! ----------> NOT Operator
10. do ..... while loop is a ................ controlled loop.
Ans. exit controlled loop
11. The following program finds the sum of three numbers.Modify the program to find the average.(Average should display fractional part also).
#include<iostream>
using namespace std;
int main( )
int x,y,z,result;
cout<<”Enter values for x,y,z;
cin>>x>>y>>z;
result=x+y+z;
cout<<The answer is =”<<result;
return 0;
}
Ans.
#include<iostream>
using namespace std;
int main( )
int x,y,z;
float avg;
cout<<”Enter values for x,y,z;
cin>>x>>y>>z;
avg=(x+y+z)/3;
cout<<The answer is =”<<avg;
return 0;
}
12. The following code segment prints first 10 natural numbers.
int n=1;
while (n<=10)
{
cout<<n<<” “;
++n;
}
a)Modify the program to print first 100 natural numbers.
ans. int n=1;
while(n<=100)
{
cout<<n<<" "
++n;
}
b)Rewrite the above code using for loop.
Ans.
for(int n=1;n<=10:++n)
{
cout<<n<<" ";
}
13. List the four important elements of a loop.
Loop statements usually have four components: initialization (usually of a loop control variable), continuation test on whether to do another iteration, an update step, and a loop body.
14. Write C++ program to input a digit and print it in words.
(Hint : if digit = 1 then print “ ONE “)
#include<iostream>
15. . Rewrite the following C++ statement using if .. … …. Else statement
cout<<result = mark > 30 ? “Passed” : “Failed”;
Ans.
if(mark>30)
{
cout<<result="Passed";
else
cout<<result="Failed";
}
16. . a) Correct the errors in the following program code to display numbers from 1 to 10.
for (i=1; i>-10; i++)
cout>>i;
Ans.
for(int 1=1;i<=10;++i)
{
cout<<i;
b) Explain the different types of programming errors with the help of the above code
17. Write the syntax of switch statement. Explain using an example of switch its working
Switch statement is a multiple selection statement.
Syntax
switch
(expression)
{
case constant 1 : statement block 1;
Break;
case constant 1 : statement block 1;
Break;
case constant 1 : statement block 1;
Break;
… .. … .. ..
default : statement block n;
}
Unlike in else if ladder here the case value should be a constant value (not a range of values). If any case value is matched corresponding statement block will be executed and then the break statement to exit from the switch statement. If no match is found , then the default block get executed.
Eg.
switch (day)
{
case 1: cout << “Sunday”;
break;
case 2: cout <<”Monday”;
break;
.. .. ……
default: cout<<”Wrong input”;
}
18. Compare the working of “do….. while” loop and “while” loop using an example.
do--while loop:
Syntax
Initialization of loop control variable;
do
{
body of the loop;
updating of loop control variable;
}
( test expression);
eg.
n=1;
do
{
cout << n ;
++n;
}
while(n<= 10);
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
while loop:
Syntax
Initialization of loop control variable
while ( test expression)
{
body of the loop;
updating of loop control variable;
}
eg.
n=1;
while( n< = 10)
{
cout << n ;
++n;
}
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
19. There are three looping statements in C++.
a) Which is the exit-controlled loop?
Ans. do--while loop
b) How does it differ from an entry controlled loop?
If the evaluation of test expression takes place only at the end of looping statements , such loops are known as exit control loop. Here minimum one iteration takes place.
20. Rewrite the following switch statement using if – else if statement :
switch (n)
{
case 4 : cout<<"Excellent";
break;
case 3 : cout<<"Good";
break;
case 2 : cout<<"Average";
break:
case 1 : cout<<"Poor";
break;
default : cout<<"Invalid";
}
Ans.
if(n==4)
{
cout<<"Excellent";
else if(n==3)
cout<<"Good";
else if(n==2)
cout<<"Average";
else if(n==1)
cout<<"Poor";
else
cout<<"Invalid";
}
21. State whether the following statements are true or false. If false give reason.
a). Break statement is essential in Switch.-----------True
b). For loop is an entry controlled loop. ---------- True
c). Do .. .. while loop is an entry controlled loop.--------- False
do---while loop is a exit contolled loop.
d). Switch is a selection statement.----------- True
22. Write a program to find the biggest from 3 given numbers
23. Rewrite the following code using if … .. else ladder.
#include<iostream>
using namespace std;
int main( )
{
int colour;
cout <<” Enter number between 1 and 4 : “;
cin>> colour;
switch(colour)
{
case 1 :
cout<< “Red”;
break;
case 2 :
cout<<”Green”;
break;
case 3 :
cout<<”Blue”;
break;
default :
cout<<”Wrong input”;
}
}
Ans.
#include<iostream>
using namespace std;
int main( )
{
int colour;
cout <<” Enter number between 1 and 4 : “;
cin>> colour;
if(colour==1)
{
cout<< “Red”;
else if(colour==2)
cout<< "Green";
else if(colour=="Blue";
cout<<"Blue";
else
cout<<”Wrong input”;
}
}
24. Rewrite the following code using switch case statement.
if (Lan=='M')
cout<< " I prefer Malayalam”;
else if (Lan = 'E’)
cout "I prefer English";
else
cout<<” I prefer neither Malayalam nor English” ;
Ans
switch(Lan)
{
case M:
cout<<" I prefer Malayalam”;
break;
case E:
cout <<"I prefer English";
break;
default:
cout<<” I prefer neither Malayalam nor English” ;
}
25. Write a C++ program to find the simple interest of an amount (P) deposited with a rate of interest (R) for a period of Years (N).
Rate of interest = 7% If deposit amount P is less than 1 lakh.
Rate of interest = 8% if. Deposit amount P is between 1 lakh and 5 lakhs.
Rate of interest = 9o/o if deposit amount P is above 5 lakhs,
(Hint : Simple interest = P x N x R/100)
1. Rewrite the following C++ statement using if … else cout <<( n % 2 == 0?”EVEN” : “ODD”);
Ans: if(n%2 == 0)
cout<<”EVEN”;
else
cout<<”ODD”;
2. Rewrite the following C++ code using if . . . else statement.
large = (n1 > n2) ? n1 : n2 ;
Ans. if(n1>n2)
cout<<n1;
else
cout<<n2;
3. Explain elements of looping statements with suitable examples.
for loop:
Syntax:
for (initialization ; test expression; update statement)
{
body of the loop;
}
eg.
for ( n=1; n< = 10; ++n)
cout << n
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the for loop. After execution of loop body update expression is executed ++n. it will repeated until test expression become false.
while loop:
Syntax
Initialization of loop control variable
while ( test expression)
{
body of the loop;
updating of loop control variable;
}
eg.
n=1;
while( n< = 10)
{
cout << n ;
++n;
}
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
do--while loop:
Syntax
Initialization of loop control variable;
do
{
body of the loop;
updating of loop control variable;
}
( test expression);
eg.
n=1;
do
{
cout << n ;
++n;
}
while(n<= 10);
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
4. Write a C++ program to find the sum of squares of first 10 odd numbers.
5. Write a C++ program to check whether a given year is a leap year or not.
#include<iostream>
using namespace std; int main() { int year = 2016; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) cout<<year<<" is a leap year"; else cout<<year<<" is not a leap year"; return 0; }
Output
2016 is a leap year
6. Write a C++ program print first 10 even natural numbers.
- #include<iostream>
- using namespace std;
- int main()
- {
- for(int i = 0; i < 10; i++){
- cout<< 2 * i + 1 << ' ';
- }
- return 0;
- }
7. Write output of the following C++ program
# include <iostream>
using namespace std;
int main ()
{
Int a, b, c ;
a = b = 1 ;
c = 2;
if (a + b > c))
cout<<”\n RED”;
else if(a + b < c)
cout<<”\n GREEN”;
else
cout<<”\n BLUE”;
return 0;
}
Output:
BLUE
8. Explain the various iteration statements in C++ with syntax and example
Iteration statements are used to perform repeated execution of a set of one or more statements in a program. They are also known as looping statements.
A looping statement has four parts
a). initialization of control variable
b). test expression
c). update statement for control variable
d). body of the loop
eg. for loop, while loop, do while loop
9.Identify the name of the following operators in C++.
&&,||,!
&& ---->AND Operator
|| ----------> OR Operator
! ----------> NOT Operator
10. do ..... while loop is a ................ controlled loop.
Ans. exit controlled loop
11. The following program finds the sum of three numbers.Modify the program to find the average.(Average should display fractional part also).
#include<iostream>
using namespace std;
int main( )
int x,y,z,result;
cout<<”Enter values for x,y,z;
cin>>x>>y>>z;
result=x+y+z;
cout<<The answer is =”<<result;
return 0;
}
Ans.
#include<iostream>
using namespace std;
int main( )
int x,y,z;
float avg;
cout<<”Enter values for x,y,z;
cin>>x>>y>>z;
avg=(x+y+z)/3;
cout<<The answer is =”<<avg;
return 0;
}
12. The following code segment prints first 10 natural numbers.
int n=1;
while (n<=10)
{
cout<<n<<” “;
++n;
}
a)Modify the program to print first 100 natural numbers.
ans. int n=1;
while(n<=100)
{
cout<<n<<" "
++n;
}
b)Rewrite the above code using for loop.
Ans.
for(int n=1;n<=10:++n)
{
cout<<n<<" ";
}
13. List the four important elements of a loop.
Loop statements usually have four components: initialization (usually of a loop control variable), continuation test on whether to do another iteration, an update step, and a loop body.
14. Write C++ program to input a digit and print it in words.
(Hint : if digit = 1 then print “ ONE “)
#include<iostream>
#include<cmath> using namespace std; int reverse(int v,int lim) /*Method to reverse the number*/ { if(lim==1) return v; else return (((v%10)*pow(10,lim-1))+reverse(v/10,lim-1)); } void print_c(int digit,int l,int r=12) /*Method to print word equivalent of a number*/ { if(l!=2) /*l is the length of number */ { /*digit is the digit being processed*/ switch(digit) /*TO print digit at ones place*/ { case 1: cout<<"one "; break; case 2: cout<<"two "; break; case 3: cout<<"three "; break; case 4: cout<<"four "; break; case 5: cout<<"five "; break; case 6: cout<<"six "; break; case 7: cout<<"seven "; break; case 8: cout<<"eight "; break; case 9: cout<<"nine "; break; } } else if(l==2) /*to print digit at tens place*/ { switch(digit) { case 1: switch(r) /*TO print values such as ten,thirteen etc.*/ { case 0: cout<<"ten"; break; case 1: cout<<"eleven"; break; case 2: cout<<"twelve"; break; case 3: cout<<"thirteen"; break; case 4: cout<<"fourteen"; break; case 5: cout<<"fifteen"; break; case 6: cout<<"sixteen"; break; case 7: cout<<"seventeen"; break; case 8: cout<<"eighteen"; break; case 9: cout<<"nineteen"; break; } break; case 2: cout<<"twenty "; break; case 3: cout<<"thirty "; break; case 4: cout<<"fourty "; break; case 5: cout<<"fifty "; break; case 6: cout<<"sixty "; break; case 7: cout<<"seventy "; break; case 8: cout<<"eighty "; break; case 9: cout<<"ninty "; break; case 0: cout<<""; break; } } } int main() { int num,temp,length=0,result,n,m=0; cout<<"Enter the number :"; cin>>num; temp=num; for(;num>0;num/=10) { length++; } result=reverse(temp,length); while(result) { n=result%10; m=m*10+n; result/=10; if(length==1) { print_c(n,length); /*To print the digit at ones place*/ } else if(length==2) { if(n==1) { print_c(n,length,result); /*To print the digit at tens place like ten,twelve etc.*/ break; } else { print_c(n,length); /*To print the digit at tens place like twenty,thirty etc.*/ length--; } } else if(length==3) { print_c(n,length); /*To print the digit at hundred place*/ length--; if(n!=0) { cout<<"hundred "; } } else if(length==4) { print_c(n,length); /*To print the digit at thousand place*/ length--; cout<<"thousand "; } } return 0; }
Output
First Run: Enter the number :1005 One thousand five Second Run: Enter the number :01500 One thousand five hundred Third Run: Enter the number :1234 one thousand two hundred thirty four
cout<<result = mark > 30 ? “Passed” : “Failed”;
Ans.
if(mark>30)
{
cout<<result="Passed";
else
cout<<result="Failed";
}
16. . a) Correct the errors in the following program code to display numbers from 1 to 10.
for (i=1; i>-10; i++)
cout>>i;
Ans.
for(int 1=1;i<=10;++i)
{
cout<<i;
b) Explain the different types of programming errors with the help of the above code
17. Write the syntax of switch statement. Explain using an example of switch its working
Switch statement is a multiple selection statement.
Syntax
switch
(expression)
{
case constant 1 : statement block 1;
Break;
case constant 1 : statement block 1;
Break;
case constant 1 : statement block 1;
Break;
… .. … .. ..
default : statement block n;
}
Unlike in else if ladder here the case value should be a constant value (not a range of values). If any case value is matched corresponding statement block will be executed and then the break statement to exit from the switch statement. If no match is found , then the default block get executed.
Eg.
switch (day)
{
case 1: cout << “Sunday”;
break;
case 2: cout <<”Monday”;
break;
.. .. ……
default: cout<<”Wrong input”;
}
18. Compare the working of “do….. while” loop and “while” loop using an example.
do--while loop:
Syntax
Initialization of loop control variable;
do
{
body of the loop;
updating of loop control variable;
}
( test expression);
eg.
n=1;
do
{
cout << n ;
++n;
}
while(n<= 10);
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
while loop:
Syntax
Initialization of loop control variable
while ( test expression)
{
body of the loop;
updating of loop control variable;
}
eg.
n=1;
while( n< = 10)
{
cout << n ;
++n;
}
At first initialization takes place n=1 Then test expression evaluated n<=10 If it is true body of the loop executed, otherwise the program control goes out of the while loop. After execution of update expression ++n . repetition takes place until test expression become false.
19. There are three looping statements in C++.
a) Which is the exit-controlled loop?
Ans. do--while loop
b) How does it differ from an entry controlled loop?
If the evaluation of test expression takes place only at the end of looping statements , such loops are known as exit control loop. Here minimum one iteration takes place.
20. Rewrite the following switch statement using if – else if statement :
switch (n)
{
case 4 : cout<<"Excellent";
break;
case 3 : cout<<"Good";
break;
case 2 : cout<<"Average";
break:
case 1 : cout<<"Poor";
break;
default : cout<<"Invalid";
}
Ans.
if(n==4)
{
cout<<"Excellent";
else if(n==3)
cout<<"Good";
else if(n==2)
cout<<"Average";
else if(n==1)
cout<<"Poor";
else
cout<<"Invalid";
}
21. State whether the following statements are true or false. If false give reason.
a). Break statement is essential in Switch.-----------True
b). For loop is an entry controlled loop. ---------- True
c). Do .. .. while loop is an entry controlled loop.--------- False
do---while loop is a exit contolled loop.
d). Switch is a selection statement.----------- True
22. Write a program to find the biggest from 3 given numbers
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
{
cout << "Largest number: " << n1;
}
if(n2 >= n1 && n2 >= n3)
{
cout << "Largest number: " << n2;
}
if(n3 >= n1 && n3 >= n2) {
cout << "Largest number: " << n3;
}
return 0;
}
23. Rewrite the following code using if … .. else ladder.
#include<iostream>
using namespace std;
int main( )
{
int colour;
cout <<” Enter number between 1 and 4 : “;
cin>> colour;
switch(colour)
{
case 1 :
cout<< “Red”;
break;
case 2 :
cout<<”Green”;
break;
case 3 :
cout<<”Blue”;
break;
default :
cout<<”Wrong input”;
}
}
Ans.
#include<iostream>
using namespace std;
int main( )
{
int colour;
cout <<” Enter number between 1 and 4 : “;
cin>> colour;
if(colour==1)
{
cout<< “Red”;
else if(colour==2)
cout<< "Green";
else if(colour=="Blue";
cout<<"Blue";
else
cout<<”Wrong input”;
}
}
24. Rewrite the following code using switch case statement.
if (Lan=='M')
cout<< " I prefer Malayalam”;
else if (Lan = 'E’)
cout "I prefer English";
else
cout<<” I prefer neither Malayalam nor English” ;
Ans
switch(Lan)
{
case M:
cout<<" I prefer Malayalam”;
break;
case E:
cout <<"I prefer English";
break;
default:
cout<<” I prefer neither Malayalam nor English” ;
}
25. Write a C++ program to find the simple interest of an amount (P) deposited with a rate of interest (R) for a period of Years (N).
Rate of interest = 7% If deposit amount P is less than 1 lakh.
Rate of interest = 8% if. Deposit amount P is between 1 lakh and 5 lakhs.
Rate of interest = 9o/o if deposit amount P is above 5 lakhs,
(Hint : Simple interest = P x N x R/100)
#include<iostream.h> #include<conio.h> int main() { float amount, rate, time, si; cout<<"Enter Principal Amount: "; cin>>amount; cout<<"Enter Rate of Interest: "; cin>>rate; cout<<"Enter Period of Time: "; cin>>time; si = (amount * rate * time) / 100; cout<<"Simple Interest: "<<si; return 0; }