Programming Exercise

Programming Exercise Looping Structure P-8

Conditional Structure Programming Exercise P-6

Q.50 Write a program which reads any positive integer value from the keyboard during execution of the program and displays all its digits separately. For example, if number is 69482, it should be displayed as:// 6// 9// 4// 8// 2

```#include<iostream.h>
#include<conio.h>
main()
{
unsigned long r, n;
clrscr();
cout<<"Enter any integer value ? ";
cin>>n;
do
{
r = n%10;
n = n/10;
cout<<r<<endl;

}while(n!=0);
getch();
}```

Q.51 Write a program that displays the following series using the do-while loop.// 1, -4, 7, -10, ……, -40

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1, p;
clrscr();
do
{
if(n%2==0)
p = -n;
else
p = n;
cout<<p<<'\t';
n = n + 3;
}while(n<=40);
getch();
}```

Q.52 Write a program to show the following output using a single do-while loop:// 1 1// 2 3// 3 6// 4 10// 5 15

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1, m = 1;
clrscr();
do
{
cout<<n<<'\t'<<m<<endl;
n = n + 1;
m = m + n;
}while(n<=5);
getch();
}```

Q.53 Write a program to show the following output using a do-while loop:// 1// 2// 3// 4// 5// Product=120

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1, p = 1;
clrscr();
do
{
cout<<n<<endl;
p = p * n;
n = n +1;
}while(n<=5);
cout<<"Product = "<<p;
getch();
}```

Q.54 Write a program to show the following output using a single do-while loop:// 1 1// 2 2// 3 6// 4 24// 5 120//

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1, m = 1;
clrscr();
do
{
cout<<n<<'\t'<<m<<endl;
n = n + 1;
m = m *n;
}while(n<=5);
getch();
}```

Q.55 Write a program to show the following output using a single do-while loop:// 1 1 1// 2 3 2// 3 6 6// 4 10 24// 5 15 120

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1, m = 1, p = 1;
clrscr();
do
{
cout<<n<<'\t'<<m<<'\t'<<p<<endl;
n = n + 1;
m = m + n;
p = p * n;
}while(n<=5);
getch();
}```

Q.56 Write a program that displays the following output using a single do-while loop:// 1 2 3// 4 5 6// 7 8 9

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1;
clrscr();
do
{
cout<<n<<'\t';
if(n == 3 || n == 6)
cout<<endl;
n = n + 1;

}while(n<=9);
getch();
}```

Q.57 Write a program that shows the following output using a single do-while loop:// 1 2 3 4// 5 6 7 8// 9 10 11 12

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 1;
clrscr();
do
{
cout<<n<<'\t';
if(n == 4 || n == 8)
cout<<endl;
n = n + 1;

}while(n<=12);
getch();
}```

Q.58 Write a program that displays the uppercase alphabetic letters (A to Z) and their corresponding ASCII values using the do-while loop. The output should be as under:// Letter ASCII value// ===================// A 65// B 66// C 67// ————————// Z 90

```#include<iostream.h>
#include<conio.h>
main()
{
int n = 65;
char ch;
clrscr();
cout<<"Letter"<<'\t'<<"ASCII value"<<endl;
cout<<"=================================="<<endl;
do
{
ch = n;
cout<<ch<<'\t'<<n<<endl;
n = n + 1;

}while(n<=90);
getch();
}```

Q.59 Write a program to calculate and display sum of the following series using the do-while loop:// 1 + 1/4 + 1/9 +1/16 +……. + 1/100

```#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
int n = 1;
float sum = 0, res;
clrscr();
do
{
res = pow(n, 2);
sum = sum + 1.0/res;
cout<<"1/"<<res<<" = "<<1/res<<endl;
n++;
}while(n<=10);
cout<<"Sum of series = "<<sum;
getch();
}```

Q.60 Write a program to generate the following series using the ‘for’ loop.// 5, 10, 15, 20, ……, 100

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
for( n = 1; n<=20; n++)
cout<<n*5<<'\t';
getch();
}```

Q.61 Write a program to calculate and display sum of the following series using the for loop.// 1! + 2! + 3! + 4! + 5!// The symbol “!” represents the factorial of the number.

```#include<iostream.h>
#include<conio.h>
main()
{
long i, j, f, sum = 0;
clrscr();
for( i = 1; i<=5; i++)
{
f = 1;
for( j = i; j>=1; j--)
f = f *j;
cout<<i<<"! = "<<f<<endl;
sum = sum + f;
}
cout<<"Sum of series :"<<sum;
getch();
}```