These are also called as iterative or repetitive control structures
Loop :Some set of statements execute multiple time loops are of 2 types;
- Entry controlled Loop Structures
- Exit controlled Loop Structures
1. Entry controlled Loop Structure :-
These Loops are also called top tested loops. In this the condition is checked before entering into the body of the loop & the body of the loop is executed until the condition is false
2. Exit controlled Loop Structure :-
These loops are also called bottom tested loop. In this first body of the loop is executed, then the condition is evaluated, the body of the loop is executed until the condition is false
Note:
- While for are called entry Control Loop Structure
- Do while is called exit Control Loop Structure
1) "While" Statement:-
Syntax:
while ( condition)
{
body of the loop
}
/* Write a program to print first 10 numbers using for statement */
#include<conio.h>
void main ( )
{
int n=1 ;
clrscr ( ) ;
while (n<= 10)
{
printf ("%d", n) ;
n= n+1;
}
getch();
}
2) "Do while " Statement :-
Syntax :
do
{
body of the loop
}
while ( condition) ;
/* Write a program to print first 10 numbers using do while statement. */
#include<conio.h>
void main( )
{
int n=1;
clrscr ( ) ;
do
{
printf("%d", n) ;
n =n+1;
} while (n<=10) ;
getch( ) ;
}
for (initialization; condition; Inc / dec)
{
body of the loop
}
// Write a program to print first 10 numbers using for statement.
#include<conio.h>
void main( )
{
int n;
clrscr ( ) ;
for (n=1; n<=10; n++)
{
printf("%d", n) ;
}
getch( ) ;
}
/* Write a program to print first 10 numbers in reverse order using while statement.*/
#include<conio.h>
void main ( )
{
int n=10;
clrscr( ) ;
while (n>=1)
{
printf ("%d", n) ;
n=n-1;
}
}
/*Write a program to print first 10 numbers in reverse order using do while statement.*/
#include<stdio.h>
#include<conio.h>
void main ( )
{
int n=10;
clrscr ( ) ;
do
{
printf ("%d", n) ;
n =n-1;
}while (n>=1) ;
getch( ) ;
}
using for statement.*/
# include<stdio.h>
void main ( )
int n;
clrscr( ) ;
for (n= 10; n>=1; n- - )
{
printf("%d", n) ;
}
getch( ) ;
}
// Write a 'C' program to skip a no. of given n numbers.
#include<stdio.h>
void main ( )
{
intn,i=1;
clrecr ( ) ;
printf(" enter any number ") ;
scanf("%d", &n) ;
while (i<=10)
{
if ( n == I)
printf ("\n") ;
else
printf ("%d", I) ;
i++
}
getch( ) ;
}
# include<stdio.h>
void main( )
{
int n, i=1;
clrscr( ) ;
do
{
printf (" enter any number ") ;
scanf("%d", &n) ;
}
do
{
if (n ==i)
printf ("\n")
else
printf ("%d", i);
i++ ;
}
while (i<=10) ;
getch( ) ;
}
#include<stdio.h>
void main ( )
{
int n, i=1;
clrscr( ) ;
pf("enter any number") ;
sf("%d", &n) ;
for(i=1, i<10, i++)
{
if(n==i)
pf("\n")
}
else
{
pf("%d", n);
}
getch ();
}
Write a program to print 4 th table
#include<stdio.h>
void man ()
{
int n,i=1, x;
pf("enter any number ") ;
sf("%d", & n) ;
while (I<=10)
{
x=n*i;
pf("\n%d*%d=%d", n, i, x) ;
i++;
}
getch () ;
}
Do while
Void main()
{
int n, i=1, x;
Clrscr() ;
pf(" enter any number")
sf("%d", &n) ;
do
{
x=n*i;
pf("\n%d*%d=%d", n, i, x) ;
i++;
}while (i<=10) ;
getch () ;
}
#include <stdio.h>
void main ()
{
int n, i=1.x;
Clrscr() ;
pf("enter any number ") ;
sf("%d", &n) ;
for(i=1, i<=10;i+++)
{
x=n*i;
pf("\n%d*%d=%d", n, i,x);
}
getch () ;
}
#include<stdio.h>
void main ()
{
int n, tmp, r, sum=0
Clrscr
pf(" enter any number ") ;
sf("%d", &n) ;
tmp=n;
while (n)
{
r=n%10;
n=n/10;
Sum=sum*10+r;
}
if(sum=tmp)
{
pf("%d is a palindrome", tmp) ;
}
else
{
pf("%d is not a palindrome", tmp) ;
}
getch () ;
}
Write a 'c' program to find the factorial of a given number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int n, f=1;
clrscr() ;
pf("enter any number ") ;
sf("%d", &n) ;
while (n)
{
f=f*n;
n--;
}
pf("%d factorial is%d", n, f) ;
getch () ;
}
Do while
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main ()
{
int n, f=1;
clrscr() ;
pf(" enter any number ") ;
sf("%d" &n) ;
do
{
n=f*n;
n--;
}while(n);
pf("%d factorial is%d", n, f) ;
getch () ;
}
For
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main ()
int n, f=1;
clrscr () ;
pf("enter any number ") ;
sf("%d, &n) ;
for(" n! =0, n--")
{
f=f*n;
}
pf("%d factorial is%d", n, f) ;
getch () ;
}
Write a c program to check whether the given number is prime or not
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main ()
{
int i=1, n, a=0;
Clrscr();
pf("enter any number ") ;
sf("%d", &n);
while (i<=n)
{
if( (n%i) ==0&&(n%n==0) )
a++;
i++;
}
if (a==2)
{
pf(%d is a prime number", n) ;
}
else
{
pf(" %d is not a prime number ", n) ;
}
getch () ;
}
Do while
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
int i=1, n,a=0;
clrscr() ;
pf(" enter any number ") ;
sf("%d", &n) ;
do
{
if( (n%i==0) &&(n%n==o) )
a++;
i++;
}while (i<=n);
if (a=2)
{
pf(%d is a prime number", n) ;
}
else
{
pf(%d is not a prime number ;n) ;
}
getch () ;
}
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main
{
int n, i=1, a=0;
Clrscr;
pf("enter any number ") ;
sf("%d", &n) ;
for(i=1, i<=n, i++)
{
if ( (n%i==0) &&(n%n==0))
{
a++
}
}
if(a==2)
{
pf(" %d is a prime number ", n) ;
}
else
{
pf("%d is not a prime number", n) ;
}
getch () ;
}
Write a c program to check whether the given number is an arm strong number Or not
#include <stdio.h>
#include <conio.h>
#include <math.h>
voidmain ()
{
int n, tmp, r, sum=0;
clrscr() ;
pf(" enter any number ") ;
sf("%d" , &n) ;
tmp=n
while (n) or (n!=0)
{
r=n%10
n=n/10
sum=sum+(r*r*r);
}
if(sum=tmp)
{
pf("%d is an arm strong number", tmp) ;
}
else
{
getch () ;
}
No comments:
Post a Comment