0% found this document useful (0 votes)
10 views

Lecture6 Loop

Control Statements In C

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture6 Loop

Control Statements In C

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

:Loop :

It is a block , containing some statements , depends


upon the condition and that block continue execute ,
until condition is false ( or till condition is true )

Note : the difference B/W “if” and “loop” is that only –


where the block of “if” statement executed only once
and the block of “loop” executed no of times .
Type of loop :
while .
do…while .
for .
Steps Of Working in loop .
1. Initialization of loop index or loop variable .
2. Condition of loop .
3. Incement /decrement in loop index .
:“while” loop :
Syntax
initialization of loop index

while ( condition )
{
Statement 1 ;
Statement 2 ;
----------------
Statement n ;
}
//WAP to print Counting from 1 to 100
#include<stdio.h>
void main()
{ int i=1;
while(i<=100)
{ printf(" %d",i);
i++;
}
}
Some Special cases of while loop
Case 1 :
void main() Case 3:
{ int i ; #include<stdio.h>
while(i<=100) void main()
{ int i=1;
{ printf(" %d",i);
while(i<=100) ;
i++; { printf(" %d",i);
} i++;
} }
Case 2 : }
#include<stdio.h> Note :
void main()
{int i=1;
while(i<=100)
{ printf(" %d",i);

}
}
#WAP to display the table of any number .
#include<stdio.h>
void main()
{ int i=1,no;
printf ("\nEnter a number:");
scanf ("%d", &no ) ;
while (i<=10)
{

}
}
#WAP to display the table of any number(1)
#include<stdio.h>
void main()
{ int i=1,no;
printf ("\nEnter a number:");
scanf ("%d",&no);
while (i<=10)
{ printf("\n %d x %d = %d”, );
i++;
}

}
//WAP to Calculate Factorial of no
#include<stdio.h>
void main()
{ int no , i= 1 ;
long int f= 1 ;
printf("enter any no");
scanf("%d",&no);
while(i<=no)

printf("\n Factorial =%ld",f);


}
//WAP to Calculate Factorial of no
#include<stdio.h>
void main()
{ int no ;
long int f=1;
printf("enter any no");
scanf("%d",&no);
while( )

printf("\n Factorial =%ld",f);


}
// WAP to enter 10 nos in a loop and find out sum and average
#include<stdio.h>
void main()
{ int no, i= 1 , sum = ;

printf("\nEnter 10 nos “);


while( i <= 10 )
{ scanf ( "%d" , &no);

i++;
}
printf("\n Sum = %d “, );
printff(“\n Average =%f” , );
}
// WAP to enter 10 nos in a loop and find out sum of all even
nos and sum of all odd nos
#include<stdio.h>
void main()
{ int no, i= 1 , sume = 0,sumo=0 ;

printf("\nEnter 10 nos “);


while( i < = 10 )
{
scanf ( "%d" , &no);

i++;
}
printf("\n Sum Of Even Nos = %d “ , );
printff(“\n Sum Of Odd Nos =%d” , );
}
// WAP to calculate X^Y using while loop
#include<stdio.h>
void main()
{ int x , y , i = 1 ;
long int p = 1 ;
printf("\nEnter X and Y “);
scanf(“%d%d” , &x ,&y );
while( )
{

i++;
}
printf("\n Power = %ld “, p );
}
// WAP to enter 10 nos in a loop and count no of all positive nos
and no of all negative nos
#include<stdio.h>
void main()
{ int no, i= 1 , n = 0 , p=0 ;

printf("\nEnter 10 nos “);


while( i <=10 )
{
scanf ( "%d" , &no);

i++;
}
printf("\n No of positive nos = %d “, );
printff(“\n No of negative nos =%d ” , );
}
// WAP to Find out biggest no and its position from 10 no’ s
#include<stdio.h>
void main()
{ int no, big = ;
int i=1 , pos=0 ;

printf("\nEnter 10 nos );
while( i <= 10 )
{ scanf ( "%d" , &no);

i++;
}
printf("\nBiggest no=%d “, big );
printff(“\n Position=%d” , 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
Structure :
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 B2= %d , P2 = %d “ , b2 ,p2 ) ;
printf("\n B3= %d , P3 = %d “ , b3 ,p3 ) ;
}
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);
if( )
{

}
else if( )
{

}
else if( )
{

}
i++;
}
printf("\n B1= %d , P1 = %d “ , b1 ,p1 ) ;
printf("\n B2= %d , P2 = %d “ , b2 ,p2 ) ;
printf("\n B3= %d , P3 = %d “ , b3 ,p3 ) ;
}
//WAP to print any no in reverse order and calculate sum of every digits ,and
count no of digits
void main()
{ int no, r , sum=0 , c =0 ;
printf("\nEnter any number:");
scanf("%d",&no);
while( )
{

}
printf(" \n Sum of digits = %d “, sum );
printf(“\n No of digits = %d“ , c );
}
//WAP to update above program if user enter negative no

//WAP to calculate any no in reverse in another variable .

//WAP to check number is palindrome or not


( Exam : 252 , 12321 , 454 )
//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
Armstrong Number :
//WAP to check Entered number is Armstrong or not
void main()
{ int no , sum = 0 , r ;
printf("\nEnter number : ");
scanf("%d",&no);

while( )
{

}
if( )
printf(“ \n no is armstrong “ );
else
printf("\n no is not armstrong “ );
}
Assignments :

//WAP to display all Armstrong Numbers between range


( Nested loop )

//WAP to display all Armstrong Numbers between range


( Without Nested loop )
Hint :
void main()
{ int i=100 , no, s , r , --- ;
while ( i <=100 )
{no=lb ;
while( check on no )
{ -------
-------
}
if( no is armstrong )
printf("\n Armstrong no is %d ",lb);
i ++ ;
}
}
//WAP to display all armstrong no’s
void main()
{ int i=100 , no, sum , r, ;
while ( i <= 999 )
{no = i ;
while ( )
{

}
if( )
printf("\n %d ", i );
i ++ ;
}
}
//WAP to display all palindrome Numbers between range
( Nested loop )
Hint :
void main()
{ int lb , ub , no, rev , r , ;
printf("\nEnter lb and ub ");
scanf("%d%d" ,&lb , &ub );
while ( lb <= ub )
{ no=lb ;
while( )
{

}
if( )
printf(“ %d ",lb);
lb ++ ;
}
}
Prime Number :
Logic Of Prime Number :
//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( )
{

}
i++;
}

}
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
void main()
{ int n , x = 1 , y = 0 , t ;
int i=1;
printf("\nEnter no of terms ");
scanf("%d" , &n);
while( )
{

}
}
// WAP to display elements of Fibonacci series B/W Range
void main()
{ int x = 1 , y = 0 , t ;
int lb,ub ;
printf("\nEnter lb and ub ");
scanf("%d%d" , &lb,&ub);
while( )
{

}
}
Assignment
// WAP to display elements of Fibonacci series between range .
do . . . . while loop :
syntax :

initialization of loop index


do { Statement 1 ;
Statement 2 ;
----------------
Statement n ;
} while ( condition ) ;
Note : do … while loop execute at least once .

Differences between “while” loop and “do . . .while” loop


void main( ) void main( )
{ int x = 10 ; { int x = 10 ;
while ( ) do {
{
printf ( “ Hello “);
printf ( “ Hello “);
x++ ; x++ ;
} } while ( );
} }
void main( )
void main( )
{ int i = 0 ;
{ int i = 0 ;
do
while ( )
{
printf ( “ %d “ , i );
{
i++ ;
printf ( “ %d “ , i );
}while ( );
i++ ;
}
}
}
void main( )
void main( )
{ int i = 0 ;
{ int i = 0 ;
while ( )
do
{
{
printf ( “ %d “ , i );
printf ( “ %d “ , i );
i++ ;
i++ ;
}
}while ( );
}
}
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 ) ;

You might also like