Loop Programs
Loop Programs
Question 1
#include<iostream>
int main()
int n;
cout<<n<<endl;
return 0;
Question 2
#include<iostream>
int main()
int n;
cout<<n<<endl;
}
return 0;
Question 3
Write a program that inputs 5 numbers from the user and the display their sum and
average.
#include<iostream>
int main()
int n,sum=0,avg;
cout<<"enter a number"<<endl;
cin>>n;
while (n!=0)
sum=sum+n%10;
n=n/10;
avg=sum/5;
return 0;
}
Question 4
#include<iostream>
int main()
int n=1,sum;
while (n<=10)
cout<<n<<endl;
sum=sum+n;
n=n+1;
cout<<"sum is"<<sum<<endl;
return 0;
Question 5
#include<iostream>
int main()
int n=1,odd;
odd=n;
cout<<""<<odd<<endl;
return 0;
Question 6
#include<iostream>
int main()
int sum=0;
sum=sum+i;
cout<<sum<<endl;
return 0;
Question 7
int main()
int product=1;
for(int i=1;i<=100;i=i+2){
product=product*i;
cout<<"odd numbers="<<i<<"\tproduct="<<product<<endl;
return 0;
Question 8
Write a program that inputs number and then display the table of enter no.
#include<iostream>
int main()
int n;
cout<<"enter a number"<<endl;
cin>>n;
cout<<n<<"*"<<i<<"="<<n*i<<endl;
}
return 0;
Question 9
Write a program that inputs number and length, then display the table.
#include<iostream>
int main()
int n,l;
cout<<"enter a number"<<endl;
cin>>n;
cin>>l;
cout<<n<<"*"<<i<<"="<<n*i<<endl;
return 0;
}
Question 10
Write a program to find the factorial value of any number entered through the keyboard.
#include<iostream>
int main()
int n,f=1,i;
cout<<"enter a number"<<endl;
cin>>n;
f=f*i;
return 0;
QNO11:
Two numbers are entered through the keyboard. Write a program to find the value of one
number raised to the power of another.
#include<iostream>
int main()
cin>>no;
cout<<"enter power"<<endl;
cin>>power;
product=product*no;
cout<<"product of number"<<"="<<product<<endl;
return 0;
Question 12
Write a program that enter a number from the use. The program reveres the given
integer number and display it on the screen.
#include<iostream>
using namespace std;
int main()
int no,reverse=0,remainder;
cin>>no;
while (no!=0)
remainder = no % 10;
no=no/10;
cout<<reverse <<endl;
return 0;
Question 14 Write a program that enter a number from the use. The program find the
sum of all digits of this number and display the sum on the screen.
#include<iostream>
int main()
int no,sum=0;
cin>>no;
while (no!=0)
sum=sum+no%10;
no=no/10;
cout<<"sum is"<<sum<<endl;
return 0;
Question 15 Write a program that inputs numbers from your till user enters negative
number. At the end program calculates the average, sum, maximum, and minimum, of all
positive numbers.
#include<iostream>
int main()
{
int num,z;
cin>>num;
min=num;
max=num;
while (num>=0.0f)
sum=sum+num;
avg=sum/num;
if (num>max)
max=num;
if (num<min)
min=num;
z++;
cin>>num;
cout<<"total values"<<"="<<z<<endl;
return 0;
#include<iostream>
int main()
int n,sum=0;
cout<<"enter a number"<<endl;
cin>>n;
while (n!=0)
sum=sum+n%10;
n=n/10;
return 0;
Question 17 Write a program that accept no from the user. It check the given number
is prime or not.
#include<iostream>
int main()
int no;
bool prime=false;
cout<<"enter a number"<<endl;
cin>>no;
if (no%i==0)
prime=false;
break;
else if (no%2!=0)
prime=true;
break;
}
return 0;
Question 18 Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.
#include<iostream>
int main()
int num,neg=0,pos=0,zero=0;
char choice;
cout<<"enter a number"<<endl;
cin>>num;
if (num>0)
pos++;
if (num<0)
neg++;
if(num=0)
{
zero++;
cin>>choice;
if (choice=='N' || choice=='n')
break;
return 0;
Question 19
Write a program to enter the numbers till the user wants and at the end it should display
the maximum and minimum number entered.
#include<iostream>
int main()
int no,max,min;
char choice;
cout<<"enter a number"<<endl;
cin>>no;
max=no;
min=no;
while (no>=0)
if (no>max)
max=no;
if (no<min)
min=no;
cin>>choice;
cout<<"enter number"<<endl;
cin>>no;
break;
cout<<"Max number="<<max<<endl;
cout<<"Min number="<<min<<endl;
return 0;
Question 20+21
Write a program to print out the enter number is Armstrong numbers or not. If sum of
cubes of each digit of the number is equal to the number itself, then the number is called
an Armstrong number. For example 153 (111)+(555)+(3*3*3)
#include<iostream>
int main()
cout<<"Enter a number:";
cin>>num;
n = num;
sum = 0;
while(n != 0)
r = n % 10;
n/= 10;
if (sum == num)
if (sum!= num)
{
return 0;
#include<iostream>
int main()
int no;
bool prime=false;
cout<<"enter a number"<<endl;
cin>>no;
if (no%i==0)
prime=false;
break;
else if (no%2!=0)
prime=true;
return 0;
#include<iostream>
int main()
int num=1,prenum=0,sum,n;
cin>>n;
cout<<"0,1 ,";
sum=num+prenum;
prenum=num;
num=sum;
cout<<num<<","<<endl;
}
return 0;
Question 23 Write a program to calculate the sum of following series where n is input by
user.
#include<iostream>
int main()
int n,i;
float sum=0.0f;
cin>>n;
sum=sum+(1.0f/i);
cout<<"sum is"<<sum<<endl;
return 0;
Question 24 Write a program to calculate the sum of following series where n is input by
user.
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1 / r where n is a positive integer and input by user.
#include<iostream>
using namespace std;
int main()
int n,i;
float sum=0.0f;
cin>>n;
sign=sign*-1;
sum=sum+(1.0f/i)*sign;
cout<<"sum is"<<sum<<endl;
return 0;
Question No 28:
#include<iostream>
int main()
{
prime=true;
if (i%j==0)
prime =false;
if (prime==true)
return 0;
}
QNO25(i)
**********
**********
**********
**********
#include<iostream>
cout<<"*";
cout<<endl;
return 0;
(ii)
**
***
****
*****
#include<iostream>
int main()
cout<<"*";
cout<<endl;
return 0;
(iii)
*****
****
***
**
#include<iostream>
int main()
cout<<" ";
{
cout<<"*";
cout<<endl;
return 0;
(iv)
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<iostream>
int main()
cout<<j;
cout<<endl;
return 0;
}
(v)
2 1
3 2 1
4 3 2 1
5 4 3 2 1
#include<iostream>
int main()
cout<<" ";
cout<<k;
cout<<endl;
return 0;
}
(vi)
2 4
3 6 9
4 8 12 16
5 10 15 20 25
#include<iostream>
int main()
cout<<" "<<i*k;
cout<<endl;
return 0;
(vii)
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
#include <iostream>
int main() {
return 0;
(viii)
A B
A B C
A B C D
A B C D E
#include <iostream>
char i,j,k;
return 0;
(IX)
***
*****
*******
*********
#include <iostream>
int main() {
int i,j,k;
for(i=1; i<=5;i++)
{
for(k=1; k<=5-i;k++)
{ cout<<" ";
for(j=1;j<i*2;j++)
return 0;
(X)
BBBBBBBBB
BBBBBBB
BBBBB
BBB
#include <iostream>
int main()
int i,j,k;
for(i=5; i>=1;i--)
{
for(k=1; k<=5-i;k++)
{ cout<<" ";
for(j=1;j<i*2;j++)
return 0;
(XI)
222
33333
4444444
555555555
#include <iostream>
int main()
int i,j,k;
for(i=1; i<=5;i++)
{
for(k=1; k<=5-i;k++)
{ cout<<" ";
for(j=1;j<i*2;j++)
return 0;
(XII)
212
32123
4321234
543212345
#include <iostream>
int main()
int i,j,k,l;
for(i=1; i<=5;i++)
{
for(k=1; k<=5-i;k++)
{ cout<<" ";
for(j=i;j>=1;j--)
cout<<l;
cout<<endl;
return 0;
(XIII)
4 8
3 6 9
2 4 6 8
1 2 3 4 5
#include <iostream>
int i,j,k,l;
cout<<" ";
l=j*i;
cout<<l;
cout<<endl;
return 0;
Question 26 Find the sum of first five terms of the following series.
#include <iostream>
int main()
int i,j;
double fact=1,sum=0;
for (i=1; i<=5;i++)
fact=1;
fact=fact*j;
sum=sum+(i/fact);
return 0;
Question 27 Find the sum of first five terms of the following series.
#include <iostream>
int main()
int i,j,sign=-1;
double fact=1,sum=1;
fact=1;
sign=sign*-1;
for (j=2;j<=i; j++)
fact=fact*j;
sum=sum+(i/fact)*sign;
return 0;
THE END:)