Chapter # 06: LOOP
Question No 1: Write a program to display the following format using while loop.
Solution :
#include <iostream.h>
#include <iomanip.h>
int main()
{
clrscr();
int i;
cout<< "----------" <<endl;
cout<<"a b"<<endl;
cout<< "----------" <<endl;
i =0;
while(i<5)
{
cout<<i+1<<setw(9)<<5-i<<endl;
i++;
}
cout<< "----------" <<endl;
return 0;
return 0;
}
Question No. 02 : Write a program to display the format by using while loop.
Solution :
Solution :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
int i , sum = 0;
cout<< "--------------" <<endl;
cout<<"num sum"<<endl;
cout<< "--------------" <<endl;
i =0;
while(i<5)
{
sum = sum + i;
sum = sum + i;
i++;
}
cout<< "--------------" <<endl;
getch();
}
OR
#include <iostream.h>
#include <conio,h>
#include <iomanip.h>
void main ()
{
clrscr();
int j = 0;
while ( j < 10)
{
cout<< "-";
j++;
}
cout<< endl;
cout<<"num sum"<<endl;
j = 0;
while( j < 10)
{
cout<< "-";
j++;
}
cout<< endl;
int i , sum =0;
i = 1;
while (i <= 5)
{
sum+= i;
cout<<i<<setw(9)<<sum<<endl;
i++;
}
i = 0;
while( i < 10)
{
cout<< "-";
i++;
}
cout<<endl;
getch();
}
Question No. 03:
Write a program that displays the sum of the following series using do-while loop.
1+ 1/4 + 1/8 + ........ + 1/100
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
float c , r;
clrscr();
c = 4.0;
r = 1.0;
do
{
r = r + 1.0;
c = c + 4;
}while( c <= 100);
cout<<"Result : " <<r;
getch();
}
Question No. 04: Write s program to display alphabets form A to Z using for loop.
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
char ch;
clrscr();
// for loop to print alphabets
for(ch = 'A'; ch <= 'Z'; ch++)
{
cout<<ch;
}
getch();
}
Question No. 05:
Write a program to find the largest , smallest and average of n whole numbers.
You can assume that "n" has already been set by the user.
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int largest , smallest , value;
int n = 10;
float total;
cout<<"Please enter a whole number : " ;
cin >>vlaue;
largest = smallest = total = value;
for(int j= 0; j< n-1; ++j)
{
cout<<"Please enter a whole number: ";
cin>>value;
total += value;
if(value > largest)
largest = value;
if(value < smallest)
smallest = value;
}
cout<<"The largest value is: "<<largest<<endl;
cout<<"The smallest value is: "<<smallest<<endl;
cout<<"The average is: "<<total/n<<endl;
getch();
}
Question No. 06:
Write a program that will ask the user a question with four possible answers. The question should be asked 20 times. After all the input is gathered, the program should output the number of times each answer was selected.
Solution:
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int Answer1 = 0 , Answer2 = 0, Answer3 = 0, Answer4 = 0l
int Counter;
int Value;
for(Counter = 0; Counter < 20 ; Counter++)
{
cout<<"Enter either 1,2,3 or 4 "<<endl;
cin>>Value;
switch(Value)
{
case 1:
Answer1++;
break;
case 2:
Answer2++;
break;
case 3:
Answer3++;
break;
case 4:
Answer4++;
break;
default:
cout<<"Incorrect choice"<<endl;
Counter--;
}
}
cout<<"Number of Answer 1's = "<<Answer1<<endl;
cout<<"Number of Answer 2's = "<<Answer2<<endl;
cout<<"Number of Answer 3's = "<<Answer3<<endl;
cout<<"Number of Answer 4's = "<<Answer4<<endl;
getch();
}
OR
#include <iostream.h>
#include <conio,h>
#include <iomanip.h>
void main ()
{
clrscr();
int j = 0;
while ( j < 10)
{
cout<< "-";
j++;
}
cout<< endl;
cout<<"num sum"<<endl;
j = 0;
while( j < 10)
{
cout<< "-";
j++;
}
cout<< endl;
int i , sum =0;
i = 1;
while (i <= 5)
{
sum+= i;
cout<<i<<setw(9)<<sum<<endl;
i++;
}
i = 0;
while( i < 10)
{
cout<< "-";
i++;
}
cout<<endl;
getch();
}
Question No. 03:
Write a program that displays the sum of the following series using do-while loop.
1+ 1/4 + 1/8 + ........ + 1/100
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
float c , r;
clrscr();
c = 4.0;
r = 1.0;
do
{
r = r + 1.0;
c = c + 4;
}while( c <= 100);
cout<<"Result : " <<r;
getch();
}
Question No. 04: Write s program to display alphabets form A to Z using for loop.
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
char ch;
clrscr();
// for loop to print alphabets
for(ch = 'A'; ch <= 'Z'; ch++)
{
cout<<ch;
}
getch();
}
Question No. 05:
Write a program to find the largest , smallest and average of n whole numbers.
You can assume that "n" has already been set by the user.
Solution :
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int largest , smallest , value;
int n = 10;
float total;
cout<<"Please enter a whole number : " ;
cin >>vlaue;
largest = smallest = total = value;
for(int j= 0; j< n-1; ++j)
{
cout<<"Please enter a whole number: ";
cin>>value;
total += value;
if(value > largest)
largest = value;
if(value < smallest)
smallest = value;
}
cout<<"The largest value is: "<<largest<<endl;
cout<<"The smallest value is: "<<smallest<<endl;
cout<<"The average is: "<<total/n<<endl;
getch();
}
Question No. 06:
Write a program that will ask the user a question with four possible answers. The question should be asked 20 times. After all the input is gathered, the program should output the number of times each answer was selected.
Solution:
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int Answer1 = 0 , Answer2 = 0, Answer3 = 0, Answer4 = 0l
int Counter;
int Value;
for(Counter = 0; Counter < 20 ; Counter++)
{
cout<<"Enter either 1,2,3 or 4 "<<endl;
cin>>Value;
switch(Value)
{
case 1:
Answer1++;
break;
case 2:
Answer2++;
break;
case 3:
Answer3++;
break;
case 4:
Answer4++;
break;
default:
cout<<"Incorrect choice"<<endl;
Counter--;
}
}
cout<<"Number of Answer 1's = "<<Answer1<<endl;
cout<<"Number of Answer 2's = "<<Answer2<<endl;
cout<<"Number of Answer 3's = "<<Answer3<<endl;
cout<<"Number of Answer 4's = "<<Answer4<<endl;
getch();
}
Question No.07:
Write a program that inputs a series of 20 numbers and display minimum value.
Solution :
#include<iostream.h>
#include <conio.h>
void main()
{
int Min;
int Counter;
int value;
cout<<"Enter a Number: ";
cin>>Min;
for(Counter = 0; Counter <19; Counter++)
{
cout<<"Enter a Number; ";
cin >>value;
if(value<Min)
Min = value;
}
cout<<"Minimum value is : "<<Min <<endl;
getch();
}
Question No. 08:
Write a program that inputs a number form the user and display Fibonacci series upto number entered.
Solution :
#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, next, n;
clrscr();
cout<<"Enter the number upto which fibonacci series is required: ";
cin>>n;
cout<<"Fibonacci series upto "<<n<<"is "<<endl;
a = 0;
b = 1;
cout<<a<<" "<<b;
next = a + b;
while(next <= n)
{
cout<<" "<<next;
a = b;
b = next;
next = a + b;
}
getch();
}
Question No.48: Write the program to display the following output :
Solution:
#include <iostream.h>
int main()
{
int i, j , n=1;
for(i=1; i<=7; i++)
{
for(j =1; j<=i;j++)
{
cout<< n <<" ";
n=n+2;
}
cout<<endl;
}
return 0;
}