You are on page 1of 20

1)

Write a single C++ statement to accomplish each of the


following:
a) Declare the variables c, q76354 and number to be of type int.
b) Read an inteer from the user at the !eyboard and store the
value entered in inteer variable ae.
c) "rint the messae #$his is a %&& proram# on one line.
d) "rint the messae #$his is a %&& proram# on t'o lines, in
'hich the first line ends 'ith %&&.
e) "rint the messae #$his is a %&& proram# 'ith each 'ord of
the messae on a separate line(
f) "rint the messae #$his is a %&& proram# 'ith each 'ord
separated from the ne)t by a tab.
Answer
a)int c, q76354, number;
b)cin>>age;
c)cout<<" This is a !! "rogram "<<en#$;
#)cout<<" This is a !! %n"rogram%n";
e)cout<<" This%nis%na%n!!%n"rogram%n";
&)cout<<" This%tis%na%t!!%t"rogram%t";
'
(tate whether each o& the &o$$owing is true or &a$se
T
* +.
%omments cause the computer to print the te)t
after ,, on the screen 'hen the proram is
e)ecuted.
T * -.
.ll variables must be declared before they are
used.
T
* 3.
.ll variables must be iven a type 'hen they
are declared.
$
)
4.
$he arithmetic operators /, ,, 0, & and 1 all
have the same level of precedence.
T
* 5.
Declarations can appear almost any'here in the
body of %&& function
$
)
6.
%&& considers the variables number and
*u+b,r to be identical.
3)
2tate the order of evaluation of the operators in the follo'in %&
& statements and sho' the value of
)
after each statement is
performed.
C++ statement value of
)
i) + - , 6 / 3 7 ) + = 34+5
ii) - , 4 , 64 ) = 345
iii)
) - , 4 ,6 64 ) =
343-
iv)
)))) 3 ,6 3 / 7 6 3 6 / 7 / 3 6 ) + =
343-4
4)
*ind the %&& e)pression for the follo'in mathematical e)pressions
Math C++
5
e d c b a
)
+ + + +
=
34 6a&b&c&d&e),5(
cd b a )
4
=
34po'6a,4)/b8sqrt6c/d)(
c ab ) =
34sqrt6a/b8c)(
) b a ln6 )
-
+ =
34lo6a/a&b)(.
5)
9rite a proram that as!s the user to enter t'o numbers,
obtain t'o numbers from the user and prints the sum,
product, difference, quotient and reminder of the t'o
numbers.

Answer
-inc$u#e<iostream.h>
/oi# main0)
1
int a b;
cin>>a>>b;
cout<<"sum 2"<<a!b<<en#$;
cout<<""ro#uct 2"<<a3b<<en#$;
cout<<"#i&&erence 2"<<a4b<<en#$;
cout<<"quotient 2"<<a5b<<en#$;
cout<<" remin#er 2"<<a6b<<en#$;
7
6)
9rite a proram that as!s the user to enter t'o numbers,
obtain t'o numbers from the user, then prints the larer
number follo'ed by the 'ords #is larer#. :f the numbers
are equal, print the messae #$hese numbers are equal#.
Answer
-inc$u#e<iostream.h>
/oi# main0)
1
int 8, 9;
cout<<"enter 82 "; cin>>8; cout<<en#$;
cout<<"enter 92 "; cin>>9; cout<<en#$;
i&08>9) cout<<"8 is $arger"<<en#$;
i&09>8) cout<<"9 is $arger"<<en#$;
i&08229) cout<<"these numbers are equa$"<<en#$;
7
7)
9rite a proram that reads in the radius of a circle
and prints the circle;s diameter, circumference and
area. <se the constant value 3.+4+57 for

. Do these
calculations in output statements.
Answer
-inc$u#e<iostream.h>
/oi# main0)
1
onst &$oat :i23.1415;;
int r;
cout<<"enter ra#ius 2 ";
cin>>r;
cout<<"%n#iameter 2 "<<' 3 r <<en#$;
cout<<" circum&erence 2"<<' 3 "i 3 r <<en#$;
cout<<"area 2"<< "i 3 r 3 r <<en#$;
7
<)
9hat is output of the follo'in proram=8
- inc$u#e<iostream.h>
/oi# main0)
1
int i,=;
&or0i21;i<26;i!!)
1
&or0=2>;=<2i;=!!)
cout<<"3";
cout<<en#$;
7
7
Ans
/
/ /
/ / /
/ / / /
/ / / / /
/ / / / / /
1>)
9rite the %&& proram to find the sum and averae of a
set of n inteer numbers.
Answer
-inc$u#e<iostream.h>
/oi# main0)
1
int n, i, 8 sum2>, a/erage;
cout<<"enter n 2 ";
cin>>n;
&or0i2>;i<2n;i!!)
1
cout<<"enter 82 ";
cin>>8;
sum !2 8;
7
a/erage 2 sum5n;
cout<<"sum 2 "<<sum<<en#$;
cout<<"a/erage 2"<<a/erage<<en#$;
;)
9rite the %&& proram to find the sum of odd inteers
bet'een + and 77 usin a for structure.
Answer
-inc$u#e<iostream.h>
/oi# main0)
1
int sum2>;
&or0i2>;i<2;;;i2i!')
sum !2i;
cout<<" sum o& o## integers between 1 an# ;; 2"<<sum;
7
11)
9rite the %&& proram to find the factorial of any inteer
number n.
Answer
-inc$u#e<iostream.h>
/oi# main0)
1
int i, n, &act21;
cout<<"enter n 2 ";
cin>>n;
&or0i21;i<2n;i!!)
&act 32i;
cout<<"the &act o& "<<n<<" 2"<<&act;
7
1')
9rite the %&& proram to find the square and square root
of +> inteer numbers+8+> usin 'hile loop.
Answer
-inc$u#e<iostream.h>
-inc$u#e<math.h>
-inc$u#e<iomani".h>
/oi# main0)
1
int i;
cout<<" i i3i
sqrt0i)"<<en#$;
cout<<"444444444444444444444444444444444444444444444444444
44444"<<en#$;
i21;
whi$e0i<21>)
1
out<<setw04)<<i<<setw04)<<i3i<<setw04)<<sq
rt0i)<<en#$;
i!21;
7
7
13)
9hat does this proram print
-inc$u#e<iostream.h>
/oi# main0)
1
int 9, 821, tota$2>;
whi$e08<21>)
1
9 2 8 3 8;
cout<<9<<en#$;
tota$ !2 9;
!!8;
7
out<<"tota$ is "<<tota$<<en#$;
7
Answer

