Lecture 5
Lecture 5
E N
ME
AT
ST
O P
LO LECTURE #5
LOOPS
A statement or set of statements that is executed
repeatedly called loop.
Increment or decrement
#include<iostream. h>
#include<conio. h> #include<iostream. h>
#include<conio. h>
Void main() Void main()
{
{ Int c;
c=1;
Int c; For( ; c<=10;)
For(c=1; c<=10; C++) {
Cout<< c<<“\n”;
Cout<< c “\n”; C++;
}
getch(); getch();
} }
Flow Chart of “For” LOOP
false
Condition exit
True
Body of loop
Increment
expression
Write a program to calculate odd numbers from 1 to
10.
#include<iostream. h>
#include<conio. h>
Void main()
Int n;
Cout<<n<<endl;
Getch();
}
Write a program to calculate the sum of odd numbers from 1 to 10
and then print sum on the screen.
#include<iostream. h>
#include<conio. h>
Void main()
{
Int n , sum;
Sum=0;
For(n=1; n<=10; n+=2)
{
Sum =sum + n;
Cout<<n<<endl;
}
Cout<<“Sum =”<<sum<<endl;
getch();
}
Assignment-1
Write a program to display the even number from 1
to 100 using for loop.
Assignment-2
Write a program to display the even number from
100 to 1 using for loop.
Assignment-3
Write a program to display the following sequence
0,5,10,20,25,30,35,40
PROGRAM
Write a program to display the following sequence.
0.5, 1, 1.5, 2, 2.5, 3
#include<iostream. h>
#include<conio. h>
Void main()
{
float n;
For(n=0.5; n<=3; n+=0.5)
{
Cout<<n<<endl;
}
getch();
Write a program to calculate the factorial of any positive
number.
(n*(n-1)*(n-2)*……..-1)
#include<iostream. h>
#include<conio. h>
Void main()
{
Int n, F=1;
Cout<<“Enter any positive number”;
Cin>>n;
For(int c=1;c<=n; c++)
F*=c;
Cout<<n<<“factorial is ”<<f;
getch();
}
“WHILE” LOOP
It is a conditional loop statement used to execute a
statement or a set of statement as long as he given
condition remains true.
While(Condition)
Statements;
While(Condition)
{
Statement 1;
Statement 2;
Statement n;
}
When the while loop statement is executed, the
condition is evaluated first.
false
Condition
True
Body of loop
#include<iostream. h>
#include<conio. h>
Void main()
{
Int n, sum;
Sum=0;
n=0
while( n<=10)
{
sum =sum + n;
Cout<<n<<endl;
n=n+2;
}
Cout<<“Sum =”<<sum<<endl;
getch();
}
Write a program to calculate factorial using while loop.
#include<iostream. h>
#include<conio. h>
Void main()
{
Int fact,n,c;
Cout<<“Enter a positive number”;
Cin>>n;
Fact=1;
While(n>=1)
{
Fact=fact*n;
n=n-1;
}
Cout<<factorial=“<<fact;
getch();
}
Write a program to find the sum of the following series:-
1+1/2+1/3+1/4………..1/45
#include<iostream. h>
#include<conio. h>
Void main()
{
Float s,c;
S=0.0;
C=1;
While(c<=45)
{
S=s+1.0/c;
c++;
}
Cout<<“sum of series=”<<s;
getch();
}
Write a program to print the number from 1 to 10 in
descending order.
#include<iostream. h>
#include<conio. h>
Void main()
{
Int n=10;
While(n!=0)
Cout<<n--<<endl;
Getch();
}
THE “DO-WHILE” LOOP
The “do-while” is condition loop statement.
It is same like “While” loop ,but in this case the condition
is tested after the execution of the statement.
do
{
Statements;
}
While(condition);
do it is a keyword that indicates the starting of
do-while loop.
Body of loop
True
Condition
false
For(j=1;j<=2;j++)
For(k=1;k<=2;k++)
Cout<<i<<“\t”<<j<<“\t”<<k;
cout<<endl;
getch();
}
#include<iostream. h>
Write a program to print the output
as shown below: #include<conio.h>
CS Void main()
CS CS
CS CS CS {
CS CS CS CS
CS CS CS CS
Int a, b;
CS For(a=1;a<=5; a++)
{
For (b=1;b<=a; b++)
Cout<<“CS”;
Cout<<endl;
}
getch();
}
CALCULATE LUCAS SEQUENCE
1 3 4 7 11 18 29
#include<iostream. h> a=b;
#include<conio.h> b=c;
Void main() C=a + b;
{ }
Int a, b, c; getch();
a=1; }
b=3;
Cout<<a<<“\t”<<b;
C=a + b;
While(c<=29)
{
FABONACI SERIES
#include<iostream. h> Cout<<third<<“\t”;
#include<conio.h> First = second;
Void main() Second=third;
{ }
Int first =0, second = 1,third; getch();
Int n , c; }
Clrscr();
Cout<<“order the fabonacci series”;
Cin>>n;
Cout<<“\n fabonacci series
is”<<endl;
Cout<<first <<“\t”<<second<<“\t”;
For(c=0;c<n-2;c++)
{
#include<iostream. h>
Write a program to print the output as
shown below: #include<conio.h>
5 4 3 2 1 Void main()
4 3 2 1 {
3 2 1
2 1 Int a, b, c, space =40;
1 C =5;
do
{
For(a=1;a<=space; a++)
Cout<<“ ”;
For(b=c; b>=1; b--)
Cout<<b<<“ ”;
Cout<<endl;
Space+=2;
N--;
}
While(c>=1);
getch();
WRITE A PROGRAM TO
CALCULATE PRIME NUMBER
#include<iostream. h> else
#include<conio.h> {
Void main()
c++;
{
Int n, c=2, r;
Continue;
Cout<<“Enter any number”; }
Cin>>n; }
While(c<n) Cout<<n<< “is a prime
{ number”;
r=n % c; getch();
If(r= =0)
}
{
Cout<<n<< “is a composite number”;
exit(0);
}
CONTINUE AND EXIT() STATEMENT
• The break statement immediately ends the looping process.
It transfer the control the closing braces of the most current
loop.
• Transfer the control at the top of the loop for next iteration.