Lecture6 Loop
Lecture6 Loop
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)
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 ;
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 ;
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
while( )
{
}
if( )
printf(“ \n no is armstrong “ );
else
printf("\n no is not armstrong “ );
}
Assignments :
}
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 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 :
Example 2 :
for ( i = 0 ; i < 10 ; ++ i )
printf ( “ %d “, i ) ;
Example 3 :
for ( i = 0 ; i < 10 ; i ++ )
printf ( “ %d “, i ) ;