Loops are used to repeat set of statement as long as specified condition is true.
loop/Iteration is a block of statement which are repeatedly executed for a certain number of times.
Loop is used to execute the statements repeatedly as long as the specified condition is true.
There are two categories of Loops:
- Entry control loop:- This loop executes when specified condition is true. Example for loop, while loop.
- Exit control loop:- This loop executes at least once whether the specified condition is true or false. example do while loop.
Parts of Loop
There are following parts of loop.
Initialization :- It indicates from where loop is to be started.
example: i=1;
Test condition:- It indicates how many times loop is to be executed.
example: i<=5;
Increment /Decrement :- To reach an end of the test condition increment or decrement is needed.
example: i++, i- –
For Loop:–
This loop is the easiest loop among all the loops because it contains all the loop control structure at one place in a pair of parenthesis.
It is used for fixed time iteration.
It checks the initialization first then evaluates the test condition and if test condition becomes true then body of the loop is executed.
At last control goes to increment\ decrement and again test condition is evaluated , if test condition founds true then again body of the loop is executed.
This process continues until the specified condition is true.
syntax:
for(initialization; test condition ; increment/decrement)
{
body of the loop;
}
Q.Write a program to print your name five times using for loop.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=5;i++)
{
cout<<"\n AIPS ACADEMY";
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=5;i>=1;i--)
{
cout<<"\n AIPS ACADEMY";
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Q.Write a program to print numbers from 1 to 5.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=5;i++)
{
cout<<”\n”<<i;
}
getch();
}
output
1
2
3
4
5
Q. Write a program to print number from 5 to 1.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=5;i>=1;i- -)
{
cout<<”\n”<<i;
}
getch();
}
Output
5
4
3
2
1
Q. Write a program to print even number from 1 to 20.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=2;i<=20;i+=2)
{
cout<<”\n”<<i;
}
getch();
}
Output
2
4
6
8
10
12
14
16
18
20
Q. Write a program to print odd number from 1 to 10.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;i+=2)
{
cout<<”\n”<<i;
}
getch();
}
Output
1
3
5
7
9
Q. Find output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=5;i<=50;i+=5)
{
cout<<”\n”<<i;
}
getch();
}
Output
5
10
15
20
25
30
35
40
45
50
Q.Find output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=50;i>=5;i-=5)
{
cout<<”\n”<<i;
}
getch();
}
Output
50
45
40
35
30
25
20
15
10
5
Q.Write a program to print your name up to specified term.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<”Enter terms”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<”\n AIPS ACADEMY”;
}
getch();
}
Output
Enter terms=4
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Q.Write a program to print numbers from 1 to specified term
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<”Enter terms”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<”\n”<<i;
}
getch();
}
Output
Enter terms=5
1
2
3
4
5
Q.Write a program to print numbers from specified term to 1.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<”Enter terms”;
cin>>n;
for(i=n;i>=1;i--)
{
cout<<”\n”<<i;
}
getch();
}
Output
Enter terms=5
5
4
3
2
1
Q.Write a program to print table of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j;
cout<<”Enter any number=”;
cin>>n;
for(i=1;i<=10;i++)
{
j=n*i;
cout<<”\n”<<j;
}
getch();
}
Output
Enter any number =5
5
10
15
20
25
30
35
40
45
50
Q.Write a program to print table of input number in following format.
Enter any number=5
5*1=5
5*2=10
…………..
……………
5*10=50
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j;
cout<<”Enter any number=”;
cin>>n;
for(i=1;i<=10;i++)
{
j=n*i;
cout<<”\n”<<n<<”*”<<i<<”=”<<j;
}
getch();
}
Output
Enter any number=5
5*1=5
5*2=10
…………..
……………
5*10=50
Q.Write a program to print Factorial of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j=1;
cout<<”Enter any number=”;
cin>>n;
for(i=n;i>=1;i--)
{
j=j*i;
}
cout<<”Factorial=”<<j;
getch();
}
Output
Enter any number=5
Factorial=120
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j=1;
cout<<”Enter any number=”;
cin>>n;
for(i=n;i>=1;i--)
{
j=j*i;
cout<<i<<”*”;
}
cout<<”=”<<j;
getch();
}
Output
Enter any number=5
5*4*3*2*1*=120
Q.Write a program to find factor of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<”Enter any number=”;
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<”\n”<<i;
}
}
getch();
}
Output
Enter any number =6
1
2
3
6
Q.Write a program to find sum of factors of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,s=0;
cout<<”Enter any number=”;
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<”\n”<<i;
s=s+i;
}
}
cout<<”sum of factor=”<<s;
getch();
}
Output
Enter any number =6
1
2
3
6
sum of factors=12
Q.Write a program to check number is perfect or not.
(sum of factors =two times of number)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,s=0;
cout<<”Enter any number=”;
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
s=s+i;
}
}
if(s==n*2)
cout<<”Perfect number”;
else
cout<<”Not perfect number”;
getch();
}
Output
Enter any number=6
Perfect number
Q.Find output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=65;i<=90;i++)
{
cout<<i <<"\t";
}
getch();
}
Output
65 66 67 68 69 70 ……..90
Q.Write a program to print uppercase alphabet using ASCII Code.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=65;i<=90;i++)
{
cout<<(char)i;
}
getch();
}
Output
A B C D E F G H……….Z
Q.Write a program to print uppercase alphabet in reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=90;i>=65;i--)
{
cout<<(char)i;
}
getch();
}
Output
Z Y X W ………………..A
Q. Find Output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=90;i>=65;i--)
{
cout<<i;
}
getch();
}
90 89 88 87…..65
Q.Write a program to print lowercase alphabet using ASCII Code.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=97;i<=122;i++)
{
cout<<(char)i;
}
getch();
}
Output
a b c d e …………………..z
Q.Find output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=97;i<=122;i++)
{
cout<<i;
}
getch();
}
Output
97 98 99 100…….122
Q.Write a program to print lowercase alphabet in reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=122;i>=97;i--)
{
cout<<(char)i;
}
getch();
}
Output
z y x w ………………..a
Q.Write a program to print numbers from 0 to 9 using ASCII code.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=48;i<=57;i++)
{
cout<<(char)i;
}
getch();
}
Output
0 1 2 3 4 5 6………………..9
Q.Find output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=48;i<=57;i++)
{
cout<<i;
}
getch();
}
Output
48 49 50 51 52 53 ……..57
Q.Write a program to print following series.
1 2 3 4 5 ……………n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<”Enter value of n=”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<i<<”\t”;
}
getch();
}
Output
Enter value of n=5
1 2 3 4 5
Q.Write a program to print following series and sum also.
1 2 3 4 5 ……………n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,s=0;
cout<<”Enter value of n=”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<i<<”\t”;
s=s+i;
}
cout<<”\nsum of series=”<<s;
getch();
}
Output
Enter value of n=5
1 2 3 4 5
sum of series=15
While loop:-
This loop is an entry control loop. It executes the block of statement as long as the certain condition is satisfied.
Control exits from loop when the specified condition is false.
Syntax:
while(test condition)
{
body of the loop;
}
Q.Write a program to print your name five times using while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
while(i<=5)
{
cout<<”\n AIPS ACADEMY”;
i++;
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=5;
while(i>=1)
{
cout<<”\n AIPS ACADEMY”;
i--;
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Q.Write a program to print reverse of digits of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r,s=0;
cout<<”Enter value of n=”;
cin>>n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
cout<<”Reverse of digits=”<<s;
getch();
}
Output
Enter any number=123
Reverse=321
Q.Write a program to print sum of digits of of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r,s=0;
cout<<”Enter value of n=”;
cin>>n;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
cout<<”sum of digits=”<<s;
getch();
}
Output
Enter any number=123
sum of digits =6
Q.Write a program to print multiplication of digits of of input number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r,s=1;
cout<<”Enter value of n=”;
cin>>n;
while(n>0)
{
r=n%10;
s=s*r;
n=n/10;
}
cout<<”Multiplication of digits=”<<s;
getch();
}
Output
Enter any number=423
Multiplication of digits =24
Q.Write a program to check to number is palindrome or not.
(palindrome means same number and reverse)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r,s=0,t;
cout<<”Enter value of n=”;
cin>>n;
t=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==t)
cout<<”Palindrome number”;
else
cout<<”Not palindrome number”;
getch();
}
Output
Enter any number=121
Palindrome number
Do- while loop:-
This loop executes its body at least once whether the specified condition is true or false.
This loop executes its body first then checks the test condition.
Syntax:
do
{
body of the loop;
}while(test condition);
Q. Write a program to print your name five times using do while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
do
{
cout<<”\n AIPS ACADEMY”;
i++;
} while(i<=5);
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=5;
do
{
cout<<”\n AIPS ACADEMY”;
i--;
} while(i>=1);
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Q.Write a program to print numbers from 1 to 5 using do while.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
do
{
cout<<”\n”<<i;
i++;
} while(i<=5);
getch();
}
Output
1
2
3
4
5
Q.Write a program to print numbers from 5 to 1 using do while.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=5;
do
{
cout<<”\n”<<i;
i--;
} while(i>=1);
getch();
}
Output
5
4
3
2
1
Nested Loop in C++
one loop inside another loop is called nested loop. It is used to print data in tabular form.
example:
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cout<<“AIPS \t”;
}
cout<<“\n”;
}
here j is nested or inner loop and i is outer loop.
outer loop for row and inner loop for column
Q.Write a program to print following format.
AIPS AIPS AIPS AIPS
AIPS AIPS AIPS AIPS
AIPS AIPS AIPS AIPS
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=4;j++)
{
cout<<”AIPS \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
cout<<j<<”\t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
cout<<i<<”\t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
AIPS
AIPS AIPS
AIPS AIPS AIPS
AIPS AIPS AIPS AIPS
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<”AIPS \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
1
1 2
1 2 3
1 2 3 4
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<j<<” \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
1
2 2
3 3 3
4 4 4 4
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
cout<<i<<” \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
A B C D E
A B C D E
A B C D E
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=65;i<=67;i++)
{
for(j=65;j<=69;j++)
{
cout<<(char)j<<” \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
A A A A A
B B B B B
C C C C C
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=65;i<=67;i++)
{
for(j=65;j<=69;j++)
{
cout<<(char)i<<” \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
A
A B
A B C
A B C D
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=65;i<=68;i++)
{
for(j=65;j<=i;j++)
{
cout<<(char)j<<” \t”;
}
cout<<”\n”;
}
getch();
}
Q.Write a program to print following format.
A
B B
C C C
D D D D
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=65;i<=68;i++)
{
for(j=65;j<=i;j++)
{
cout<<(char)i<<” \t”;
}
cout<<”\n”;
}
getch();
}
to be continued…