You are on page 1of 22

Program 1

Write a Program to calculate f(x), where x is a real number, as


f(x) = -1, if -5.0 > x
0, if -5.0 x 5.0
!1, if x > 5.0
" inclu#e iostream.h>
" inclu#e conio.h>
$oi# main()
%
signe# int x&
clrscr() &
cout'(nter the $alue of x for f(x) ) ' &
cin>>x &
if(x-5)
cout'*n''the result is -1'&
else if(x>-5 ++ x5)
cout'*n''the result is 0'&
else
cout'*n''the result is 1'&
getch() &
,
OUTPUT
(nter the $alue of x for f(x) ) 5
the result is 1
Program 2
a) Write a Program to fin# the sum of #igits for an n-#igit number (e.g. 1-./5, out0ut =
15)
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,a,remain#er,sum=0&
clrscr()&
cout'enter the number '&
cin>>num&
a=num&
while(num>0)
%
remain#er=num110&
num=num210&
sum=sum!remain#er&
,
cout'the sum of #igits of number 'a' is 'sum&
getch()&
,
OUTPUT
enter the number ./5
the sum of #igits of number ./5 is 1-
b) Write a Program to fin# the re$erse of an n-#igit number (e.g. 1-./5, out0ut = 5/.-1)
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,a,#igit,re$erse=0,i&
clrscr()&
cout'enter the number '&
cin>>num&
a=num&
i=1&
while(num>0)
%
#igit=num110&
num=num210&
re$erse=re$erse310!#igit&
i!! &
,
cout're$erse of 'a' is 're$erse&
getch()&
,
OUTPUT
enter the number /54
re$erse of /54 is 45/
c) Write a Program to fin# whether the gi$en number is a 0alin#rome or not (e.g. 1-1,
-./.-, 5445 is a 0alin#rome)
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,a,#igit,re$erse=0,i&
clrscr()&
cout'enter the number '&
cin>>num&
a=num&
i=1&
while(num>0)
%
#igit=num110&
num=num210&
re$erse=re$erse310!#igit&
i!! &
,
cout're$erse of 'a' is 're$erse&
if (a==re$erse)
cout'*n''the number is 0alin#rome'&
else
cout'*n''the number is not 0alin#rome'&
getch()&
,
OUTPUT
enter the number -..-
re$erse of -..- is -..-
the number is 0alin#rome
Program 3
Write a Program to fin# the smallest an# secon# smallest of gi$en n numbers (without
using arra5s)
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int no, sma1=1000, sma-=1001,num&
clrscr()&
cout'how man5 numbers 5ou want to enter *n'&
cin>>num&
cout'enter the numbers(limit 1000)) '&
for(int i=0& inum& i!!)
%
cin>>no&
if(nosma1)
%
sma-=sma1&
sma1=no&
,
else if(no>sma1 ++ nosma-)
%
sma-=no&
,
,
cout'smallest number is ' sma1 '*n'&
cout'secon# smallest number is 'sma- '*n'&
getch()&
,
OUTPUT
how man5 numbers 5ou want to enter
4
enter the numbers(limit 1000)) 56
/.
67
80
1-
./
smallest number is 1-
secon# smallest number is ./
Program 4
a) Write a Program to fin# that a gi$en number is 0ositi$e or negati$e
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num&
clrscr()&
cout'(nter the number)'&
cin>>num&
if(num>0)
cout'*n''the number 'num' is 0ositi$e'&
else
cout'*n''9he number 'num' is negati$e'&
getch()&
,
OUTPUT
(nter the number)-5
9he number -5 is negati$e
b) Write a Program to fin# whether a gi$en number is 0rime or not
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,:,remain#er&
clrscr()&
cout'(nter the number)'&
cin>>num&
:=-&
while(:=num-1)
%
remain#er=num1:&
if(remain#er==0)
%
cout'*n''number is not 0rime'&
brea;&
,
:!!&
,
if(num==:)
cout'*n''number is 0rime'&

getch()&
,
OUTPUT
(nter the number)7
number is 0rime
c) Write a Program to fin# the binar5 re0resentation of a gi$en number
" inclu#e iostream.h>
" inclu#e conio.h>
$oi# main()
%
int biarra5<-0=, n, remain#er, 0 , 5,>&
clrscr() &
cout'(nter a #ecimal number ' &
cin>>n &
>=n&
for(0=0&n>0&0!!)
%
remain#er = n 1 - &
biarra5<0= = remain#er &
n = n 2 - &
,
cout'*n''9he binar5 re0resentation of '>' is '&
for(5 = 0-1 & 5 >= 0 & 5--)
coutbiarra5<5= &
getch() &
,
OUTPUT
(nter a #ecimal number -/
9he binar5 re0resentation of -/ is 11000
#) Write a Program to fin# whether it is e$en or o## for a gi$en number
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,answer&
clrscr()&
cout'(nter the number)'&
cin>>num&
answer=num1-&
if(answer==0)
cout'*n''the number is e$en'&
else
cout'*n''the number is o##'&
getch()&
,
OUTPUT
(nter the number)5
the number is o##
e) Write a Program to fin# factorial of a gi$en number
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,>,ftl&
clrscr()&
cout'(nter the number)'&
cin>>num&
>=num&
ftl=1&
while(>>=1)
%
ftl=ftl3>&
>--&
,
cout'*n''the factorial of 'num' is 'ftl&
getch()&
,
OUTPUT
(nter the number)7
the factorial of 7 is 50/0
f) ?or a gi$en number (n) fin# the n
th
?ibonacci number
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,g,u=0,$=1,w&
clrscr()&
cout'(nter the number)'&
cin>>num&
for(g=1&g=num&g!!)
%
w=u!$&
u=$&
$=w&
,
cout'*n''the 'num'th fibonacci number is 'w&
getch()&
,
OUTPUT
(nter the number)5
the 5th fibonacci number is 6
Program 5
a) Write a Program to 0rint the following 0attern)
3
3 3
3 3 3
3 3 3 3
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%

