Anda di halaman 1dari 18

Ch: 10 Flow of Control   if(a=5) 

Page 237 cout<<ʹFiveʺ; 


1. What is the significance of a null statement?   else 
Ans  1:  a  null  statement  is  simply  a  semicolon(;).  Sequence  of  statements      out<<ʺNot Fiveʺ; 
enclosed in a pair of braces{} is called compound statement. e.g. {statement;  if the input given is (i) 7 (ii) 5? 
statement2;}  Ans:  The output is :”Five” for input 7 and 5. 
   
2. What are the three constructs that govern statement flow?  6. What will be the output of the following code fragment: 
Ans: 1. Sequence construct, 2. selection construct and 3. Iteration construct.  int year; 
  cin>>year; 
Page 248  if(year%10==0) 
1. What is the significance of a test‐condition in a loop?    if(year%400==0) 
Ans:  The  test  condition  decides  whether  the  loop  body  will  be  executed  or      cout<<ʺLeap ʺ; 
not. If true the loop body gets executed o.w. the loop is terminated.    else 
  cout<<ʺNot century yearʺ; 
2. What is a selection statement? Which selection statements does C++  if the input given is (i)) 2000 and (ii) 1900 (iii) 1971? 
provide?  Ans: :   input     output 
Ans:  The  selection  statements  allow  to  choose  the  set  of  instructions  for  2000 LEAP 
execution  depending  upon  an  expression’s  truth  value.  If  and  switch  are  2    1900    Not century year 
types of selection Statements C++ provides.    1971    no output. 
   
3. Can a conditional operator replace an if statement always?  Page 249 
Ans:  No!  for  a  single  and  simple  condition  it’s  ok  but  ‘if’  will  be  better  for  1. What will be the output of the following code fragment? 
multiple and complex conditions.  int year; 
  cin>>year; 
4. Correct the following code fragment:  if(year%10==0) 
if (x=1)    { 
k=100;  if(year%400==0) 
else        cout<<ʺLeap ʺ; 
k=10;    }   
Ans:  if(x==1)   else 
k=100;   cout<<ʺNot century yearʺ; 
else   if the input given is (i)) 2000 and (ii) 1900 (iii) 1971? 
   k=10;  Ans:   input     output 
  2000    LEAP 
5. What will be the output of the following fragment:      1900    No output 
cin>>a;      1971    Not Century year. 

1/18
  1. What are iterations statements? Name the iteration statements provided 
2. In a nested‐if, how does the default matching of dangling‐ else take  by C++.  
place? suggest a way to override the default dangling else matching.  Ans: Iteration statements repeat the group of statements until the test 
Ans: The dangling else the else goes with immediate if(nearest if). We can  condition is true. The iteration   statements provided by C++ are for, while 
override this problem by using the parenthesis at proper places.  and do‐while. 
Page 256  2. Which elements are needed to control a loop?  
1. Write one advantage and one disadvantage of using? ? : in place of an if.  Ans: The test condition and updating of loop variable are important to 
  Ans: Adv: It gives compact and simple to understand code.   control a loop. 
  Dis adv.: It becomes complex and difficult in the nested form.  3. What  is  meant  by  an  entry  controlled  loop?  Which  C++  loops  are  entry 
2. What is the significance if a break statement in a switch statement? What  controlled loops?  
is the effect of absence of break in a switch statement?  Ans: In entry controlled loops the loop condition   is checked first and then 
Ans: If a case is matched it is executed till break statement. If break is  the loop body is executed. The for loop and while loops are entry controlled 
missing all the case statements are executed till the break or end of  loops. 
switch case.  4. What  is  meant  by  exit  controlled  loops?  Which  C++  loops  are  exit 
3. Write one limitation and one advantage of a switch statement?   controlled? 
Ans: Advantage: it is best suited for check of single value.  Limitation of   Ans:  In  Exit  controlled  loops  the  loop  condition  is  checked  after  the  loop 
switch: it can check single condition based on   integer  or  character  body  is  executed.  It  is  executed  at  least  once.    The    do‐while  loop  is  exit  
constant  i.e.  too  for  equality.  It  can  not  handle  float  values  in  the  controlled loop 
condition.  5. What is the output of the following code fragment? 
4. What is ʺFall throughʺ? what is the significance of default clause in a  for(int i=1;i<10;i++)
switch statement?  cout<<i;
Ans: Fall through: If a case misses break then all the case statements till       Ans: output : 1,2,3,4,5,6,7,8,9 
break  or  end  of  switch  are  executed,  this  is  called  fall  through.  The  6. What is the output of the following code fragment?   
  for(int i=1;i<10;i++);
default  case  statement  is  executed  in  the  case  if  none  of  the  case 
cout<<i;
statement is evaluated true.  Ans: output: 10 
   
