Loops :
Loop is defined as a block of statements and it is executed repeatedly until the condition becomes false.
(or)
A loop is defines as a block of statements which are executed repeatedly for certain number of times.
Steps involved in a loop:
1) Loop variable: Declaring a variable which is used in the loop.
2) Initialization: it is the first step in which starting and final value is assigned to the loop variable .each time the updated value is checked by it self.
3) Test condition: Condition which is to be checked until it becomes false.
4) Iteration (Incrementation/Decrementation): It is a Numerical value either it is increased or decreased the value of the variable.
C supports 3 types of loops.
They are:
- While loop
- for loop
- do while loop
While loop
|
For loop
|
Do while loop
|
1.While is an entry control loop.
2. In while loop statements are executed after the test condition is checked.
|
1. For is an entry control loop.
2.In for loop statements are executed after the test condition is checked.
3.In while loop Initialization, test condition and iteration parts are placed in different places where as in for loop Initialization, test condition and iteration parts are placed in single statement.
4.some time forgetting Initialization, or iteration parts are in while loop where as in for loop there is no chance to forgetting.
|
1. Do while is an exit control loop.
2. In do while loop the test condition is checked after statements are executed once.
3.do while loops are exit control loops as the condition checks at end even though the condition is false but do while loops are executed at once.
4.do while loops are used to menu given programmes.
|
/*1.write a program to demonstrate while loop. */
|
/*1.write a program to demonstrate for loop.*/
|
/*1.write a program to demonstrate do-while loop.*/
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
i=1;
while(i<=5)
{
cout<<“\nc++”;
i++;
}
getch( );
}
Result:
Output:
c++
c++
c++
c++
c++
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
for(i=1;i<=5;i++)
{
cout<<“\nc++”;
}
getch( );
}
Result:
Output:
c++
c++
c++
c++
c++
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
i=1;
do
{
cout<<“\nc++”;
i++;
}
while(i<=5);
getch( );
}
Result:
Output:
c++
c++
c++
c++
c++
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
/*2.C++ program to display c-
language
n times using while
loop
*/
|
/*2.C++ program to display c-
language n times using for loop */
|
/*2.C++ program to display c-
language n times using do-
while loop.*/
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
i=1;
while(i<=n)
{
cout<<“\nc++”;
i++;
}
getch( );
}
Result:
input:
enter
the value of n:5
Output:
c++
c++
c++
c++
c++
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<“\nc++”;
}
getch( );
}
Result:
input:
enter
the value of n:5
Output:
c++
c++
c++
c++
c++
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
i=1;
do
{
cout<<“\nc++”;
i++;
}
while(i<=n);
getch( );
}
Result:
input:
enter
the value of n:5
Output:
c++
c++
c++
c++
c++
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
i=1
1<=5(T)
display c++
i=2
2<=5(T)
display c++
i=3
3<=5(T)
display c++
i=4
4<=5(T)
display c++
i=5
5<=5(T)
display c++
i=6
6<=5(F)
Exit from the loop
|
/*3.C
program to display 'n'
Natural
Numbers using while
loop
*/
|
/*3.C program to display 'n'
Natural Numbers using for loop
*/
|
/*3.C program to display 'n'
Natural Numbers using do-
while loop.*/
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
i=1;
while(i<=n)
{
cout<<“\t”<<i;
i++;
}
getch( );
}
Result:
input:
enter
the value of n:5
Output:
1 2
3 4 5
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<“\t”<<i;
}
getch( );
}
Result:
input:
enter
the value of n:5
Output:
1 2
3 4 5
|
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr(
);
cout<<“enter
the value of n:”;
cin>>n;
i=1;
do
{
cout<<“\t”<<i;
i++;
}
while(i<=n);
getch( );
}
Result:
input:
enter
the value of n:5
Output:
1 2
3 4 5
|
i=1
1<=5(T)
display 1
i=2
2<=5(T)
display 2
i=3
3<=5(T)
display 3
i=4
4<=5(T)
display 4
i=5
5<=5(T)
display 5
i=6
6<=5(F)
Exit from the loop
|
i=1
1<=5(T)
display 1
i=2
2<=5(T)
display 2
i=3
3<=5(T)
display 3
i=4
4<=5(T)
display 4
i=5
5<=5(T)
display 5
i=6
6<=5(F)
Exit from the loop
|
i=1
display 1
i=2
2<=5(T)
display 2
i=3
3<=5(T)
display 3
i=4
4<=5(T)
display 4
i=5
5<=5(T)
display 5
i=6
6<=5(F)
Exit from the loop
|
/*.C++ program to find the
given
number is prime no or
not,using
while loop */
|
/*.C++ program to find the
given
number is prime no or
not,using
for loop */
|
/*.C++ program to find the
given
number is prime no or
not,using
do-while loop */
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,count;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
count=0;
i=1;
while(i<=n)
{
if(n%i==0)
{
cout<<"\n”<<i;
}
i++;
}
if(count==2)
{
cout<<"\n
”<<n<<”is prime”;
}
else
{
cout<<"\n”<<n<<”
is not prime”;
}
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,count;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<"\n”<<i;
}
}
if(count==2)
{
cout<<"\n
”<<n<<”is prime”;
}
else
{
cout<<"\n”<<n<<”
is not prime”;
}
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,count;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
count=0;
i=1;
do
{
if(n%i==0)
{
cout<<"\n”<<i;
}
i++;
}
while(i<=n);
if(count==2)
{
cout<<"\n
”<<n<<”is prime”;
}
else
{
cout<<"\n”<<n<<”
is not prime”;
}
getch( );
}
|
Result:
input:
enter
the value of n:5
Output:
1
5
no.of
factors=2
5 is
prime
|
Result:
input:
enter
the value of n:5
Output:
1
5
no.of
factors=2
5 is
prime
|
Result:
input:
enter
the value of n:5
Output:
1
5
no.of
factors=2
5 is
prime
|
Step 1:
i=1 ,
count=0
1<=5(T)
If(5%1==0)
(T)
display
1
count=1;
Step 2:
i=2 ,
count=1
2<=5(T)
If(5%2==0)
(F)
Exit condition
Step 3:
i=3 ,
count=1
3<=5(T)
If(5%3==0)
(F)
Exit condition
Step 4:
i=4 ,
count=1
4<=5(T)
If(5%4==0)
(F)
Exit condition
Step 5:
i=5 ,
count=1
5<=5(T)
If(5%5==0)
(T)
display
5
count=2;
i=6
6<=5(F)
Exit from the loop
The check :
If(count==2)i.e.
If(2==2) (T)
5 is prime
|
Step 1:
i=1 ,
count=0
1<=5(T)
If(5%1==0)
(T)
display
1
count=1;
Step 2:
i=2 ,
count=1
2<=5(T)
If(5%2==0)
(F)
Exit condition
Step 3:
i=3 ,
count=1
3<=5(T)
If(5%3==0)
(F)
Exit condition
Step 4:
i=4 ,
count=1
4<=5(T)
If(5%4==0)
(F)
Exit condition
Step 5:
i=5 ,
count=1
5<=5(T)
If(5%5==0)
(T)
display
5
count=2;
i=6
6<=5(F)
Exit from the loop
The check :
If(count==2)i.e.
If(2==2) (T)
5 is prime
|
Step 1:
i=1 ,
count=0
1<=5(T)
If(5%1==0)
(T)
display
1
count=1;
Step 2:
i=2 ,
count=1
2<=5(T)
If(5%2==0)
(F)
Exit condition
Step 3:
i=3 ,
count=1
3<=5(T)
If(5%3==0)
(F)
Exit condition
Step 4:
i=4 ,
count=1
4<=5(T)
If(5%4==0)
(F)
Exit condition
Step 5:
i=5 ,
count=1
5<=5(T)
If(5%5==0)
(T)
display
5
count=2;
i=6
6<=5(F)
Exit from the loop
The check :
If(count==2)i.e.
If(2==2) (T)
5 is prime
|
Perfect Number:
Sum of the factors of the given number is equals to twice
of a given number is called Perfect Number.
/*.C++ program to find the
given
number is perfect no or
not,using
while loop */
|
/*.C++ program to find the
given
number is perfect no or
not,
using for loop */
|
/*.C++ program to find the
given
number is perfect no or
not,using
do-while loop */
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,sum;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
sum=0;
i=1;
while(i<=n)
{
if(n%i==0)
{
cout<<"\n”<<i;
sum=sum+i;
}
i++;
}
cout<<"\nsum
of factors=“<<sum;
if(sum==2*n)
{
cout<<"\n”
<<n<<” is perfect Number”;
}
else
{
cout<<"\n”<<n<<”
is not perfect Number “;
}
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,sum;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
sum=0;
for(i=1;
i<=n;i++)
{
if(n%i==0)
{
cout<<"\n”<<i;
sum=sum+i;
}
}
cout<<"\nsum
of factors=“<<sum;
if(sum==2*n)
{
cout<<"\n”
<<n <<” is perfect Number”;
}
else
{
cout<<"\n”<<n<<”
is not perfect Number “;
}
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,n,sum;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
sum=0;
i=1;
do
{
if(n%i==0)
{
cout<<"\n”<<i;
sum=sum+i;
}
i++;
}
while(i<=n;
cout<<"\nsum
of factors=“<<sum;
if(sum==2*n)
{
cout<<"\n”
<<n <<” is perfect Number”;
}
else
{
cout<<"\n”<<n<<”
is not perfect Number “;
}
getch( );
}
|
Result:
input:
enter
the value of n:6
Output:
1
2
3
6
sum
of factors=12
6 is
perfect Number
|
Result:
input:
enter
the value of n:6
Output:
1
2
3
6
sum
of factors=12
6 is
perfect Number
|
Result:
input:
enter
the value of n:6
Output:
1
2
3
6
sum
of factors=12
6 is perfect
Number
|
Step 1:
i=1 ,
sum=0
1<=6(T)
If(6%1==0)
(T)
display
1
sum=1;
Step 2:
i=2 ,
sum=1
2<=6(T)
If(6%2==0)
(T)
display
2
sum=3;
Step 3:
i=3 ,
sum=3
3<=6(T)
If(6%3==0)
(T)
display
3
sum=6;
Step 4:
i=4 ,
sum=6
4<=6(T)
If(6%4==0)
(F)
Exit condition
Step 5:
i=5 ,
sum=6
5<=6(T)
If(6%5==0)
(T)
Exit condition
Step 6:
i=6 ,
sum=6
6<=6(T)
If(6%6==0)
(T)
display
6
sum=12;
Step 7:
i=7,
sum=12
7<=7(F)
Exit from the loop
The check :
If(sum==2*n)i.e.
If(12==12) (T)
12 is perfect
|
Step 1:
i=1 ,
sum=0
1<=6(T)
If(6%1==0)
(T)
display
1
sum=1;
Step 2:
i=2 ,
sum=1
2<=6(T)
If(6%2==0)
(T)
display
2
sum=3;
Step 3:
i=3 ,
sum=3
3<=6(T)
If(6%3==0)
(T)
display
3
sum=6;
Step 4:
i=4 ,
sum=6
4<=6(T)
If(6%4==0)
(F)
Exit condition
Step 5:
i=5 ,
sum=6
5<=6(T)
If(6%5==0)
(T)
Exit condition
Step 6:
i=6 ,
sum=6
6<=6(T)
If(6%6==0)
(T)
display
6
sum=12;
Step 7:
i=7,
sum=12
7<=7(F)
Exit from the loop
The check :
If(sum==2*n)i.e.
If(12==12) (T)
12 is perfect
|
Step 1:
i=1 ,
sum=0
1<=6(T)
If(6%1==0)
(T)
display
1
sum=1;
Step 2:
i=2 ,
sum=1
2<=6(T)
If(6%2==0)
(T)
display
2
sum=3;
Step 3:
i=3 ,
sum=3
3<=6(T)
If(6%3==0)
(T)
display
3
sum=6;
Step 4:
i=4 ,
sum=6
4<=6(T)
If(6%4==0)
(F)
Exit condition
Step 5:
i=5 ,
sum=6
5<=6(T)
If(6%5==0)
(T)
Exit condition
Step 6:
i=6 ,
sum=6
6<=6(T)
If(6%6==0)
(T)
display
6
sum=12;
Step 7:
i=7,
sum=12
7<=7(F)
Exit from the loop
The check :
If(sum==2*n)i.e.
If(12==12) (T)
12 is perfect
|
/*.C++
program to find the
Sum
of the individual digits of
positive
Integerusing while loop */
|
/*.C++
program to find the
Sum
of the individual digits of
positive
Integerusing for loop */
|
#include<iostream.h>
#include<conio.h>
void main()
{
int n,r,sum;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
sum=0;
while(n>0)
//while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
cout<<"\nsum
of the individual digits of a given Number=“<<sum;
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int n,r,sum;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
for(sum=0;n>0;)
{
r=n%10;
sum=sum+r;
n=n/10;
}
cout<<"\nsum
of the individual digits of a given Number=“<<sum;
getch( );
}
|
Result:
input:
enter
the value of n:123
Output:
Sum=6
|
Result:
input:
enter
the value of n:123
Output:
Sum=6
|
Step 1:
n=123
, sum=0
123>0(T)
r=123%10=3
sum=sum+r i.e.
sum=0+3=3;
n=n/10 i.e
n=123/10=12
Step 2:
n=12
, sum=3
12>0(T)
r=12%10=2
sum=sum+r i.e.
sum=3+2=5;
n=n/10 i.e
n=12/10=1
Step 3:
n=1 ,
sum=5
1>0(T)
r=1%10=1
sum=sum+r i.e.
sum=5+1=6;
n=n/10 i.e
n=1/10=0
Step 4:
n=0 ,
sum=6
0>0(F)
Exit from the loop
Display sum=6
|
Step 1:
n=123
, sum=0
123>0(T)
r=123%10=3
sum=sum+r i.e.
sum=0+3=3;
n=n/10 i.e
n=123/10=12
Step 2:
n=12
, sum=3
12>0(T)
r=12%10=2
sum=sum+r i.e.
sum=3+2=5;
n=n/10 i.e
n=12/10=1
Step 3:
n=1 ,
sum=5
1>0(T)
r=1%10=1
sum=sum+r i.e.
sum=5+1=6;
n=n/10 i.e
n=1/10=0
Step 4:
n=0 ,
sum=6
0>0(F)
Exit from the loop
Display sum=6
|
Armstrong Number:
Sum of the cube of the individual digits of a given
number equals to given number is called
Armstrong Number.
/*.C++
program to find the
Given
number is Arm strong no
or
not,using while loop */
|
/*.C++
program to find the
Given
number is Arm strong no
or
not,using for loop */
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
r,n,sum,temp;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
temp=n;
sum=0;
while(n>0)
//while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
cout<<"\nsum=“<<sum;
if(sum==temp)
{
cout<<"\n”<<temp<<”
is Armstrong”;
}
else
{
cout<<"\n”<<temp<<”
is not Armstrong”;
}
getch( );
}
|
#include<iostream.h>
#include<conio.h>
void main()
{
int r,n,sum,temp;
clrscr(
);
cout<<"enter
the value of n:";
cin>>n;
temp=n;
for(sum=0;n>0;)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
cout<<"\nsum=“<<sum;
if(sum==temp)
{
cout<<"\n”<<temp<<”
is Armstrong”;
}
else
{
cout<<"\n”<<temp<<”
is not Armstrong”;
}
getch( );
}
|
Result:
input:
enter
the value of n:153
Output:
153 is Armstrong
|
Result:
input:
enter
the value of n:153
Output:
153 is Armstrong
|
Step 1:
n=153
, sum=0
153>0(T)
r=153%10=3
sum=sum+r*r*r i.e.
sum=0+3*3*3=27;
n=n/10 i.e.
n=153/10=12
Step 2:
n=15
, sum=27
15>0(T)
r=15%10=5
sum=sum+r*r*r i.e.
sum=27+125=152;
n=n/10 i.e.
n=15/10=1
Step 3:
n=1 ,
sum=152
1>0(T)
r=1%10=1
sum=sum+r*r*r i.e.
sum=152+1=153;
n=n/10 i.e.
n=1/10=0
Step 4:
n=0 ,
sum=153
0>0(F)
Exit from the loop
The check :
If(sum==temp)i.e.
If(153==153) (T)
153 is Armstrong
|
Step 1:
n=153
, sum=0
153>0(T)
r=153%10=3
sum=sum+r*r*r i.e.
sum=0+3*3*3=27;
n=n/10 i.e.
n=153/10=12
Step 2:
n=15
, sum=27
15>0(T)
r=15%10=5
sum=sum+r*r*r i.e.
sum=27+125=152;
n=n/10 i.e.
n=15/10=1
Step 3:
n=1 ,
sum=152
1>0(T)
r=1%10=1
sum=sum+r*r*r i.e.
sum=152+1=153;
n=n/10 i.e.
n=1/10=0
Step 4:
n=0 ,
sum=153
0>0(F)
Exit from the loop
The check :
If(sum==temp)i.e.
If(153==153) (T)
153 is Armstrong
|
Nested loops:
Loop inside another loop is known as nested loop.
Nested for loops:
for loop inside another for loop is known as nested loop.
/*.C++
program to display Prime
no’s upto x Natural numbers
*/
|
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,x,n,count;
clrscr(
);
cout<<"enter
the value of x:";
cin>>x;
cout<<"\nPrime
no's upto Natural Numbers:”<<x;
for(n=1;n<=x;n++)
{
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
cout<<"\t”<<n;
}
}
getch( );
}
|
|
Result:
input:
enter
the value of x:100
Output:
Prime
no's upto 100 Natural Numbers:
2
3 5 7
11
13 17
19 23 29
31 37 41
43 47
53 59
61 67 71
73 79 83
89 97
|
/*.C++
program to display
Perfect
no’s upto x Natural
Numbers
*/
|
|
#include<iostream.h>
#include<conio.h>
void main()
{
int
i,x,n,sum;
clrscr(
);
cout<<"enter
the value of x:";
cin>>x;
cout<<"\nPerfect
no's upto Natural Numbers:”<<x;
for(n=1;n<=x;n++)
{
sum=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==2*n)
{
cout<<"\t”<<n;
}
}
getch( );
}
|
|
Result:
input:
enter
the value of x:100
Output:
Perfect
no's upto 100 Natural Numbers: 6 28
|
Result:
input:
enter
the value of x:100
Output:
Perfect
no's upto 100 Natural Numbers: 6 28
|
No comments:
Post a Comment