int n, x, 5&
clrscr()&
cout'(nter number of rows) '&
cin>>n&

for ( x = 1 & x = n & x!! )
%
for( 5 = 1 & 5 = x & 5!! )
cout'3'&

cout'*n'&
,
getch()&
,
OUTPUT
(nter number of rows) 5
3
33
333
3333
33333
b) Write a Program to 0rint the following 0attern
3
3 3
3 3 3
3 3 3 3
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%

int n, u,>,h&
clrscr()&
cout'(nter number of rows) '&
cin>>n&
h=n&
for ( u = 1 & u = n & u!! )
%
for( > = h & > >= 1 & >-- )
cout' '&
for( > = 1 & > = u & >!! )
%
cout'3'&
,
cout'*n'&
h--&
,
getch()&
,
OUTPUT
(nter number of rows) 7
3
33
333
3333
33333
333333
3333333
c) Write a Program to 0rint the following 0attern
0
0 1
0 1 -
0 1 - .
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int num,:,r&
clrscr()&
cout'enter the number of rows) '&
cin>>num&
for(r=0&r(-3num!1)&r!!)
%
cout'*n'&
if(rnum)
%
for(:=0&:=r&:!!)
cout:&
,
,
getch()&
,
OUTPUT
enter the number of rows) /
0
01
01-
01-.
#) Write a Program to 0rint the following 0attern
0
1 1
- - -
. . . .
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int n, @, 0&
clrscr()&
cout'(nter number of rows) '&
cin>>n&
for ( 0 = 0 & 0 n & 0!! )
%
for( @ = 0 & @ = 0 & @!! )
cout0&

cout'*n'&
,

getch()&
,
OUTPUT
(nter number of rows) /
0
11
---
....
e) Write a Program to 0rint the following 0attern
1
- .
/ 5 4
7 6 8 10
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%

int n, 0, >, r=1&
clrscr()&
cout'(nter number of rows) '&
cin>>n&