5. List at least two situations where if proves better choice than switch.   7. Why does ʺHelloʺ not print even once? 
Ans: If statements are proved better than switch in the following    for(int i=1;i>10;i++)
situations: e.g.   cout<<"Hello";
(i) when we need to check condition for a range e.g salary >= 5000  Ans: : b.c. the loop conditions not satisfied even once. (i>10) 
&& salary<=2000   
(ii) when we need to set a condition which is based on multiple  8. What is the output of the following code? 
variables e.g.     for(r=0; r<4;r++)
    (salary >=5000 && salary<=20000)&& (age>=18&& age<=60)  {
for(c=0;c<10;c++)
 
cout<<"#";
Page 264  cout<<"\n";
}

2/18
Ans: output:   ####  Ans: : i=2; sum=0;
       ####  while(i<=20)
{ sum+=i; i=i+2; }
####  
 
    #### 
2. How many times is the following loop executed? 
9. Write a for loop that displays the numbers from 51 to 60 
int  s=0,i=0; 
Ans: for( i=51 ; i<=60 ; i++) cout<<i; 
do 
10. Which expressions are optional in a for loop? Suggest a situation where 
s+=i; 
an empty loop is useful. 
while(i<5); 
Ans: The initialization and updating are expressions are optional. The empty 
Ans: five times i.e. when i=0,1,2,3,4,
loop is helpful in the case of writing delay loops. 
Page 277 
              
1. Name the jump statements provided by C++. Compare break and 
Page: 268 
continue statements. 
1. An item in a for loop or while loop statement, can be accessed after the 
Ans:    The  jump  statements  provide  by  c++  are  goto,  break,  continue  and 
statement is over. True or False? 
return  and  a  function  exit().  The  break  causes  the  loop  or  switch 
Ans: False b.c. the item will have the local scope. 
statement to terminate  while  the  continue statement does  not  terminate 
2. What is meant by a variable’s scope? 
the loop rather the control is shifted to the next iteration. 
Ans: The area where the variable is accessible is called its scope. 
2. The goto statement causes control to goto (a) an operator (b) a label (c) a 
3. What is the difference between a while and a do‐while loop? 
variable (d) a function. 
Ans:  While  is  entry  controlled  and  do‐while  is  exit  controlled.  The  Do‐while 
Ans: : (b) a label 
loop  will  execute  at  least  once.  Where  as  while  will  depend  on  the  test 
3. The break statement causes a exit (a) only from the innermost loop (b) 
expression. 
only from the innermost loop (c) from all loops and switched (c) from all 
4. Write a while loop that displays numbers 2,4,6,8,.........18,20. 
i=2; loops and switches (d) from the innermost loop and switch. 
while(i<=20) Ans:  (d)from inner most loop or switch. 
{ 4. The exit() function breaks out of. (a) the function it appears in (b) the 
cout<<i<<”,”;
i=i+2; loop it appears in (c) the block it appears in (d) from the program it 
} appears in. 
  Ans: (d) the program it appears in. 
5. Write a do‐while loop that displays numbers 2,4,6,8,.... 18,20.  5. Which header file must be included in the program for using the exit() 
i=2; function? 
do
Ans: The header file is process.h for exit(). 
{ cout<<i<<”,”;
i=i+2;  
}while(i<=20); Short Answer Questions
  Q.1 What is the problem of dangling else? When does it arise? What is the default
Page: 270  dangling else matching and how it can be overridden?
Ans1: Dangling else problem: Nested if-else statements introduces source of potential
1. Write an equivalent while loop for the following for loop: 
ambiguity referred to as dangling else problem, when in a nested if statement, number of
for(int i=2, sum=0; i<=20; i=i+2)  ifs is more than the number of else clauses. The question arises, with which if does the
Sum+=i;  additional else clause property match up? E.g.

3/18
if(ch>=’A’)  cout<”Upper case letter”;
  if(ch<=’z’)  ++ upcase;  else if (ch>=97 &&ch<=122)
