
a) Write a program to compute ab where a and b are of real and integer tyes(use while...loop)
b) Write a program to computer the factorial of a given number.(use for() loop).
c) Write a program to generate Fibonacci seires upto nth term.
d) Write a program to print the following patterns:
d1) 1
12
123
d2) 4
34
234
d3) A
BC
DEF
GHIJ
e) Using switch, write a program to accept the day in a month, and print the messages as:
If day is 1, message is 1st day in the month
If day is 2,22, message is 2nd/22nd day in the month
If day is 4,14,15,16…. message is 4th/14th …. day in the month.
a) To find ab
#include<iostream.h>
#include<conio.h>
void main()
{
float a,p=1;int i,b;i=1;
cout<<"\nEnter the number:";cin>>a;
cout<<"\nEnter the power to be raised:";cin>>b;
while(i<=b)
{
p*=a;i++;
}
cout<<a<<" raised to the power of "<<b<<" is"<<p;
getch();
}
Explanation:
Assume the input to be a=5 b=3;
|
i |
p |
increment |
Condition i<=b |
|
1 |
5[1*5] |
2 |
True |
|
2 |
25[5*5] |
3 |
True |
|
3 |
125[25*5] |
4 |
False |
Output: 125

#include<iostream.h>
#include<conio.h>
void main()
{
long f=1;
int n;clrscr();
cout<<"\nEnter a number";
cin>>n;
for(int i=1;i<=n;i++)
{
f*=i;
}cout<<"factorial of "<<n<<" is "<<f;
getch();
}
|
i<=n |
f |
i |
|
True |
1[1*1] |
2 |
|
True |
2[1*2] |
3 |
|
True |
6[2*3] |
4 |
|
False |
|
|
Output: 6

c)To generate Fibinocci series
#include<iostream.h>
#include<conio.h>
void main()
{
int f1,f2,f3,n;
f1=-1;f2=1;clrscr();
cout<<"\nEnter the number:";
cin>>n;
while(n>0)
{
f3=f1+f2;
f1=f2;f2=f3;
cout<<"\n"<<f3;
n--;
}
getch();
}
Explanation:
Assume input n=8
|
n>0 |
f1 |
f2 |
f3 |
n |
|
True |
-1 |
1 |
0 |
8 |
|
True |
1 |
0 |
1 |
7 |
|
True |
0 |
1 |
1 |
6 |
|
True |
1 |
1 |
2 |
5 |
|
True |
1 |
2 |
3 |
4 |
|
True |
2 |
3 |
5 |
3 |
|
True |
3 |
5 |
8 |
2 |
|
True |
5 |
8 |
13 |
1 |
|
True |
8 |
13 |
21 |
0 |
|
False |
|
|
|
|

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<j<<"\t";
}
cout<<"\n";
}
getch();
}
|
i |
j |
Output |
|
1 |
1 |
1 |
|
1 |
2 |
|
|
2 |
1 |
1 |
|
2 |
2 |
2 |
|
2 |
3 |
inner loop terminates |
|
3 |
1 |
1 |
|
3 |
2 |
2 |
|
3 |
3 |
3 |
|
3 |
4 |
inner loop terminates |
|
4 |
1 |
1 |
|
4 |
2 |
2 |
|
4 |
3 |
3 |
|
4 |
4 |
4 |
|
4 |
5 |
inner loop terminates |
|
5 |
1 |
1 |
|
5 |
2 |
2 |
|
5 |
3 |
3 |
|
5 |
4 |
4 |
|
5 |
5 |
5 |
|
5 |
6 |
inner loop terminates |
|
6 |
Outer loop terminates |
|

4
34
234
1234
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;clrscr();
for(i=4;i>0;i--)
{
for(j=i;j<=4;j++)
{
cout<<j<<"\t";
}
cout<<"\n";
}
getch();
}
|
i |
j |
Output |
|
4 |
4 |
4 |
|
4 |
5 |
inner loop terminates |
|
3 |
3 |
3 |
|
3 |
4 |
4 |
|
3 |
5 |
inner loop terminates |
|
2 |
2 |
2 |
|
2 |
3 |
3 |
|
2 |
4 |
4 |
|
2 |
5 |
inner loop terminates |
|
1 |
1 |
1 |
|
1 |
2 |
2 |
|
1 |
3 |
3 |
|
1 |
4 |
4 |
|
1 |
5 |
inner loop terminates |
|
0 |
Outer loop terminates |
|

A
BC
DEF
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
char ch='A';clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<ch<<"\t";ch++;
}
cout<<"\n";
}
getch();
}
Explanation
|
i |
j |
Output |
ch |
|
|
1 |
1 |
A |
B |
|
|
1 |
2 |
|
|
|
|
2 |
1 |
B |
C |
|
|
2 |
2 |
C |
D |
|
|
2 |
3 |
inner loop terminates |
||
|
3 |
1 |
D |
E |
|
|
3 |
2 |
E |
F |
|
|
3 |
3 |
F |
G |
|
|
3 |
4 |
inner loop terminates |
||
|
4 |
1 |
G |
H |
|
|
4 |
2 |
H |
I |
|
|
4 |
3 |
I |
J |
|
|
4 |
4 |
J |
K |
|
|
4 |
5 |
inner loop terminates |
||
|
5 |
Outer loop terminates |
|||

e) To display day and month in words
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int day,month;
clrscr();
cout<<"\nEnter the day:";
cin>>day;
cout<<"\nEnter the month:";
cin>>month;
if(day>0 &&day<32)
switch(day)
{
case 1:case 21:case31:
cout<<day<<"st of ";break;
case 2:case 22:cout<<day<<"nd of ";
break;
case 3:case 23:cout<<day<<"rd of ";break;
default:cout<<day<<"th of ";break;
}
else
{ cout<<"Invalid date"; exit(1);}
if(month>0 && month<=12)
switch(month)
{
case 1:cout<<"January";break;
case 2:cout<<"February";break;
case 3:cout<<"March";break;
case 4:cout<<"April";break;
case 5:cout<<"May";break;
case 6:cout<<"June";break;
case 7:cout<<"July";break;
case 8:cout<<"August";break;
case 9:cout<<"September";break;
case 10:cout<<"October";break;
case 11:cout<<"November";break;
case 12:cout<<"December";break;
}
else
{cout<<"\nInvalid month";exit(1);}
getch();
}
Explanation:
Assume input is day=15 month=4
The if condition is satisfied, day is between 1 and 31. But it does not satisfy any of the cases so statement under default is printed.
15th day of
The second if condition is also satisfied, month is between 1 and 12. Since month is 4 the statement under case 4 is printed.
April
Output:
15th day of April