for ( 0 = 1 & 0 = n & 0!! )
%
for( > = 1 & > = 0 & >!! )
%
coutr&
r!!&
,
cout'*n'&
,
getch()&
,
OUTPUT
(nter number of rows) /
1
-.
/54
76810
f) Write a Program to 0rint the following 0attern
1
0 1
1 0 1
0 1 0 1
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int >,$,t=0,u,number&
clrscr()&
cout'enter the number of rows) '&
cin>>number&
for(>=1&>=number&>!!)
%
for($=1&$=>&$!!)
%
t!!&
coutt1-&
,
for(u=>&u=number&u!!)
%
t!!&
,
cout'*n'&
,
getch()&
,
OUTPUT
enter the number of rows) /
1
01
101
0101
g) Write a Program to 0rint the following 0attern
3
3 3 3
3 3 3 3 3
3 3 3 3 3 3 3
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int x, 5, n, h&
clrscr()&
cout'(nter the number of rows) '&
cin>>n&
h = n&
for ( x = 1 & x = n & x!! )
%
for ( 5 = 1 & 5 h & 5!! )
cout' '&
for ( 5 = 1 & 5 = -3x - 1 & 5!! )
cout'3'&
cout'*n'&
h--&
,
getch()&
,
OUTPUT
(nter the number of rows) /
3
333
33333
3333333
h) Write a Program to 0rint the following 0attern
1
- . -
. / 5 / .
/ 5 4 7 4 5 /
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int 0,w,;,$,count=0,num&
clrscr()&
cout'enter the number of rows) '&
cin>>num&
for(0=0&0num&0!!)
%
for(w=/&w>=0&w--)
%
cout' '&
,
for(;=0&;=0&;!!)
%
cout!!count&
,
for($=0&$>0&$--)
%
cout--count&
,
cout'*n'&
,
getch()&
,
OUTPUT
enter the number of rows) 5
1
-.-
./5/.
/54745/
547686745
i) Write a Program to 0rint the following 0attern
1
1 0 1
1 0 1 0 1
1 0 1 0 1 0 1
"inclu#e iostream.h>
"inclu#econio.h>
$oi# main()
%
int re0itition,g,h,n&
clrscr()&
cout'enter the number of rows) '&
cin>>re0itition&
n=re0itition&
for(g = 0& g re0itition& g!!)
%
for(h = n& h > 0& h--)
cout' '&
cout '1'&
for(h = 0& h g& h!!)
cout '01'&
cout '*n'&
n--&
,
getch()&
,
OUTPUT
enter the number of rows) /
1
101
10101
1010101
@) Write a Program to 0rint the following 0attern
1 - . / . - 1
1 - . - 1
1 - 1
1
"inclu#e iostream.h>
"inclu#econio.h>
$oi# main()
%
int i,s,x = 0, num,@ &
clrscr()&
cout'enter the number of rows) '&
cin>>num&
s=0&
for(i = 1& i = num& i!!)
%
for(@=1&@=s&@!!)
cout' '&
s!!&
x = (num!1) - i&
for(@ = 1& @ x& @!!)
%
cout@&
,
for(@ = x& @ > 0& @--)
%
cout@&
,
cout'*n'&
,
getch()&
,
OUTPUT
enter the number of rows) /
1-./.-1
1-.-1
1-1
1
Program 6
Write a Program to calculate gra#e of stu#ent accor#ing to mar;s obtaine# in 5 sub@ects
a) 1 >= 60 Ara#e B! b) 1 >= 40 an# 60 Ara#e B
c) 1 >= 50 an# 40 Ara#e C #) 1 >= /0 an# 50 Ara#e D
e) 1 /0 Ara#e ? (?BEF)
"inclu#eiostream.h>
"inclu#econio.h>
$oi# main()
%
int mar;s<5=,total=0,0ercent,g&
clrscr()&
cout'enter mar;s in fi$e sub@ects) '&
for(g=1&g=5&g!!)
%
cin>>mar;s<g=&
total=total!mar;s<g=&
,
0ercent=(total25)&
cout'the 0ercentage is '0ercent&
if(0ercent>=60)
cout'*n''AGBH( B!'&
else if (0ercent>=40 ++ 0ercent60)
cout'*n''AGBH( B'&
else if(0ercent>=50 ++ 0ercent40)
cout'*n''AGBH( C'&
else if(0ercent>=/0 ++ 0ercent50)
cout'*n''AGBH( D'&
else
cout'?BEF'&
getch()&
,
OUTPUT
enter mar;s in fi$e sub@ects) 46
46
58
80
86
the 0ercentage is 74
AGBH( B

You might also like