cout<<”Lower case letter”;
else ++others; // this else statement is with which if? 
}while(ch!=’&’);
Solution to the problem: Ensure proper use of parenthesis or Discuss the advantages of version2 over version1
The default a dangling else statement goes with the preceding unmatched if statement. Ans 4: The version II is better than version I because it will not check all the conditions
Q.2 Compare an if and a? : operator. every time. E.g. if the first condition becomes true the rest are not checked while all the
Ans2: ? : as compared to if- else is more concise , clean and compact code, but it is less conditions are checked in version I .
obvious as compared to if. Q.5 Write the syntex and purpose of a switch statement.
Only single value can be assigned to the expression of ? or : while we can assign Ans 5: Switch : It is a multiple branch selection statement. This selection statement
multiple expressions as a result of if-else.
successively tests the value of an expression against a list of integer or character. Syntax 
In the nested form of ? : becomes very complex.
Q.3 Given the following code fragment: of switch : switch(expression) 
if(a==0) {case constant 1: statement sequence 1; break;   
cout<<”Zero”; case constant 2: statement sequence 2; break; 
if(a==a) case constant 3: statement sequence 3; break; 
cout<<”One”;
  : 
if(a==2)
cout<<”Two”; case constant n‐1: statement sequence n‐1; break; 
if(a==3) [default: statement sequence n]; 
cout<<”Three”; } 
write an alternative code (using if) that saves on number of comparisons. Q.6  Discuss  when  does  an  if  statement  prove  more  advantageous  over  a 
Ans3:The alternating code for the given problem is: switch statement? 
if(a==0) cout<<”Zero”;
else if(a==1) cout<<”One”; Ans 6: The if is a better choice then switch in the following situations:  
else if(a==3) cout<<”Two”; 1. multiple conditions are to be checked.  
else if(a==3) cout<<”Three”; 2.conditions are to be checked for other then equality too. 
Q.7 is repetition of Q.6  
Q.4 Given teo code fragments as given below:  
//version1
Q.8 Rewrite the code given in question 3 using switch.  
do
{ cin>>ch; Ans 8: Rewrite the code of q3 is as follows. 
if(ch>=48 &&ch<=57) Using switch:
cout<<”Digit”; switch(a)
if(ch>=65&&ch<=90) {
cout<”Upper case letter”; case 0: cout<<”Zero”; break;
if (ch>=97 &&ch<=122) case 1: cout<<”One”; break;
cout<<”Lower case letter”; case 2: cout<<”Two”; break;
}while(ch!=’&’); case 3: cout<<”Three”; break;
// version2 }
do Q.9 Rewrite the following code fragments using switch:
{ cin>>ch; (a)
if(ch>=48 &&ch<=57) if(ch==‘E’) eastern++;
cout<<”Digit”; else if(ch==‘W’) western++;
else if(ch>=65&&ch<=90) else if(ch==’N’) northern++;

4/18
else if(ch==‘S’) southern++; case 5:calval=inputnum*25-20;
else default: unknown++; cout<<inputnum+calval; break;
} case 10:calval=inputnum*25-20;
Ans: 9(a) cout<<calval-inputnum; break;
switch(ch) }
{ Q. 10 What will be the output of following code fragment when the input is(a) ‘A’
case ‘E’: eastern++; break; (b) ‘C’ (c) ‘D’ (d) ‘F’
case ‘W’: western++; break; cin>>ch;
case ‘N’: northern++; break; switch(ch);
case ‘S’: southern++; break; {
default: unknown++; break; case ‘A’: cout<<”Grade A\n”;
} case ‘B’: cout<<”Grade B\n”;
(b) char code; case ‘C’: cout<<”Grade C\n”; break;
cin>>code; case ‘D’: cout<<”Grade D\n”;
if(code==‘A’) cout<<”Accountant”; default : cout<<Grade F\n”;
else if(code==‘C’)||else if(code==‘D’) }
cout<<:Grade IV”; Ans 10: Input Output
else if(code==’F’) (a) ‘A’ Grade A
cout<<”Financial Advisor”; Grade B
Ans 9(b) Grade C
char code; cin>>code; (b) ‘C’ Grade C
switch(code) (c) ‘D’ Grade D
{ Grade F
case ‘A’: cout<<”Accountant”; break; (d) ‘F’ Grade F
case ‘C’: Q. 11:
case ‘D’: cout<<:Grade IV”; break; (a) if(!3){ cout<<”Tricky\n”;}
case ‘F’: cout<<”Financial Advisor”; cout<<”Yes”;
break; Ans: (a) Yes
} (b) if(3)
(c) int inputnum,calval; cout<<”Tricky again”;
cin>>inputnum; else
if(inputnum==5) cout<<”Am I Right?”;
{ cout<<” No???”;
calval=inputnum*25-20; Ans: (b) Tricky again No???
cout<<inputnum+calval;
(c) if(0)
}
cout<<”Third time tricky”;
else if(inputnum==10)
cout<<Am I right?”;
{
Ans: (c) Am I right?
calval=inputnum*25-20;
cout<<calval-inputnum; (d) if(!0)
cout<<”Fouth time again\n”:
}
Ans 9(c) int inputnum,calval; cout<<”No??”;
cin>>inputnum;
switch(inputnum) Ans: (d) Fourth time again No??
{ (e) if(0)

5/18
cout<<”Plz! Not aain\n”; Q.14 Identify the possible error(s) in the following code fragment. Discuss the
else reason(s) of error(s) of error(s) and correct the code:
cout<<”Promise,this is the last time”; :
cout<<”Thenk God!!”; f=1;
Ans: (e) Promise, this is the last time Thank God!!! for(int a=40; (a); a--)
f*=a;
:
Q.12(a) int x=3, y=6; S=0;
if (x>=3) for(int a=1; a<40/a++)
cout<<”Zip Drive”<<endl; Ans 14:for(int a=1;a<40/a++) --> the up dating expression is missing in the
if ((x<=3) && (y>6)) loop(second ;)
cout<<Scanner “<<endl;
if (( x==3 )&&(y>=6)) Q.15 Identify the possible error(s) in the following code fragments. Discuss the
cout<<”TFT LCD Screen”<<endl; reason(s) of error(s) and correct the code:
if ((x<=3) || (y>6)) (a) cin>>i>>j;
cout<<”CD Burner”<<endl; While(i<j)
Ans 12: (a) Zip Drive cout<<i*j;
TFT LCD screen i++;
CD Burner Ans(a) : The update expression is out side the loop body. The corrected code will be
(b) int x=3, y=6; cin>>i>>j;
if (x>=3) while(i<j)
cout<<”Zip Drive”<<endl; { cout<<i*j;
else if ((x<=3) && (y>6)) i++;
cout<<Scanner “<<endl; }
else if (( x==3 )&&(y>=6)) (b) cin>>i>>j;
cout<<”TFT LCD Screen”<<endl; While(i<j);
else if ((x<=3) || (y>6)) {
cout<<”CD Burner”<<endl; cout<<i*j;
(b) Zip Drive i++;
}
Q.13 Briefly explain the working of a for loop along with its syntax. Give an Ans: (b) The loop body will not work b.c. while(i<j);
example of for loop to support your answer. The corrected code will be:
Ans 13: The for loop is the easiest to understand of the C++ loops. All its loop control cin>>i>>j;
elements are gathered in one place(on the top of the loop), while(i<j)
The general form of the for loop statement is : {
for(initialization expressions(s); test expression; update cout<<i*j;
expression(s)) i++;
{ }
body of the loop Q. 16 What is wrong with following code fragments?
} (a) for( int x=0; x>0; x--)
Example: for(i=1;i<=100;i++) cout<<x;
cout<<<<’\n’<<i; Ans 16: (a) loop condition is not satisfied even once. X>0
will print numbers 1 to 100 (b) int n=7;
do{

6/18
cout<<”Check this !!”; while(i<51)
n-=2; {
} while(n!=2); cout<<i;
Ans (b) The condition while(n!=2); will never be satisfied. i+=2;
(c) int p=8; }
do
{ cout<<”In the loop”;
p*=2;
} while(p%2==0);

Ans: (c) It will be an infinite loop b.c. the while(p%2==0) will always be true for the
given example.
(d) int i=9;
While(((i<10)&& (i>24))
cout<<”Here I am- in the loop”; Q.19
i--; Ans 19: i=100;
Ans: (d) The updating expression is not part of loop body. i.e. i--; is out of loop. do {
(e) for(int k=2, k<=12,k++) cout<<i--;
Cout<<k*k*k<<endl; }while(i);
Ans: (e) ; should be used in for loop. cout<<”\n Thank you”;
Q.20
The corrected statement will be
for(int k=2;k<=12;k++)
(f) num=4;
While(num<10)
{ eggs=num*12;
cout<<eggs:
}
Ans: (f) The updating expression is missing so it will become an end less loop.
Q.17 Compare and discuss the suitability of three loops in different situations.
Ans 17: For loop is appropriate when you know in advance how many times the loop
will be executed. The other two loops (while and do while ) are suitable in the in the
situations where it is not known in advance when the loop will be terminated. The while
loop should be preferred when you may not want to execute the loop body even once if
the condition is false. And the do-while loop is to be preferred when you want loop
body to be executed at least once.
Q.18 Given the following code fragment:
i=2; Ans 20: :
Start: { int a,b;
cout<<i; goto end;
i+=2; char ch;
if(i<51) goto start; cin>>ch;
cout<<”\n Thank you”; if(ch!=’\n’)
Rewrite the above code suing a while loop. cout<<”Not a Newline “;
Ans 18: i=2; end:
7/18
; }// label can’t be the last statement in the block so we can add .

Q.21

Ans 22: “;” is used with the loop i.e.


for(n1=1; n2<=10; ++n1);

Q.23

Q.21
(a) (b) (c) (d)
00 12345 10 1 102 10 3 ABCDEF
01 12345 9 1 92 93 ABCDE
02 12345 8 1 82 83 ABCD Ans 23:
03 7 1 72 73 ABC int Lavalue=9;
1 0 6 1 62 63 AB while(Lvalue)
11 A {
12 cout<<Lvalue;
13 cout<<Lvalue+1<<endl<<Lvalue-1<<endl;
Q.22 Lvalue-=3;
cout<<endl;
}
Q.24

8/18
Ans25: Output=26
Note : All the programs should include the following header files (Include extra
header files if required)
#include<cono.h>

Q.26 Write a program to input a digit and print it in words.


Ans 26:program to input a digit and print it in digits
void main()
{
int num;
clrscr();
cout<<"Enter the number :";cin>>num;
cout<<"\nThe number in word is : ";
switch(num)
{
case 0: cout<<"Zero";break;
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;
Ans 24: //version 1
default: cout<<"Wrong Choice..";
3
}
// version 2
getch();
3 }
Q.25
Q. 27 Write a short program to check whether square root of a number is prime or
not?
//Q27 To check square root of number is prime or not
#include<math.h>
void main()
{ float num,sqr;
int num1,i,flag=0; clrscr();
cout<<"Enter the number to check :";
cin>>num;
sqr=sqrt(num);
num1=(int)sqr;
if(sqr==num1) // a no. to be prime if it is an integer only
{
for(i=2;i<=num1/2;i++)

9/18
{ peri=2*(side1+side2);
if(num1%i==0) area=side1*side2;
{ cout<<"\nArea="<<area<<" unit square\t
flag=1; "<<"Perimeter="<<peri<<" units"<<endl;
break; if(area>peri)
} cout<<"\nArea is greater than perimeter";
} else
if(flag==1) cout<<"\nPerimeter is greater than area";
cout<<"Not a prime "; getch();
else }
cout<<"It is a prime ";
} Q30 Write a short program to print the following series:
getch(); (i) 1 4 7 10.......40
} Ans: //Program To print the following series:
(i) 1 4 7 10.......40
Q.28 Write a short program to print first n odd numbers in descending order. void main()
Ans: //pogramTo print first n odd numbers in descending order { int start,end;
#include<math.h> clrscr();
void main() cout<<"Enter the first value : "; cin>>start;
{ cout<<"Enter the final value : ";cin>>end;
int n,i; cout<<"The required series is :\n";
clrscr(); for(int i=start;i<=end;i+=3)
cout<<"Enter the how many odd numbers you want :"; cout<<i<<" ";
cin>>n;
cout<<"The first "<<n<<" odd numbers in descending order getch();
are :\n"; }
for(i=2*n-1;i>=1;i=i-2) (b) Program to print the following series:
{ (i) 1 -4 7 -10.......-40
cout<<i<<","; void main()
} {
getch(); int start,end;
} int sign=-1;
clrscr();
Q.29 Given four sides of a rectangle. Write a program to find out whether area is cout<<"Enter the first value : "; cin>>start;
greater than its perimeter cout<<"Enter the final value : ";cin>>end;
Ans: //program to Given four sides of a rectangle. Write a program to find out cout<<"The required series is :\n";
whether area is greater than its perimeter for(int i=start;i<=end;i+=3)
Ans: void main() {
{ sign*=-1;
float side1,side2,peri,area; clrscr(); cout<<sign*i<<" ";
cout<<"Enter the length of the rect. :"; }
cin>>side1;
cout<<"Enter the height of the rect. :"; getch();
cin>>side2; }

10/18
Q.31 Write a program to find a character is a digit or a simp_int=(P*T*R)/100;
letter: cout<<"The Calculated Simple Interest is :"<<simp_int;
Ans: getch();
void main() }
{ char ch;
clrscr(); Q34 Write a short program to find average of list of numbers entered from
cout<<"Enter the character to check : "; keyboard
cin>>ch; void main( )
if((ch>='A'&& ch<='Z')||(ch>='a'&&ch<='z')) {
cout<<"It is an alphabet"; int n=0,sum=0,num=1;
else if(ch>='0'&& ch<='9') float avg;
cout<<"It is a digit "; clrscr();
getch(); while(1)
} {
cout<<"Enter the number(0 to terminate) :";
Q31program to find a character is a digit or a letter: cin>>num;
Ans: void main() if (num!=0)
{ {
char ch; sum+=num;
clrscr(); n++;
cout<<"Enter the character to check : "; }
cin>>ch; else
if(ch>='A' && ch<='Z') break;
{ }
ch+=32; avg=(float)sum/n;
cout<<" It is an upper case alphabet."<<endl; cout<<"The average of "<<n <<" numbers entered from
cout<<" The corresp. small case alphabet is:"<< ch; keyboard is= "<<avg;
} getch();
else if(ch>='a'&& ch<='z')
{ ch-=32; }
cout<<"It is an lower case alphabet."<< endl;
cout<<"The corresp. small case alphabet is:“<< ch; Q35 Write a short program to
} (i) calculate area of triangle,
else (ii) radius of circle whose area is given
cout<<"Not an alphabet.."; (iii) calculate the volume of sphere.
getch(); Ans: const float pi=3.14159;
} void main( )
Q33 Write a short program to calculate simple interest. { int ch; float b,h,area,vol,rad;
Ans: void main() do
{ clrscr(); {
float P,T,R,simp_int; clrscr();
cout<<"Enter the Principal :";cin>>P; cout<<"---AREA/VOLUME MENU-----\n"<<endl;
cout<<"Enter the Rate:";cin>>R; cout<<"1. Area of Triangle"<<endl
cout<<"Enter the Time :";cin>>T; <<"2. Radius of Circle"<<endl

11/18
<<"3. Volume and Area of sphere"<<endl }
<<"4 Exit "<<endl; cout<<"\nThe largest number of the list is ="<<largest;
cout<<"\n Enter your choice 1-4 "; cin>>ch; getch();
switch(ch) }
{ Q. 37 Write programs using nested loops to produce the following designs:
case 1:cout<<"Enter the base & height :"; (a) A
cin>>b>>h; A B
area=0.5*b*h; A B C
cout<<"The area="<<area; break; A B C D
case 2: cout<<"Enter the area circle :"; A B C D E
cin>>area; A B C D E F
rad=sqrt(area/pi); void main()
cout<<"The radius of a circle is ="<< rad; {
break; int rows,i,j;
case 3: cout<<"Enter the radius of the char ch;
sphere :"; cin >>rad; clrscr();
vol=4/3.0*pi*rad*rad*rad; cout<<"How many rows you want?";
area=4*pi*rad*rad; cin>>rows;
cout<<"Volume :"<<vol<<” Area :"<<area<<endl; for(i=1;i<=rows;i++)
break; {
case 4: break; ch='A';
default: cout<<"Pl. enter 1-4 only"; for(j=1;j<=i;j++)
} {
cout<<"Press any key to continue..."; cout<<ch++<<" ";
getch(); }
}while(ch!=4); cout<<endl;
getch(); }
} getch();
}
Q36 Write a short program to find largest To find largest no. of a list of nos entered (b)
through kbd &&&&&&&
Ans: & &&&&
#include<limits.h> && &
void main() &
{ void main()
int n,num,I; {
int largest=INT_MIN;//int_min is the smallest integer int rows,i,j,k;
clrscr(); char ch;
cout<<"How many numbers you want to enter :"; cin>>n; clrscr();
for(i=0;i<n;i++) cout<<"How many rows you want?";
{ cin>>rows;
cout<<"\nEnter the number:"; cin>>num; for(i=1;i<=rows;i++)
if(num>largest) {
largest=num; ch='&';

12/18
for(j=1;j<i;j++)//loop for spaces for(j=1;j<=2*n-1;j++)// for the last row
{ cout<<"& ";
cout<<" "; }
} cout<<endl;
for(k=2*rows-(2*i-1);k>=1;k--) //loop for printing '&' }
{
cout<<ch<<" "; getch();
} }
cout<<endl; Long Answer Questions
} Q1 Given three numbers A,B and C, write a program to write their values in an
getch(); ascending order. For example, if A=12, B=10,and C=15, your program should print
} out:
Smallest number = 10
(c) program to print the following figure Next higher number = 12
/* & spaces1= 5 space2=- row=1 Highest number = 15
&& 4 1 2 Ans:
& & 3 3 3
void main()
& & 2 5 4
& & 1 7 5 {
&&&&&&&&&&& 0 - 6 int A,B,C; int m1,m2,m3;
*/ clrscr();
void main() cout<<"Enter the First Number :"; cin>>A;
{ cout<<"Enter the Second Number :"; cin>>B;
int i,j,k,n; cout<<"Enter the Third Number :"; cin>>C;
clrscr(); if(A>B)
cout<<"Enter the number of rows:";cin>>n; {
for(i=1;i<=n;i++) if(A>C)
{ {
if(i==1) // for the first row m1=A;
{ if(B>C)
for(j=1;j<n;j++) {
cout<<" "; m2=B;
cout<<"& "; m3=C;
} }
else if(i>=2&&i<n) // for middle rows else
{ {
for(j=1;j<=n-i;j++) m2=C;
cout<<" "; m3=B;
cout<<"& "; }
for(k=1;k<=2*(i-1)-1;k++) }
cout<<" "; else
cout<<"& "; {
} m1=C;
else if(A>B)
{ {

13/18
m2=A; Q2 A bank accepts fixed deposits for one year or more and the policy it accepts on
m3=B; interest as follows:
} (i) If a deposit is less than Rs. 2000 and for 2 or more years, the interest rate is 5
else percent compound annually.
{ (ii) If a deposit is Rs. 2000 or more but less than 6000 and for 2 or more years, the
m2=B; interest rate is 7% compounded annually.
m3=A; (iii) If the deposit is more than Rs. 6000 and is for a year or more, the interest is 8%
} compounded annually.
} (iv) On all deposits for 5 years or more, interest is 10% compounded annually.
} (v) On all other deposits not covered by above conditions, the interest is 3%
else if(B>C) compounded annually.
{ Given amount deposited and the number of years, write a program to calculate the
m1=B; money in the customer’s account at the end of the specified time.
if(A>C) #include<math.h>
{ void main()
m2=A; {
m3=C; float deposit,rate,time,amt;
} clrscr();
else cout<<"Enter the amount to deposit:"; cin>>deposit;
{ cout<<"Enter the time:"; cin>>time;
m2=C; if(deposit<2000 && time>=2)
m3=A; rate=5;
} else if(deposit>=2000 && deposit<6000 && time>=2)
} rate=7;
else else if(deposit>6000 && time>=1)
{ rate=8;
m1=C; else if(time>=5)
if(A>B) rate=10;
{ else
m2=A; rate=3;
m3=B; amt=deposit*pow((1+rate/100),time);
} cout<<"\n The amount to be deposit in the account of the
else customer is Rs"<<amt;
{ getch();
m2=B; }
m3=A;
} Q.3 Write a complete C++ program to do the following:
} (i) read an integer X
cout<<"\nThe smallest Number="<<m3; (ii) form an integer Y by reversing the digits of X and integer S having sum of digits
cout<<"\nNext higher Number="<<m2; of X
cout<<"\nHighest Number="<<m1; (iii) Output Y and S.
getch(); #include<math.h>
} void main()
{

14/18
long num,num1=0,temp; {
int sum=0,r; divisor=A;
clrscr(); dividend=B;
cout<<"Enter the number : "; cin>>num; }
temp=num; while(divisor)
while(num) {
{ HCF=divisor;
r=num%10; R=dividend % divisor;
sum+=r; dividend/=divisor;
num1=num1*10+r; divisor=R;
num/=10; }
} cout<<"\nThe LCM of "<<A<<" and "<<B<<" is ="<<LCM;
cout<<"\nNumber you entered is :"<<temp; cout<<"\n\nThe HCF of "<<A<<" and "<<B<<" is ="<<HCF ;
cout<<"\nNumber formed by reversing the no. is :" << num1; getch();
cout<<"\nSum of digits of the no. is :"<<sum; }
getch(); Q.5 Write a program to print the truth Table for XY+Z.
} #include<math.h>
void main()
Q.4 Write a program to find the LCM and GCD of two numbers. {
#include<math.h> int X,Y,Z,XY,XY_Z;
void main() clrscr();
{ cout<<"\n====\tTruth Table of XY+Z \t====\n";
int A,B,LCM,HCF; cout<<"\nX\t\tY\t\tZ\t\tXY\t\tXY+Z\n";
int small, big, divisor, dividend, R, Q; cout<<"\n============================\n";
clrscr(); for(X=0;X<2;X++)
cout<<"Enter the first number : "; cin>>A; {
cout<<"Enter the second number: "; cin>>B; for(Y=0;Y<2;Y++)
// to find LCM {
big=(A>B)?A:B; for(Z=0;Z<2;Z++)
for(int i=big;i<=A*B;i++) {
{ XY=(X==1&&Y==1)?1:0;
if(i%A==0 && i%B==0) XY_Z=(XY+Z==1)?1:0;
{ cout<<"\n"<<X<<"\t\t"<<Y<<"\t\t"<<Z<<"\t\t"<<XY<<"\t\t"<<XY_Z<<endl;
LCM=i; break; }
} }
} }
// to find HCF cout<<"\n==============================";
HCF=1; getch();
R=1; }
if(A>B) Q.6 Write a C++ program to Fibonacci series (up to 10 terms).
{ Ans:
divisor=B; void main()
dividend=A; {
} int first=0,second=1,third;
else
15/18
int n; {
clrscr(); long int num,R,newnum,temp;
cout<<"\n Enter the no of terms :";cin>>n; int count=0;
cout<<"\n The Fibonacci series up to 10 terms is :"; clrscr();
cout<<first<<", "<<second<<", "; cout<<"\nEnter the number :";cin>>num;
for(int i=3;i<=n;i++) temp=num;
{ while(num)
third=first+second; {
first=second; R=num%10;
second=third; num/=10;count++;
cout<<third<<", "; }
} cout<<"\nYou entered the number :"<<temp<<endl;
getch(); cout<<"\nThe most significant digit is :"<<R<< endl;
} cout<<"\nThe sum of digits is :"<<count<<endl;
newnum=count*10+R;
Q.7 Given a list of integers, write a program to find those which are palindromes. cout<<"\nThe number formed by most significient digit "<<" and no. of
For e.g. the number 4321234 is a palindrome as it reads the same from left to right digits is :"<<newnum;
and right to left. getch();
void main() }
{
long int num,num1=0,temp; Q.9. Write a C++ program print every integer between 1 and n divisible by m. Also
clrscr(); report whether the number that is divisible by m is even or odd.
cout<<"\n Enter the number :";cin>>num; Ans:
temp=num; void main()
while(num) {
{ int limit2,num;
num1=num1*10+num%10; clrscr();
num/=10; cout<<"\nEnter the number to check with :"; cin>>num;
} cout<<"\nEnter the limit :";
if(num1==temp) cin>>limit2;
cout<<endl<<temp <<" is a palimdrome \n"; cout<<"\n\nThe numbers between 1 and "<<limit2
else <<" divisible by "<<num<<" are :"<<endl;
cout<<endl<<temp<<" not palimdrome \n"; for(int i=1;i<=limit2;i++)
getch(); {
} if(i%num==0)
{
Q.8 Write a complete C++ program to do the following: if(i%2==0)
(i) Read an integer X. cout<<"\t"<<i<<"\t-\tEven\n";
(ii) Determine the number of digits in X. else
(iii) Form an integer Y that has the number of digits n at ten’s place and the most cout<<"\t"<<i<<"\t-\tOdd\n";
significant digit of X at one’s place. }
(iv) output Y }
Ans: getch();
void main() }

16/18
Q10. Write C++ program to sum the given sequence : for(int j=2;j<=i;j++)
(a) 2/9 - 5/13 + 8/17 ...... {
void main() fact*=j;
{ }
int sign=-1,terms,count; sum+=(double)1/fact;
float n,d; double sum=0; }
clrscr();
cout<<"\n Enter the number of terms :"; cin>>terms; cout<<"\nThe sum of series up to "<<n<<" terms is ="<<sum;
for(n=2,d=9,count=1;count<=terms; n+=2,d+=4, count++) getch();
{ }
sign*=-1;
sum+=sign*n/d; Q12 Write a program to accept the age of n employees and count the number of
} persons in the following age group:
cout<<"\n The sum up to "<<terms<< " terms is = "<<sum; (i) 26-35
getch(); (ii) 36-45
} (iii) 46-55
(b) program to sum of series 12 + 32 + 52 + ....+n2 void main()
#include<math.h> {
void main() int c1=0,c2=0,c3=0,n,i,age;
{ clrscr();
int n,i; cout<<"\n Enter the no. of employees ?:";cin>>n;
double sum=0; for(i=1;i<=n;i++)
clrscr(); {
cout<<"\n Enter the value series up to ?:"; cin>>n; cout<<"Enter the age :";cin>>age;
for(i=1;i<=n;i+=2) if(age>=26&& age<=35) c1++;
{ else if(age>=36&& age<=45) c2++;
sum+=pow(i,2); else if(age>=46&& age<=55) c3++;
} }
cout<<"\nThe sum up to "<<n<<" is ="<< sum; cout<<"\nNo.of emp in age group 26-35 is =" <<c1;
getch(); cout<<"\nNo.of emp in age group 36-45 is =" <<c2;
} cout<<"\nNo.of emp in age group 46-55 is =" <<c3;
getch();
Q.11 Write a C++ program to sum the sequence }
1+1/1! + 2/2! + 3/3! + ....
void main() Q.13 Wrrite programs to dind the sum of the following series:
{ (a) To sum of series x-x2/2! + x3/3! - x4/4! + x5/5!-x6/6!
int n,i; #include<math.h>
unsigned long fact; void main()
double sum=1; {
clrscr(); int i,x,sign=-1;
cout<<"\n Enter the no. of terms you want?:";cin>>n; unsigned long fact;
for(i=1;i<=n;i++) double sum=0;
{ clrscr();
fact=1; cout<<"\n Enter the value of X :";cin>>x;

17/18
for(i=1;i<=6;i++) sum+=pow(x,i);
{ }
sign*=-1;
fact=1; cout<<"\nThe sum of series up to "<<n<<" terms is ="<<sum;
for(int j=2;j<=i;j++) getch();
{ }
fact*=j;
}
sum+=sign*pow(x,i)/fact;
}
cout<<"\nThe sum of series up to "<<6<<" terms is ="<<sum;
getch();
}

13(b) To sum of series x+x2/2 + x3/3 + x4/4 ....xn/n!


#include<math.h>
void main()
{
int i,x,n;
unsigned long fact;
double sum=0;
clrscr();
cout<<"\n Enter the value of X :"; cin>>x;
cout<<"\n Enter the no of terms :";cin>>n;
for(i=1;i<=n;i++)
{
sum+=pow(x,i)/i;
}
cout<<"\nThe sum of series up to "<<n<<" terms is ="<<sum;
getch();
}

Q.14 Write a program to find the sum of the series


S=1+x+x`+ x3+ x4 ....xn
#include<math.h>
void main()
{
int i,x,n;
double sum=0;
clrscr();
cout<<"\n Enter the value of X :";cin>>x;
cout<<"\n Enter the no of terms :";cin>>n;
for(i=0;i<n;i++)
{

18/18

Anda mungkin juga menyukai