Kerala Plus One Computer Application Chapter Wise Questions Chapter 7 Control Statements
Plus One Computer Application Control Statements 1 Mark Questions and Answers
Question 1.
An if statement contains another if statement completely. Then it is known as _______.
Answer:
Nested if
Question 2.
From the following which is not optional with switch statement.
a) break
b) default
c) case
d) none of these
Answer:
c) case.
Question 3.
To exit from a switch statement ____ is used
a) quit
b)exit
c) break
d) none of these
Answer:
c) break
Question 4.
From the following which statement is true for switch statement
a) switch is used to test the equality
b) switch is used to test relational or logical expression
c) switch can handle real numbers case data
d) none of these
Answer:
a) switch is used to test the equality
Question 5.
Sonet wants to execute a statement more than once. From the following which is exactly suitable.
a) if
b) loop
c) switch
d) if -else if ladder
Answer:
b) loop
Question 6.
Odd one out
a) for
b) if
c) switch
d) if else if ladder
Answer:
а) for
It is a loop the others are branching statement
Question 7.
a) for
b) if
c) while
d) do while
Answer:
b) if.
It is a branching statement and the others are loops
Question 8.
From the following which loop does the three things, initialisation, checking and updation.
a) while
b) do while
c) for
d) none of these
Answer:
c) for
Question 9.
Predict the output
for(i=1;i<=10;i++);
cout << i;
a) 10
b) 1 to 10
c) 11
d) none of these
Answer:
c) 11
Question 10.
From the following which is exit controlled loop
a) for
b) while
c) do while
d) None of these
Answer:
do while
Question 11.
_______ statement is used for unconditional jump from one location to another.
Answer:
goto
Question 12.
Sunitha wants to skip one iteration. From the fol-lowing which will help her?
a) continue
b) break
c) for
d) case
Answer:
a) continue
Question 13.
To terminate a program, from the following which is used ?
a) break
b) continue
c) end( )
d) exit( )
Answer:
exit( )
Question 14.
Which header file is needed to use exit( ) function in a program?
a) iostream
b) cstdlib
c) math
d) iomanip
Answer:
b) cstdlib
Question 15.
In while loop, the loop variable should be updated?
a) along with while statement
b) after the while statement
c) before the while statement
d) inside the body of while
Answer:
d) Inside the body of while
Question 16.
How many times the following loop will execute?
int S = 0, i =0;
do
{
S+= i;
i++;
}
while(i<5);
Answer:
5 times
Question 17.
a) _____ statement takes the program control out of the loop even though the test expression is true.
b) Consider the following code fragment. How many times will the character ‘ * ’ be printed on the screen?
for (1=0; i< 10; i =i+2);
{
cout << “*”;
}
Answer:
a) break or goto
b) Only one time because of semicolon(;) in the end of the for(i=0;i<10;i=i+2);
Question 18.
Which selection statement tests the value of a variable or an expression against a list of integers or character constants?
a) For
b) If
c) Switch
d) Conditional expression
Answer:
c) switch
Question 19.
How many times the following loop will execute?
int m = 2
do
{
cout << “Welcome”;
m++ ;
} while (m>10);
Answer:
Only one time.
Question 20.
statement takes the program control outside a loop even though the test expression is true.
Answer:
break
Question 21.
Read the following C++ statement
for (int n = 1; n<10; n+=2);
cout << n
Now, choose the correct output from the following options.
a) 1
b) 13579
c) 11
d) 10
Answer:
c) 11.
This is because of for statement is end with: (semi colon).
Here cout << n; executes only once.
Question 22.
______ search method is an example for ‘divide and conquer method’.
Answer:
goto
Question 23.
a) Name the type or loop which can be used to ensure that the body of the loop will surely be executed at least once.
b) Consider the code given below and predict the output;
for (int i=1; i<=9;i=i+2)
{
if (i==5) continue;
cout << i << ” ”
}
Answer:
a) do while loop(Exit controlled loop)
b) 1 3 7 9. It bypasses one iteration of the loop when i=5.
Plus One Computer Application Control Statements 2 Marks Questions and Answers
Question 1.
Your friend Arun asked you that is there any loop that will do three things, initialization, testing and updation. What is your answer. Explain ?
Answer:
Yes,
There is only one loop namely for loop that will do this three things. The other loops will do the . checking only, initialisation must be do before the 1 loop and updation must be inside the loop.
The syntax of for toop is given below
For(initialisation; testing; updation)
{
Body of the for loop;
}
Question 2.
While writing a program Geo uses while loop but forgets to update the loop variable. What will happen?
Answer:
The loop variable inside the while loop must be updated otherwise the loop will not be terminated The loop will be work infinitely.
Question 3.
Draw the flow chart of if statement.
Answer:
Question 4.
Draw the flow chart of if else statement .
Answer:
Question 5.
Write a while loop that display numbers from 500 to 550.
Answer:
int i=500
while (i<=550)
{
cout << i;
i=i+1;
}
Question 6.
Distinguish between exit(O) function and return statement.
Answer:
Both are used to terminate the program but both are different. Return is a keyword and exit(0) is a function.The difference is, we can use more than one exit(0) function but we can use only one return statement in a scope. To use exit(0), the header file cstdlib should be used.
Question 7.
Draw the flow chart of for loop.
Answer:
Question 8.
How many time the following for loop will execute? Justify.
for(i = 0; ; i ++)
{
if(i > 5)
cout << “continue”;
else
cout << “over”;
}
Answer:
Here the loop becomes infinite because the check condition is missing.
Question 9.
Predict the output.
#inctude<iostream.h>
int main( )
{
int a = 0;
start:
cout << endl << ++a;
if(a < 5)
goto start;
}
Answer:
1
2
3
4
5
Question 10.
for(int i=2, sum=0; i <= 20; i=i+2) sum += i;
Answer:
Rewrite the above code using while loop.
int i = 2;
sum=0;
while (i<=20)
{
sum +=i;
i = i +2;
}
Question 11.
Rewrite the following code using switch case statement.
if(day == 1)
cout << “Sunday”;
eise if(day == 2)
cout << “Monday”;
else if(day == 7)
cout << “Saturday”;
else
cout << “Wednesday”; (2 Scores)
Answer:
switch (day)
{
case 1:cout << “Sunday”;break;
case 2:cout << “Monday”;break;
case 7:cout << “Saturday”;break;
default:cout << “Wednesday”;
}
Question 12.
Pick the odd one out from the following. Give reason
1) a) for
b) while
c) do while
2) a) if
b) switch
c) for
Answer:
1) do ______ while It is an exit controlled loop while
others are entry controlled loop
2) for. It is a loop while others are branching statements.
Question 13.
State whether the following statements are True or False. In either case Justify your answer
a) Break statement is essential in switch
b) For loop is an entry controlled loop
c) Do… while loop is an entry controlled loop
d) Switch is a selection statement
Answer:
a) False. It is not essential in single case statement
b) True. Because it will first .check the condition. If it is true then only the body will be executed.
c) False. It is an exit controlled loop.
d) True.
Question 14.
Write the equivalent code for the following statement
R=(P<Q?P:Q)
Answer:
if(P<Q)
R=P;
else
R=Q;
Question 15.
Examine the following code snippet and find out the output? What will happen if the statement int ch; is replaced by char ch;
int ch;
for(ch=,A,;ch<-Z,;++ch)
cout << ch << ”;
Answer:
This code snippet will print 65,66,67 90. If
the statement int ch; is replaced by char ch; it prints A,B,C,………..,Z.
Question 16.
a) ______ is an entry control loop.
b) Explain the memory allocation for the following
int A[10] [10];
Answer:
a) while or for loop
b) To store On integer 4 bytes is used in Geany Editor
int A[10] [10]; .-> It needs 10*10*4 = 400 bytes7
Question 17.
Differentiate between break and continue statements in C++.
Answer:
break statement: It is used to skip over a part of the code i.e. we can premature exit from a loop such as while, do-while, for or switch.
Syntax:
while (expression)
{
if (condition)
break;
}
continue statement : It bypasses one iteration of the loop. That is it skips one iteration and continue the loop with next iteration value.
Syntax:
while (expression)
{
if (condition)
continue;
}
Plus One Computer Application Control Statements 3 Marks Questions and Answers
Question 1.
Compare if else and conditional operator?
Answer:
We can use conditional operator as an alternative of if-else statement. The conditional operator is a ternary operator.
The syntax of if- else
if (expression 1)
expression 2;
else
expression 3;
First expression 1 is evaluated if it is true expression 2 will be executed ptherwise expression 3 will be executed. Instead of this, we can be written as follows using conditional operator Expression 1 ? expression 2: expression3;
Question 2.
#include<iostream>
using namespace std;
int main( )
{
int n;
cout << “Enter a number in between 1-7”;
cin << n;
switch (n)
{
case 1: cout << “Sunday”;
break;
case 2: cout << “Monday”;
break;
case 3: cout << “Tuesday”;
break;
case 4: cout << “Wedesday”;
break;
case 5: cout << “Thursday”;
break;
case 6: cout << “Friday”;
break;
case 7: cout << “Saturday”;
break;
default : cout << “lnvalid”,
}
}
Rewrite the above code using if else if ladder.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n;
cout << “Enter a number in between 1-7:”;
cin << n;
if(n==1)
cout << “Sunday”;
else if(n=5=2)
cout << “Monday”;
else if(n==3)
cout << “Tuesday”;
else if(n==4)
cout << “Wedesday”;
else if(n==5)
cout << “Thursday”;
else if(n==6)
cout << “Friday”;
else if(n==7)
oout << “Saturday”; ‘
else
cout << “lnvalid”;
}
Question 3.
Rewrite the program following program using if else
#include<iostream>
using namespace std;
int main( )
{
int a,b,big;
cout << “Enter two integers”;
cin << a << b;
big=(a>b)?a:b;
cout << “Biggest number is ” << big << endl;
return 0;
}
Answer:
#include<iostream>
using namespace std;
intmain( )
{
int a,b,big;
clrscr( );
cout << “Enter two integers”;
cin << a << b;
if (a>b) big=a;
else big=b;
cout << “Biggest number is ” << big << endl;
return;
}
Question 4.
Consider the following code
#include<iostream>
using namespace std;
intmain( )
{ int mark;
clrscr( );
cout << “Enter a mark”; cin << mark;
if(mark>=75)
cout << “Distinction”;
else if (mark>=60) .
cout << “First class”;
else if (mark>=50)
cout << “Second class”;
else if (mark>=40) cout << “Passed”;
else
cout << “Failed”; .
}
Is it possible to rewrite the above program using switch statement? Distinguish between switch and if else if ladder.
Answer:
No. It is not possible to write the above code using switch statement. Following are the difference between switch and if else if ladder.
1) Switch can test only for equality but if can evaluate a relational or logical expression
2) If else is more versatile
3) If else can handle floating values but switch cannot .
4) If the test expression contains more variable if else is used
5) Testing a value against a set of constants switch is more efficient than if else.
Question 5.
Rewrite the following using nested switch construct
#include<iostream>
using namespace std;
int main( )
{
int a,b; .
cout << “Enter values for a and b”;
cin << a << b;
if(b==0)
cout << “Divide by zero error”;
else
if (a==0) .
cout << “The result is zero”;
else
cout << “The result is ” << (float)a/b;
}
Answer:
#include<iostream>
using namespace std;
int main( )
{
inta.b;
cout << “Enter values for a and b”;
cin << a << b;
switch (b)
{
case 0:cout << “Divide by zero error”;
break;
default:
switch(a)
{
case 0:cout << “The result is zero”;
break; .
default:
cout << “The result is” << (float)a/b;
}
}
}
Question 6.
Consider the following output and write down the code for the same.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i j;
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
cout << “*”;
cout << endl;
}
}
Question 7.
Consider the following output and write down the code for the same:
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i,j;
for(i=1;i<5;i++)
{
forG=1;j<=i;j++) cout << j << “”; cout << endl;
}
}
Question 8.
code for the same.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i, j;
for(i=1;i<5;i++)
{
forG=1;j<=i;j++)
cout << i << ” “;
cout << endl;
}
}
Question 9.
Consider the following output and write down the code for the same.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i, j, k=0;
for(i=1;i<5;i++)
{
for j=1;j<=i;j++)
cout << ++k << ” “;
cout << endl;
}
}
Question 10.
Consider the following output and write down the code for the same.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i,j;
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
cout << ++k << ” “;
cout << endl;
}
}
Question 11.
Consider the following output and write dowri the code for the same.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i, j;
for(i=1 ;i<5;i++)
{
for(j=i ;j<=i;j++) cout << i*2 << ” “;
cout << endl;
}
}
Question 12.
Write a program to print the sum of first n natural numbers
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,i,sum=0;
cout << :”Enter a value for n”;
cin << n;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout << “The sum of first ” << n << ” numbers is ” << sum;
}
Question 13.
Write a program to read a number and check whether it is palindrome or not.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,m,rem,rev=0;
cout << “Enter a number”;
cin << n;
m=n;
while(n)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==m)
cout << “The number ” << m << ” is palindrome”;
else
cout << “The number ” << m << ” is not palindrome”;
}
Question 14.
Write a program to print the factorial of a number .
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,i;
longfact=1; .
cout << “Enter a number”;
cin << n;
for(i=1 ;i<=n;i++) fact=fact*i;
cout << “The factorial of “<;
<n << ” is ” << fact;
}
Question 15.
Write a program to print the Fibonacci series
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n, fib1=0,fib2=1,fib3;
cout << “Enter the limit”;
cin << n;
cout << “The fibonacci series is ”
if(n==1)
cout << fib1 << “,”;
else if(n==2)
cout << fib1 << “,” << fib2 << “,”;
else if (n>2)
{
cout << fib1 << “,” << fib2 << “,”;
fib3=fib1+fib2;
while(fib3<=n)
{
cout << fib3 << “,”;
fib1=fib2;
fib2=fib3; ,
fib3=fib1+fib2;
}
}
else
cout << “lnvalid”;
}
Question 16.
Write a program to read a number and check whether the given number is Armstrong or not
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,m,rem,cube=0;
cout << “Enter a number”;
cin >> n;
m=n;
while(n)
{
rem=n%10;
cube=cube+rem*rem*rem;
n=n/10;
}
if(cube == m)
cout << “The number ” << m << ” is Armstrong”;
else
cout << “The number ” << m << ” is not Armstrong”;
}
Question 17.
Write down the code for the following output using while loop.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i,j;
i=1
while(i<=4)
{
j=1;
while(j<=i)
{
cout << “*”;
j++;
}
i++;
cout << endl;
}
}
Question 18.
Distinguish between entry controlled loop and exit controlled loop.
Answer:
An entry controlled loop first checks the condition and execute(or enters in to) the body of loop only if it is true. But exit control loop first execute the body of the loop once even if the condition is false then check the condition. The for loop and while loop are entry, controlled loops but do- while loop is an exit controlled loop.
Question 19.
Write a program to find the largest of 3 numbers.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int a,b,c;
cout << “Enter three numbers”;
cin << a << b << c;
if (a > b && a > c)
cout << a << ” is large”;
else if (b>a && b>c) cout << b << ” is large”;
else
cout << c << ” is large”;
}
Question 20.
Check whether a given number is prime or not
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,i;
cout << “Enter a number”;
cin << n;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
cout << n << “ is not a prime no.”;
return 0;
}
}
cout << n << “ is a prime”;
}
Question 21.
Write a program to print the prime numbers less than 100
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i,j=4;
cout << “The prime numbers less than 100 is given below\n2,3,”;
while(j<=100)
{
for(i=2;i<=j/2;i++)
if(j%i==0)
break;
if(i==j/2+1)
cout << j << “,H;
j++;
}
}
Question 22.
Write a program to read number and display its factors.
Answer:
#include<iostream>
using namespace std;
int main( )
int n,i;
cout << “Enter a number greater than zero”;
cin << n;
cout << ‘The factors are”;
for(i=1;i<=n;i++)
if(n%i==0)
cout << i << “,”;
}
Question 23.
Write a program to print the Armstrong numbers less than 1000
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,i,rem,cube; for(i=1;i<=1000;i++)
{
n=i;
cpbe=0;
while(n)
{
rem=n%10;
cube=cube+rem*rem*rem;
n=n/10;
}
if(cube==i)
cout << i << “,”;
}
}
Question 24.
Char result;
float marks;
cin << marks;
if (marks >=50)
result = ‘P’;
else
result = ‘F’;
cout << result;
Rewrite the above code without using if statement.
Answer:
result=(marks>=50) ?’P’:’F’;
Question 25.
The output of a program is given below.
1
3
5
7
9
The sum is 25
Write a C++ program for obtaining the above output.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int sum=0,i;
for (i=*1; i<=9; i+=2)
{
cout << i << endl;
sum=sum+i;
}
cout << ‘The sum is” << sum;
}
Question 26.
Find out the error in syntax if any and correct it?
a) while (test condition);
{
}
b) do (condition)
{
}
while
c) switch(condition)
{
Case 1:
Case 2:
Case 3:
Case 4:
}
Answer:
a) No need of semi colon. The corrected loop is given below
While (test condition)
{
}
b) In do … while loop the while must be end with semicolon.
do (condition)
{
}
while;
c) switch contains expression instead of condition switch(expression)
{
Case 1:
Case 2:
Case 3:
Case 4:
}
Question 27.
Given the total mark of each student in SSLC examination. Write a C++ code fragment to find the grades.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int mark;
cout << “Enter total mark”;
cin << mark;
if (mark>100 || mark<0) cout << “lnvalid mark”;
else
{
If (mark>=90)
cout << “Your grade is A+”;
else if (mark>=80)
cout << “Your grade is A”;
else if (mark>=70)
cout << “Your grade is B+”;
else if (mark>=60)
cout << “Your grade is B”;
else if (mark>=50)
cout << “Your grade is C+”;
else if (mark>=40)
cout<£”Your grade is C”;
else if (mark>=30)
cout << “Your grade is D+”;
else
cout << “Your grade is D”;
}
}
Question 28.
You are given the heights of 3 students. Write the relevant code segment to find the maximum height?
Answer:
#include<iostream>
using namespace std;
int main( )
{
int h1,h2,h3,max;
cout << “Enter heightl cin << h1;
cout << “Enter height2:”;
cin << h2;
cout << “Enter height3:’f;
cin << h3;
if(h1>h2 && h1>h3)
max=h1;
else if(h2>h1 && h2>h3)
maxih2; .
else
max=h3;
cout << “The maximum height is “ << max;
}
Question 29.
Write the easiest code snippet for printing your name 1000 times. Explain
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i;
char name[20];
cout << “Enter your name: cin << ‘name;
for(i=0;i<1000;i++)
cout << name << endl;
}
Question 30.
Given a code segment for(i=1; i<10; i++)
cout << i;
a) Rewrite the code using do while loop
b) What will be the output when i=0? Give reason.
Answer:
a) i=1;
do{
cout << i;
i++; .
}
while(i<10);
b) When i=0, it will execute one more time. ie. the for loop execute 9 times but here this loop executes 10 times.
Question 31.
Whenever a string is entered the inverse of that string is displayed( eg: if we enter-‘CAR’ the output is ‘RAC’). Write a suitable programme for the output.
Answer:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main( )
{
int i, len;
char str[80];
puts(Enter a string :”);
gets(str);
len=strlen(str);
for(i=len-1;i>=0;i–)
putchar(str[i]).;
}
Question 32.
Qn. 32
Write a C++ program to display as follows
Answer:
#include<iostream>
using namespace std;
int main( )
{
char i,j;
for(i=’A’.;i<‘F’;i++)
{
for(j=’A’;j<=i;j++)
cout << j << “\t”;
cout << endl;
}
}
OR
#include<iostream>
using namespace std;
int main( )
{
int i,j;
for(i=65;i<70;i++)
{
‘ for(j=65;j<=i;j++)
cout << (char)j << “\t”;
cout << endl;
}
}
Question 33.
Write C++ program forgetting the following output.
OR
Consider the following C++ program and answer the following questions.
#include<iostream.h>
int main( )
{
int a, p=1;
for(a=1;a<=5;a+=2)
P = P*a;
cout << p;
}
a) Predict the output of the above code.
b) Rewrite the above program using while loop.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int i, j;
for(i=1;i<=4;i++)
{
for j=1;j<=i;j++)
cout << j << “\t”;
cout << “\n”; ‘
}
}
OR
a) The output is 15.
b) #include<iostream>
using namespace std;
int main( )
{
inta=1,p=1;
while(a<=5)
{
p=p*a;
a+=2;
}
cout << p;
}
Plus One Computer Application Control Statements 5 Marks Questions and Answers
Question 1.
We know that the execution of a program is sequential”. Is it possible to change this sequential manner and explain different jump statements in detail. .
Answer:
The execution of a program is sequential but we can change this sequential manner by using jump statements. The jump statements are
1. goto statement: By using goto we can transfer the control anywhere in the program without any condition. The syntax is goto label;
Eg.
#include<iostream>
using namespace std; int main( )
{
float a,b;
cout << “Enter 2 numbers”;
cin << a << b; . .
if(b==0)
goto end;
cout << “The quotient is ” << a/b;
return 0;
end:
cout << “Division by zero error”;
}
2. break statement: It is used to skip over a part of the code i.e. we can premature exit from a loop such as while, do-while, for or switch.
Syntax :
while (expression)
{
if (condition)
break;
}
Eg.
#include<iostream>
using namespace std;
main( )
{
int i=1;
while(i<10)
{
cout << i << endl;
if(i==5) break;
i++;
}
}
The output is
1
2
3
4
5
3. continue statement : lt bypasses one iteration of the loop.
Syntax :
while (expression)
{
if (condition)
break;
}
Eg:
#include<iostream>
using namespace std;
main( )
{
int i=0;
while(i<10)
{
i++; .
if(i==5) continue;
cout << i << endl;
}
}
The output is
1
2
3
4
5
6
7
8
9
10
4. exit(0) function: It is used to terminate the pro-gram. For this the header file cstdlib must be included.
Question 2.
Mr. X wants to get an output 9 when inputting 342 and he also wants to get 12 when inputting 651. Write the program and draw a suitable flowchart forX?
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,rem,sum=0;
cout << “Enter a number”;
cin << n;
while(n)
{
rem=n%10;
sum=sum+rem;
n= n /10;
}
cout << “The sum of digits is ” << sum;
}
Question 3.
Explain conditional statements in detail?
Answer:
1. Simple if:- The syntax is given below
if(expression)
statement;
or
if(expression)
{
Block of statements
}
First expression evaluates if it is true then only statement will be executed.
Eg. if (n>0)
cout << n << ” is positive”;
2. if else :-The syntax is given below,
if (expression)
statement 1;
else
statement 2;
or
if (expression)
{
statement block 1;
}
Else
{
statement block 2;
}
First expression evaluates if it is true statement block 1 will be executed otherwise statement block 2 will be executed. Only one block will be executed at a time so it is called branching statement.
Eg.
if (n>0)
cout << n << ” is positive”;
else
cout << n << ” is negative”;
3. if else if ladder:- The syntax will be given below
if (expression 1)
{
statement block 1;
else if (expression 2)
{
statement block 2;
}
else if (expression 3)
{
statement block 3;
}
else
{
statement block n;
}
Here first expression 1 will be evaluated if it is true only the statement block 1 will be executed otherwise expression 2 will be executed if it is true only the statement block2 will be executed and so on. If all the expression evaluated is false then only statement block n will be evaluated .
Eg.
If (mark>=90)
cout << “Your grade is A+”;
else if (mark>=80)
cout << “Your grade is A”;
else if (mark>=70)
cout << “Your grade is B+”;
else if (mark>=60)
cout << “Your grade is B”;
else if (mark>=50)
cout << “Your grade is C+”;
else if (mark>=40)
cout << “Your grade is C”;
else if (mark>=30)
cout << “Your grade is D+”;
else
cout << “Your grade is D”;
4. conditional operator:- It is a ternary operator and it is an alternative for if else construct.
The syntax is given below.
expression 1? expression 2: expression 3;
OR
expression 1? Value if true : value if false;
Here expression 1 will be evaluated if it true expression 2 will be executed otherwise expression 3 will be executed.
Eg.
n>0?cout << n << ” is positive”:cout << n << ” is negative”;
5. Switch It is a multiple branch statement. Its syntax is given below.
switch(expression)
{
case value: statements;break;
case value: statements;break;
case value: statemerits;break;
case value: statements;break;
case value: statements;break;
………….
default : statements;
}
First expression evaluated and selects the statements with matched case value.
Eg.
switch(n)
{
case 1: cout << “Sunday”;break;
case 2: cout << “Monday”;break;
case 3: cout << “Tuesday”;break;
case 4: cout << “Wedesday”;break;
case 5: cout << “Thursday”;break;
case 6: cout << “Friday”;break;
case 7: cout << “Saturday”;
break;
default : cout << “lnvalid”
}
Question 4.
Explain different loops in detail?
Answer:
1. For loop: The syntax of fbr loop is for(initialization; checking ; update loop variable)
{
Body of loop;
}
First part, initialization is executed once, then checking is carried out if it is true the body of the for loop is executed. Then loop variable is updated and again checking is carried out this process continues until the checking becomes false. It is an entry controlled loop.
Eg. for(i=1,j=1;i<=10;i++,j++)
cout << i << ” * ” << j << ” = ” << i*j;
2. While loop : It is also an entry controlled loop The syntax is given below
Loop variable initialised
while(expression)
{
Body of the loop;
Update loop variable;
}
Here the loop variable must be initialised out side the while loop. Then the expression is evaluated if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.
Eg.
i=1;
j=1;
while(i<=10)
{
cout << i << ” * ” << j << ” = ” << i*j;
i++;
j++;
}
3. do While loop: It is an exit controlled loop. The syntax is given below do
{
Statements
}while(expression);
Here the body executes atleast once even if the condition is false. After executing the body it checks the expression if it false it quits the body otherwise the process will be continue.
Question 5.
Write a program to do the following:
a) Inputs the values for variables ‘n’ and’m’.
b) Prints the numbers between ‘1 ’ and ‘n’which are exactly divisible by ‘m’.
c) Checks whether the numbers divisible by ‘m’ are odd or even.
OR
Write a program using nested loop that inputs a number ‘n’ which generates an output as follows. Hint: if the value of ‘n’ is 5, the output will be as ‘n’.
—–
Answer:
b) #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i,n,m;
cout << “Enter values for n and m”;
cin << n << m;
for(i=1;i<=n;i++)
if(i%m==0)
cout << i << “,”;
getch( );
}
c) #include<iostneam.h>
#include<conio.h>
void main( )
{
clrScr( );
int i,n,m;
cout << “Enter values for n and m”;
cin << n << m;
for(i=1;i<=n;i++)
if(i%m==0)
{
cout << i << “\t”;
if(i%2==0)
cout << “even” << endl;
else
cout << “odd” << endl;
}
getch( ):
}
OR
#include<iostream.h>
#include<conio.h>
#include<string.h>//forstrlen( )
main( )
{
clrscr( );
int n,i,j;
cout << “enter a value for n:“;
cin >> n;
for(i=n;i>0;i–)
{
for(j – n;j > =i;j—) .
cout << j*j << “\t”;
cout << endl;
}
getch( );
}
Question 6.
Write a C++ program to display Fibonacci series.
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,fib1=0,Fib2=1,fib3;
cout << “Enter the limit”;
cin << n;
cout << “The fibonacci series is”;
if(n==1)
cout << fib1 << “,”;
else if(n==2)
cout << fib1 << “,” << fib2 << “,”;
else if (n>2)
{
cout << fib1 << “,” << fib2 << “,”;
fib3=fib1 +fib2; while(fib3<=n)
{
cout << fib3 << “,”;
fib1=fib2;
fib2=fib3;
fib3=fib1+fib2;
}
}
else
cout << “lnvalid”;
}
Question 7.
Write a C++ program to aocept an integer number and check whether it is an Armstrong number or not.
(Hint: Sum of the cubes of the digits of an Armstrong number is equal to that number itself)
OR
Write a C++ program to accept an integer number and print its reverse
(Hint: If 234 is given, the output must be 432).
Answer:
#include<iostream>
using namespace std;
int main( )
{
int n,m,rem,cube=0;
cout << “Enter a number”;
cin << n;
m=n;
while(n)
{
rem=n%10;
cube=cube+rem*rem*rem;
n=n/10;
}
if(cube==m)
cout << “The number ” << m << ” is Armstrong”;
else
cout << “The number ” << m << ” is not Armstrong”;
}
OR
(b) # include<iostream>
void main ( )
{
int n, rem, rev = 0;
cout << “Enter a number:”;
cin << n;
while (n)
{
rem = n%10;
rev = rev*10+rem;
n = n/10;
}
cout << “The reverse is” << rev;
}
Question 8.
Write a menu driven program which accepts 3 numbers and show options to find and display.
a) (i) the biggest number
(ii) the smallest number
(iii) the sun of the numbers
(iv) the product of the numbers
OR
b) Write a C++ program to check whether a number is palindrome or not.
Answer:
#include<iostream>
using namespace std;
int big (int n1, int n2, int n3)
{
if (n1>n2 && n1>n3)
return n1;
else if (n2>n1 && n2>n3)
return n2;
else
return n3;
}
int small (int n1, int n2; int n3)
{
if (n1<n2 && n1 < n3) return n1;
else if (n2<n1 && n2<n3)
return n2;
else
return n3;
}
int sum (int n1, int n2, int n3)
{
return (n1+n2+n3);
}
int prod (int n1, int n2, int n3)
{
return (n1*n2*n3);
}
void main ( )
{
\ intx,y,z,ch;
cout << “Enter 3 nos”;
cin << x << y << z;
cout << “Enter 1 for the biggest numebr”;
cout << “\n 2 for the smallest number”;
cout << “\n 3 for the sum of numbers”;
cout << “\n4 for the product of numbers”;
cout << “\n Enter your choice:”;
cin << ch;
switch (ch)
{
case 1 : cout << “The biggest number is” << big(x,y,z);
break; .
case 2 : cout << “The smallest number is” << small(x,y,z);
break;
case 3: cout << “The sum of number is” << big(x,y,z);
break;
case 4 : cout << “The product is” << prod(x,y,z);
break;
default: cout << “lnvalid choice”;
}
}
OR
b) #include<iostream>
using namespace std;
int main( )
{
int n,m,rem,rev=0;
cout << “Enter a number”;
cin << n;
m=n;
while(n)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==m)
cout << “The number ” << m << ” is palindrome”;
else
cout << ”The number ” << m << ” is not palindrome”;
}
Question 9.
Answer any one question from (a) and (b).
a) Write a C++ program to display all leap years between 1000 and 2000 excluding all century years.
OR
b) Write a C++ program to find the sum of the first 10 numbers of Fibonacci series. (Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 15 where 0 and,1 are the first two terms-and remaining terms are obtained by the sum of the two preceding terms.)
Answer:
a) # include < iostream>
using namespace std;
int main ( )
{
int i;
cout << “Leap years between 1000 and 2000 is given below \n”;
for (i= 1000; i<= 2000; i++)
{
if (i % 100 = = 0)
{
if (i % 400 == 0) cout << i << “, ”;
} else if (i % 4 = = 0) cout << i << “, ”;
}
}
OR
b) # include < iostream>
using namespace std;
void main ( )
{
int i, fib1 = 0, fib2 = 1, fib3, S ;
S = fib1+fib2;
for (i = 1; i <= 8; i + +)
{
fib3 = fib1 + fib2;
S = S + fib3;
fib1 = fib2; fib2 = fib3;
}
cout << “The sum of first 10 numbers of Fibonacii series is” << s;
}
Question 10.
Write a program to check whether the given number is palindrome or not.
OR
Write a program to print the leap years between 2000 and 3000.
(A century year is leap year only if it is divided by 400 and a noncentury year is leap year only if it is divided by 4.
Answer:
#include<iostream>
using namespace std;
int main( ) ‘{
int n,m,rem,rev=0; cout << “Enter a number”; cin << n;
m=n; while(n)
{
rem=n%10;
rev? rev*10+rem;
n=n/10;
}
if(rev==m)
cout << “The number “ << m << “ is palindrome”;
else
cout << “The number “ << m << “ is, not palindrome”; .
}
OR
#include<iostream>
using namespace std;
int main( )
{
int year;
for(year=2000;year<=3000;year++)
{
if(year%100==0)
{
if(year%400==0)
cout << year << endl;
}
else
{
if(year%4==0)
cout << year << endl;
}
}
}