# Programming Exercise Looping Structure P-6

Programming Exercise Looping Structure P-5

Conditional Structure Programming Exercise P-6

#### Q.18 Write a program that displays the following output using a single loop:// 0 10// 1 8// 2 6// 3 4// 4 2// 5 0

```#include<iostream.h>
#include<conio.h>
main()
{
int n1, n2;
n1 = 0;
n2 = 10;
clrscr();
while(n1<=5)
{
cout<<n1<<'\t'<<n2<<endl;
n1 = n1+1;
n2 = n2-2;
}
getch();
}```

#### Q.19 Write a program that displays the following output using a single loop:// 1 1// 2 3// 4 9// 8 27// 16 81// 32 243

```#include<iostream.h>
#include<conio.h>
main()
{
int n1, n2;
n1 = 1;
n2 = 1;
clrscr();
while(n1<=32)
{
cout<<n1<<'\t'<<n2<<endl;
n1 = n1*2;
n2 = n2*3;
}
getch();
}```

#### Q.20 Write a program that displays the following output using a single loop:// 1 1// 2 2// 4 3// 8 4// 16 5// 32 6

```#include<iostream.h>
#include<conio.h>
main()
{
int n1, n2;
n1 = 1;
n2 = 1;
clrscr();
while(n2<=6)
{
cout<<n1<<'\t'<<n2<<endl;
n1 = n1*2;
n2++;
}
getch();
}```

#### Q.21 Write a program that displays the following output using a single loop:// 10 1// 9 2// 8 4// 7 8// 6 16// 5 32

```#include<iostream.h>
#include<conio.h>
main()
{
int n1, n2;
n1 = 10;
n2 = 1;
clrscr();
while(n1>=5)
{
cout<<n1<<'\t'<<n2<<endl;
n1--;
n2 = n2*2;
}
getch();
}```

#### Q.22 Write a program that displays the following output using a single loop:// 1 1 1// 2 4 8// 3 9 27// 4 16 64// 5 25 125

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

#### Q.23 Write a program that inputs 10 values repeatedly in a single variable using the ‘while’ loop structure. It finds out the largest and the smallest input values inside the body of loop and displays these values when loop is terminated.

```#include<iostream.h>
#include<conio.h>
main()
{
int n, count, largest, smallest;
clrscr();
cout<<count<<" Enter a value ? ";
cin>>n;
largest = smallest = n;
count = 1;
while(count<10)
{
cout<<count<<" Enter a value ? ";
cin>>n;
if(n > largest)
largest = n;
if(n < smallest)
smallest = n;
count++;

}
cout<<" Largest value is : "<<largest<<endl;
cout<<" Smallest value is : "<<smallest;

getch();
}```

#### Q.24 Write a program that displays prime numbers between 2 and 100 using the ‘while’ loop.

```#include<iostream.h>
#include<conio.h>
main()
{
int d, p, n = 2;
clrscr();
while(n<=100)
{
d = 2;
p = 1;
while(d < n)
{
if (n%d ==0)
{
p = 0;
break;
}
d++;
}
if(p == 1)
cout<<n<<'\t';
n++;;

}
getch();
}```

#### Q.25 Write a program that calculates and displays the product of the even numbers from 1 to 8 using the while loop.

```#include<iostream.h>
#include<conio.h>
main()
{
int prod = 1, n = 1;
clrscr();
while(n<=8)
{
if(n%2 == 0)
{
cout<<n<<endl;
prod = prod*n;
}
n++;
}
cout<<"Product = "<<prod;
getch();
}```

#### Q.26 Write a program that inputs and displays the lowest digit and highest digit in the number. For example, if the user enters 39517, the program should display the output as follows:// The Lowest digit in the number 39517 = 1// The Highest digit in the number 39517 = 9

```#include<iostream.h>
#include<conio.h>
main()
{
long n, r, lowest, highest;
lowest= 9;
highest = 0;
clrscr();
cout<<" Enter a value ? ";
cin>>n;

while(n!=0)
{
r = n%10;
if(r > highest)
highest = r;
if(r < lowest)
lowest = r;
n = n/10;

}
cout<<" Lowest digit in the number: "<<lowest<<endl;
cout<<" Highest digit in the number : "<<highest;

getch();
}```

#### Q.27 Write a program that inputs and checks whether it is a palindrome or not using the ‘while loop. A number is palindrome if it is read same backward and forward. For example, the numbers 4994, 6886, and 62526 are palindrome numbers.

```#include<iostream.h>
#include<conio.h>
main()
{
long n, r, num, rev = 0;
clrscr();
cout<<" Enter a number ? ";
cin>>n;
num = n;
while(n!=0)
{
r = n%10;
rev = (rev*10)+r;
n = n/10;

}
cout<<" Reverse number is : "<<rev<<endl;
if(num == rev)
cout<<" Number is palindrome : ";
else
cout<< "Number is not palindrome ";
getch();
}```