- - - -
+> 3 - + + + + +
14)
*ind the errors in each of the follo'in code sements
and e)plain ho' to correct them 6ans'er only eiht)

0a)
cout >>The result=<<area;

?o ,rror orrection
+ @@ AA
- #is not found #
0b)
if (d=1)
cout <<Sat"<<endl;
else;
cout <<"not Sat"<<endl;
0c)
for (int i=1;i<=100;i+)
Cout<<i<<andl;
0#)
#include <iostream>
oid maim ()

?o ,rror orrection
+ 4 44
- Blse( Blse
?o ,rror orrection
+ :& :&&
- .ndl Bndl
?o ,rror orrection
+ Aiostream@ Aiostream.h@
- Caim main
0e)
oid main();
!
cout<<""elcome to #a$or e%am
&"<<end;
'

0&)
int a?3@201',6,;);
&or0int i2>; i<3; i!!)
cout <<a;
0i)
#oub$e 8?4@2 11.1 , '.3 , 4.4 , 5.< ,6.;7;
cout <<a?1@<<" "<<a03);

?o ,rror orrection
+ Doid main6)( Doid main6)
- Bnd Bndl
?o ,rror orrection
+
int a?3@201',6,;); int a?3@211',6,;7;
- . aEiF
?o ,rror orrection
+
8?4@2 11.1 , '.3 , 4.4 , 5.< ,
6.;7;
8?5@2 11.1 , '.3 , 4.4 , 5.< ,6.;7;
-
a03) A?3@
15) Bhat is the out"ut o& the &o$$owing "rogramsC
0a)

3333333B,DE+,3333333
#2<
sum2'1
33333333TFA*G HEI33333333
0b) use an9 in"ut 9ou want
"$ease enter height23
:$ease enter wi#th25
The resu$t215
0c)
1
'
3
4
5
6
7
<
;
1>
0#)

the sum 255

6e)
Ginclude Aiostream.h@
void main 6 )
H
int )46(
cout AA # ) 4 # AA ) AA endl(
cout AA # ) 4 # AA &&) AA endl(
cout AA # ) 4 # AA ) AA endl(
cout AA # ) 4 # AA )&& AA endl(
cout AA # ) 4 # AA ) AA endl(
I
16)
Brite a "rogram to "rint +u$ti"$ication tab$e &rom 1
to 5 as &o$$owing out"utC
-inc$u#e<iostream.h>
Joi# main0)
1
Knt K,=,";
)or0i21;i<25;=!!)
1
)or0=21;=<25;=!!)
1
:2=3K;
346
347
347
347
345
out<<";
out<<"LLLLLLLLLLLLL";
7
out<<en#$;
7
7
17) Brite a "rogram to com"ute the o"erations
&or two int numbers 0sum, subtract,
mu$ti"$ication, #i/i#e), using switch statement as
&o$$owing out"utC
-inc$u#e<iostream.h>
Joi# main0)
1
Knt 8,9;
)$oat r;
har o";
out<<"enter 8 then 9 then an9
o"eration0!,4,3,5)"<<en#$;
in>>8>>9>>o";
(witch 0o")
1
aseM!MC r28!9; breaN;
aseM4MC r2849; breaN;
aseM3MC r2839; breaN;
aseM5MC r2859; breaN;
Oe&au$tC cout<<"ba# o"eration";
7
out<<"r2"<<r<<en#$;
7

1<)
Brite a !! "rogram to rea# arra9 a o& n rea$
e$ements an# &in# their sum an# a/erage.
-inc$u#e<iostream.h>
Joi# main0)
1
onst int n25;
Knt K,n,
)$oat a?n@,sum, a/erage;
(um2>;
)or0i2>;i<a;i!!)
1
in>>a?i@;
(um!2a?i@;
7
A/erage2sum5n;
out<<"sum2"<<sum<<en#$;
out<<"a/erage2"<<a/erage<<en#$;
7
1;)
Brite a !! "rogram to &in# the sma$$est e$ement o& an
arra9 o& n rea$ e$ements.
-inc$u#e<iostream.h>
Joi# main0)
1
onst int n25
Knt K;
)$oat a?n@,sma$$;
)or0i2>;i<n;i!!)
in>>a?i@;
(ma$$2a?>@;
)or0i21;i<n;i!!)
K&0a?i@<sma$$)
(ma$$2a?i@;
out<<"sma$$2"<<sma$$<<en#$;
7

You might also like