AIM POINT INFOSYSTEM PVT. LTD.
2019
Loop
Definition : it is a block containing some statements and depends on the condition and
that block continue executed until condition is false (or til condition is true)
Structure Of Loop
if( condition )
loop( condition ) {
{ -----------
----------- -----------
----------- }
-----------
}
Difference between “if” and loop : the block of “if” statement executed only once
and the block of “loop” get executed no of times (depend on the condition) .
Type of loop :
While
do-while
for.
Step of working in loop.
Initialization of loop index or loop variable.
loop variable:- a variable which we use in the condition is called loop
variable
Condition of loop
Increment / decrement in loop variable
“while” loop :
Syntax
initialization of loopVar
while ( condition )
{ -----------------
----------------
Incre / decre
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 1
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to print Number from 1 to 100
#include<stdio.h>
void main( )
{ int i = 1; //initialization
while( i<= 100) //condition
{ printf(" %d", i );
i++; //increment
}
}
Some Special cases of while loop
Case 1 :
void main( )
{ int i ; //no initialization
while( i <= 100)
{
printf(" %d", i );
i++;
}
}
Note : If we not initialize loop variable then compiler will take garbage value ( pre
stored value of memory ) in this variable and this loop will be uncontrolled
Case 2 :
void main( )
{ int i=1;
while(i<=100)
{ printf(" %d", i );
// no increment
}
}
Note : If we ignore increment / decrement in loop variable then this loop will be
infinite
Case 3:
void main( )
{ int i=1; // initialization
while( i<=100 ) ;
{
printf( " %d" , i ) ; // infinite loop
i++; // increment of loop variable
}
}
Note : If we put a semicolon ( ; ) after the condition in while loop then this loop
will be infinite
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 2
AIM POINT INFOSYSTEM PVT. LTD. 2019
#WAP to display the table of any number(1)
void main( ) #WAP to display the table of any number(2)
{ int i=1,no; #include<stdio.h>
printf ("\nEnter a number:"); void main( )
scanf ("%d",&no); { int i=1,no;
while ( i <= 10 ) printf ("\nEnter a number:");
{ printf("\n %d” , no * i ); scanf ("%d",&no);
i ++ ; while (i<=10)
} printf("\n %d” , no * i++ );
} }
//WAP to Calculate Factorial of no
void main( )
{ int no , i = 1 ;
long int f = 1 ;
printf("enter any no");
scanf("%d",&no);
while( i <= n )
f = f * i ++ ; // or f *= i ++ ;
printf("\n Factorial =%ld" , f );
}
//WAP to Calculate Factorial of no
void main( )
{ int no ;
long int f=1;
printf("enter any no");
scanf("%d",&no);
while( no > 1 )
f = f * no - - ;
printf("\n Factorial =%ld",f);
}
// WAP to calculate X^Y using while loop
void main()
{ int x , y ;
long int p = 1 ;
printf("\nEnter X and Y “);
scanf(“%d%d” , &x ,&y );
while( y>0 )
{
p = p * x;
y--;
}
printf("\n Power = %ld “, p ) ;
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 3
AIM POINT INFOSYSTEM PVT. LTD. 2019
// WAP to enter 10 nos in a loop and find out sum and average
void main()
{ int no, i= 1 , sum = 0 ;
printf("\nEnter 10 nos “);
while( i <= 10 )
{ scanf ( "%d" , &no);
sum = sum + no ;
i++;
}
printf("\n Sum = %d “, sum );
printff(“\n Average =%f” , sum / 10.0 );
}
// WAP to enter 10 nos in a loop and find out sum of all even nos and sum of all
odd nos
void main( )
{ int no, i= 1 , sume = 0,sumo=0 ;
printf("\nEnter 10 nos “);
while( i < = 10 )
{ scanf ( "%d" , &no);
if ( no % 2 == 0 )
sume = sume + no ;
else
sumo = sumo + no ;
i++;
}
printf("\n Sum Of Even Nos = %d “ , sume );
printff(“\n Sum Of Odd Nos =%d” , sumo );
}
// WAP to enter 10 nos in a loop and count no of all positive nos and no of all
negative nos
void main( )
{ int no, i= 1 , n = 0 , p=0 ;
printf("\nEnter 10 nos “);
while( i <= 10 )
{ scanf ( "%d" , &no);
if ( no>=0 )
p++;
else
n++;
i++;
}
printf("\n No of positive nos = %d “, p ) ;
printff(“\n No of negative nos =%d ” , n );
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 4
AIM POINT INFOSYSTEM PVT. LTD. 2019
// WAP to Find out biggest no and its position from 10 no’ s
#include<stdio.h>
void main()
{ int no , big = -32768 ;
int i=1 , pos=0 ;
while(i<=10)
{ printf("\nEnter %d number:",i);
scanf ( "%d" , &no);
if( no>big )
{
big = no ;
pos = i ;
}
i++;
}
printf("\nBiggest no=%d and position =%d" , big , pos);
}
Assignment : WAP to Find out smallest no and its position from 10 no’ s .
Assignment : WAP to Find out top three nos and their positions from 10 no’ s .
Logic:
void main( )
{ int no , i =1 , b1= -32768 , b2 = -32768, b3= -32768 , p1 ,p2 ,p3 ;
printf (“\n Enter 10 Nos “);
while( i <= 10 )
{ scanf ( "%d" , &no);
-------------
-------------
-------------
i++;
}
printf("\n B1= %d , P1 = %d “ , b1 ,p1 ) ;
printf("\n B1= %d , P1 = %d “ , b1 ,p1 ) ;
printf("\n B1= %d , P1 = %d “ , b1 ,p1 ) ;
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 5
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to print any no in reverse order and calculate sum of digits , no of digits
void main( )
{ int no, r , sum=0 , c =0 ;
printf("\nEnter any number:");
scanf("%d",&no);
while( no > 0 )
{ r = no % 10 ;
printf( “%d” , r );
sum = sum + r ;
c ++ ;
no = no / 10 ;
}
printf (" \n Sum of digits = %d “ , sum ) ;
printf ( “\n no of digits = %d " , c ) ;
}
Assignments :
1 ) WAP to update above program if user enter negative no .
Logic:
2 ) WAP to calculate any no in reverse in another variable .
Logic:
3 ) WAP to check number is palindrome or not
( Exam : 252 , 12321 , 454 )
Logic:
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 6
AIM POINT INFOSYSTEM PVT. LTD. 2019
4 ) WAP to print any number(int) in words
Example : 1 ) no = 2379
O/P : Two Three Seven Nine
2 ) no = 1300
O/P : One Three Zero Zero
Logic:
Armstrong Number :
1 ) it’s a three digit no ( 100 to 999 )
2 ) the sum of cube of every digit is equal to it self
Example : 153
//WAP to check number is “Armstrong” or not
#include<stdio.h>
void main()
{ int no , sum = 0 , r , x ;
printf("\nEnter number ");
scanf("%d", &no );
x = no ;
while(no>0)
{ r = no % 10 ;
sum = sum + ( r * r * r) ;
no = no / 10 ;
}
if( sum = = x )
printf("\n no is Armstrong " );
else
printf("\n no is not the Armstrong " );
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 7
AIM POINT INFOSYSTEM PVT. LTD. 2019
Assignments :
1 ) WAP to display all Armstrong Numbers between range ( Nested loop )
Logic
void main( )
{ int i = 100 , no, sum , r ;
while ( i <= 999)
{ no = i ;
while( check on no )
{ -------
-------
}
if( no is Armstrong )
printf("\n %d ", i );
i ++ ;
}
}
2 ) WAP to display all palindrome Numbers between range ( Nested loop )
Logic
void main( )
{ int lb , ub , no, rev , r ;
printf("\nEnter lb and ub : ");
scanf("%d%d" ,&lb , &ub );
while ( lb <= ub )
{ no=lb ;
while( check on no )
{ -------
-------
}
if( rev = = lb )
printf("\n %d " , lb ) ;
lb ++ ;
}
}
“break “ : it’s a keyword and control statement , which is used to terminated current
loop / “switch” statement
// WAP to demonstrate working of “break”
void main( )
{ int i = 1;
while( I <= 10 )
{ printf(" %d", i );
If ( i = = 5 )
Break ;
i ++ ;
}
} o/p 1 2 3 4 5
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 8
AIM POINT INFOSYSTEM PVT. LTD. 2019
// Example of “break”
void main( )
{ printf ( \n Hello “);
break ; // error (misplaced “break “
printf (:\n Good Bye “);
}
“return “ : it’s a keyword and control statement which is used to return control / or value
from one function to another function
// Example of “return”
void main( )
{ printf ( \n Hello “);
return ;
printf (:\n Good Bye “);
}
O/P : Hello
Prime Number :
Logic :
//WAP to check Entered number is prime or not
void main( )
{ int no, i=2;
printf("\nEnter any number:");
scanf ( "%d" , &no ) ;
while( i <= no )
{ if( no % i = =0 )
{ printf(" \n Not The Prime No ");
return ;
}
i++;
}
printf(" \n Prime No ");
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 9
AIM POINT INFOSYSTEM PVT. LTD. 2019
Assignment :
//WAP to display all prime numbers between range ( nested loop )
//WAP to display all reverse numbers between range ( nested loop )
//WAP to display all Tables of numbers between range ( nested loop )
//WAP to print Fibonacci Series
Fibonacci series :
1 1 2 3 5 8 13 21 34 .. .. .. .. .. .. .. .. ..
// WAP to display elements of Fibonacci series up to ‘n’ terms
#include<stdio.h>
void main()
{ int n , x = 1 , y = 0 , t ;
int i=1;
printf("\nEnter no of terms : ");
scanf("%d" , &n);
while( i <= n )
{
t=x+y;
printf ( “ %d” , t ) ;
x = y ;
y = t ;
i ++ ;
}
}
Assignment :
// WAP to display elements of Fibonacci series between range .
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 10
AIM POINT INFOSYSTEM PVT. LTD. 2019
do . . . . while loop :
syntax :
initialization of loop index
do{
Statement 1 ;
Statement 2 ;
----------------
Incre / decre
} while ( condition ) ;
Note : do … while loop execute at least once , means it does not execute one
more time then “while” loop
Differences between “while” and “do . . .while” loop
1 ) void main( ) 2 ) void main( )
{ int x = 10 ; { int x = 10 ;
while ( x < 5 ) do {
{ printf ( “\ Hello “);
printf ( “\ Hello “); x++ ;
x++ ; } while ( x < 5 ) ;
}
} }
O/P : NO OUTPUT O/P : Hello ( Only
Once )
1 ) void main( ) 2 ) void main( )
{ int i = 0 ; { int i = 0 ;
while ( i < 10 ) do {
{ printf ( “ %d “ , i );
printf ( “ %d “ , i ); i++ ;
i++ ; }while( i<10 ) ;
} }
}
O/P : 0 TO 9
O/P : 0 TO 9
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 11
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to calculate X to the power Y using do . . . while loop ( without pow() fn )
void main()
{ int x , y ;
long int p = 1 ;
printf("\nEnter X and Y “);
scanf(“%d%d” , &x ,&y );
do
{
p = p * x;
y--;
} while ( y > 0 ) ;
printf("\n Power = %ld “, p ) ;
}
Assignments //Write any two “while” loop example using do . . .while loop .
“ for “ loop
Syntax :
for ( initialization ; condition ; incre / decre )
{
-------------------------
-------------------------
}
Example 1 :
for ( i = 0 ; i < 10 ; i ++ )
printf ( “ %d “, i ) ;
Example 2 :
for ( i = 0 ; i < 10 ; ++ i )
printf ( “ %d “, i ) ;
Example 3 :
for ( i = 0 ; i < 10 ; i ++ ) ;
printf ( “ %d “, i ) ;
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 12
AIM POINT INFOSYSTEM PVT. LTD. 2019
Note : every part of for loop is an optional part .
1 ) void main( ) 3 ) void main( )
{ int i ; { int i ;
for ( i = 0 ; i < 10 ; i ++ ) for ( ; ; i ++ )
printf ( “ %d “, i ) ; { if ( i = = 10 )
} break ;
printf ( “ %d “, i ) ;
2 ) void main( ) }
{ int i = 0 ; }
for ( ; i < 10 ; i ++ )
printf ( “ %d “, i ) ; 4 ) void main( )
} { int i ;
for ( ; ; )
Note : the output of all these 4 example will { if ( i = = 10 )
Be 0 …… 9 . break ;
printf ( “ %d “, i++ ) ;
}
}
Note : more then one initialization , more then one condition , more
then one increment / decrement is possible in a for loop .
1 ) void main( )
{ Output ( printf( ) get execute 10
int i , j ; times )
for ( i=0 , j = 20 ; i < 10 && j > 0 ; i++ , j- - ) 0 20
printf ( “ \n %d %d “ , i , j ); 1 19
} 2 18
3 17
---------
---------
2 ) void main( ) Output ( printf( ) get execute 20
{ times )
int i , j , k ;
for ( i=0 , j = 20 ; i < 10 || j > 0 ; i++ , j- - ) 0 20
printf ( “ \n %d %d “ , i , j ); 1 19
} 2 18
3 17
---------
---------
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 13
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to print following pyramid using for loop
#include<stdio.h>
void main()
{ int i , j , n ;
printf("\nEnter no of row:"); *
scanf("%d" ,&n); ***
for(i=0;i<no;i++) *****
{ for(j=0 ; j<n – i ; j++) *******
printf(" ");
for(j=0;j<2*i+1;j++)
printf("*");
printf("\n");
} }
//WAP to print following pyramid using for loop
#include<stdio.h>
void main()
{ int i , j , n ;
printf("\nEnter no of rows :");
scanf("%d",&n);
for(i=0;i<n;i++)
{ for( )
printf(" %c" , j );
printf("\n");
}
}
Assignment : WAP to print following pyramid using for loop
A
BA
CBA
DCBA
Assignment : WAP to print following pyramid using for loop
ABCDCBA
ABC CBA
AB BA
A A
Assignment : /WAP to print following pyramid using for loop
*
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 14
AIM POINT INFOSYSTEM PVT. LTD. 2019
**
***
****
Assignment : WAP to print following pyramid using for loop
*****
***
**
*
Assignment : WAP to print following pyramid using for loop
1010101
010101
10101
0101
101
01
1
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 15
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to Calculate Sum of Series
1 + X + X^2 + X^3 + X^4 + X^5 + - - - - - - N
#include<stdio.h>
#include<math.h>
void main()
{ int i , sum=0 , n , x ;
printf("\nEnter value of X and no of terms :") ;
scanf("%d%d",&x,&n) ;
for ( i=0 ; i<n ; i++ )
printf("\nSum of series = %d ",sum ) ;
}
//WAP to Calculate Sum of Series
1 + X + X^2/!2 + X^3/!3 + X^4/!4 + X^5/!5 + - - - - - - N
#include<stdio.h>
#include<math.h>
void main()
{ int i,sum=1,no,x ;
long int f=1;
printf("\nEnter value of X and no of terms :") ;
scanf("%d%d",&x,&n) ;
for( i=1 ; i<n ; i++ )
{
}
printf("\nSum of series=%f", sum ) ;
}
Assignment : WAP to Calculate Sum of Series
1 - X + X^2/!2 - X^3/!3 + X^4/!4 - X^5/!5 + - - - - - - N
Assignment : WAP to Calculate Sum of Series
1 + ( 1 + X) + ( 1 + X^2 ) / !2 + ( 1 + X^2 + X^3 ) / !3 + - - - - - N
“goto” statement :
“goto” is keyword and built - in control statement , which is used to shift control
from one statement to another statement ( to named level ) . it is very dangerous
statement that’s why we should avoid the use of “goto “ statement .
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 16
AIM POINT INFOSYSTEM PVT. LTD. 2019
//WAP to Calculate Average of 10 Numbers without using Loop
#include<stdio.h>
void main( )
{
int no,sum=0,i=1 ;
printf("\n enter %d no " , i ) ;
scanf("%d",&no) ;
sum+=no ;
i++ ;
if(i<=10)
printf("\n avg of nos = %f ", sum/10.0 ) ;
}
//Special Case 1 : goto with if
#include<stdio.h>
void main()
{
int x=10;
if(x<10)
{
printf("hello");
}
}
//Special Case 2 : goto with for Loop
#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d",i);
}
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 17