Programming Exercise Looping Structure P-12

Looping Structure

Programming Exercise

Programming Exercise Looping Structure P11

IF Statement Programming Exercise Part-1

Q.84 Write a program that displays the following output using the nested ‘while’ loop:
// 1
// 2 4
// 3 6 9
// 4 8 12 16
// 5 10 15 20 25

#include<iostream.h>
#include<conio.h>
main()
{

int i, j;
clrscr();
i = 1;
while(i<=5)
{
j = 1;
while(j<=i)
{
cout<<j*i<<'\t';
j++;
}
i++;
cout<<endl;
}
getch();
}

Q.85 Write a program that displays the following output using the nested ‘do-while’ loop.
// 5 5 5 5 5
// 4 4 4 4
// 3 3 3
// 2 2
// 1

#include<iostream.h>
#include<conio.h>
main()
{

int i, j;
clrscr();
i = 5;
do
{
j = 1;
do
{
cout<<i<<'\t';
j++;
}while(j<=i);
cout<<endl;
i--;
}while(i>=1);
getch();
}

Q.86 Write a program to print the following output:
// 1
// 2 3
// 4 5 6
// 7 8 9 10
// 11 12 13 14 15

#include<iostream.h>
#include<conio.h>
main()
{

int i, j, n;
clrscr();
i = 1;
n = 1;
while(i<=5)
{
j = 1;
while(j<=i)
{
cout<<n<<'\t';
j++;
n++;
}
cout<<endl;
i++;
}
getch();
}

Q.87 Write a program to print the following output:
// 1
// 3 5
// 7 9 11
// 13 15 17 19
// 21 23 25 27 29
// 31 33 35 37 39 41
// 43 45 47 49 51 53 55
//

#include<iostream.h>
#include<conio.h>
main()
{

int i, j, n;
clrscr();
i = 1;
n = 1; 
while(i<=7)
{
j = 1;
while(j<=i)
{
cout<<n<<'\t';
j++;
n+=2;
}
cout<<endl;
i++;
}
getch();
}

Q.88 Write a program that displays the following output using the nested ‘for’ loop.
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1

#include<iostream.h>
#include<conio.h>
main()
{

int i, j, s;
clrscr();
for(i = 5; i>=1;i--)
{

for(s = 1; s<=5-i;s++) // this loop insert required tabs at the beginning of a line
cout<<'\t';
for(j = 1; j<=i;j++) // this loop prints the value and insert tab after each value
cout<<j<<'\t';
cout<<endl;
}
getch();
}

Q.89 Write a program that displays the following output using the nested loop.
// 1
// 1 4
// 1 4 9
// 1 4 9 16
// 1 4 9 16 25

#include<iostream.h>
#include<conio.h>
main()
{

int i, j, s;
clrscr();
for(i = 1; i<=5;i++)
{

for(s = 1; s<=5-i;s++) // this loop insert required tabs at the beginning of a line
cout<<'\t';
for(j = 1; j<=i;j++) // this loop prints the value and insert tab after each value
cout<<j*j<<'\t';
cout<<endl;
}
getch();
}

Q.90 Write a program that displays the following output:
// 0
// 0 1
// 0 1 2
// 0 1 2 3
// 0 1 2 3 4
// 0 1 2 3 4 5

#include<iostream.h>
#include<conio.h>
main()
{

int i, j;
clrscr();
for(i = 0; i<=5;i++)
{

for(j = 0; j<=i;j++) 
cout<<j<<'\t';
cout<<endl;
}
getch();
}

Q.91 Write a program that displays the following output using the nested loop.
// # # # # #
// # # # # #
// # # # # #
// # # # # #
// # # # # #

#include<iostream.h>
#include<conio.h>
main()
{

int i, j;
clrscr();
for(i = 1; i<=5;i++)
{
for(j = 1; j<=5;j++)
cout<<"* ";
cout<<endl;
}
getch();
}

Q.92 Write a program that displays the following output using nested “for” loop and “setw” manipulator.
//
// X
// X X
// X X X
// X X X X
// X X X X
// X X X
// X X
// X
//

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
main()
{
int u,i,space=20;
clrscr();

// first nested loop for upper part of the shape
for(u = 1; u<=4; u++)
{
cout<<setw(space);
for(i = 1; i<=u;i++)
cout<<"X ";
space--;
cout<<endl;
}

// second nested loop for lower part of the shape
space++;
for(u = 4; u>=1; u--)
{
cout<<setw(space);
for(i = 1; i<=u;i++)
cout<<"X ";
space++;
cout<<endl;
}
getch();
}

Q.93 Write a program that displays the following output using nested “for” loop and “setw” manipulator.
// X X X X X
// X X X X
// X X X
// X X
// X
//

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
main()
{
int u,i,space=20;
clrscr();
for(u = 5; u>=1; u--)
{
cout<<setw(space);
for(i = 1; i<=u;i++)
cout<<"X ";
space++;
cout<<endl;
}
getch();
}

Q.94 Write a program that displays the following output using the nested loop.
// T
// T T
// T T
// T T
// T T

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
main()
{
int u, i, s = 20;
clrscr();
for(u = 1; u<=10; u++)
{
cout<<setw(s);
for(i = 1; i<=u; i++)
if(i==1 || i == u)
cout<<"T ";
else
cout<<" ";
cout<<endl;
s--;
}
getch();
}

Q.95 Write a program that displays the following output using the nested loop.

// # # # # #
// # #
// # #
// # #
// # # # # #

#include<iostream.h>
#include<conio.h>
main()
{
int u, i;
clrscr();
for(u = 1; u<=5; u++)
{
for(i = 1; i<=5; i++)
if(i==1 || u == 1 || i==5 || u == 5)
cout<<"*";
else
cout<<" ";

cout<<endl;
}
getch();
}

Q.96 Write a program that displays the following output using the nested ‘for’ loop.
//
// * *
// *
// * *

#include<iostream.h>
#include<conio.h>
main()
{
int u,i;
clrscr();
for(u=1;u<=3;u++)
{
for(i=1;i<=3;i++)
{
if(u%2==0)
{
if(i%2==0)
cout<<"* ";
else
cout<<" ";
}
else
{
if(i%2==0)
cout<<" ";
else
cout<<"*";
}
}
cout<<endl;
}
getch();
}

Q.97 Write a program that displays the following output using the nested loop.
//
//
// * *
// * *
// * *
// *

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
main()
{
int u, i, s = 20;
clrscr();
for(u = 4; u>=1; u--)
{
cout<<setw(s);
for(i = 1; i<=u; i++)
if(i==1 || i == u)
cout<<"* ";
else
cout<<" ";
cout<<endl;
s++;
}
getch();
}

 Q.98 Write a program to print the following output using nested any loop

#include<iostream.h>
#include<conio.h>
main()
{
int i, u = 1, res;
clrscr();
while(u<=4)
{
res = 1;
i = 1;
while(i<=u)
{
cout<<res<<'\t';
res*=6;
i++;
}
cout<<endl;
u++;
}
getch();
}

 

Leave a Reply