Pages

Saturday, 1 May 2021

CONTROL STRUCTURES --Loop control structures

These are also called as iterative or repetitive control structures

Loop :Some set of statements execute multiple time loops are of 2 types;

  1.  Entry controlled Loop Structures
  2.  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<stdio.h>
#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<stdio.h>
#include<conio.h>
void main( )
{
int n=1;
clrscr ( ) ;
do
{
printf("%d", n) ;
n =n+1;
while (n<=10) ;
getch( ) ;
}

3) "for" Statement:-

for (initialization; condition; Inc / dec)

{

body of the loop

}

// Write a program to print first 10 numbers using for statement.

#include<stdio.h>
#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<stdio.h>
#include<conio.h>
void main ( ) 
{
int n=10;
clrscr( ) ;
while (n>=1)
{
printf ("%d", n) ;
n=n-1;
}
getch( ) ;
}

/*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( ) ;
}

/* Write a program to print first 10 numbers in reverse  order
using for statement.*/
# include<stdio.h>
#include<conio.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>
#include<conio.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( ) ;
}

Do  while
# include<stdio.h>
#include<conio.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( ) ;
}

For
#include<stdio.h>
#include<conio.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>
#include<conio.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
#include <stdio.h>
#include<conio.h>
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 () ;
}

for:
#include <stdio.h>
#include<conio.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 () ;
}

Write a 'c' program to check whether the given no is a palindrome Or not
#include<stdio.h>
#include<conio.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 () ;
}

For
#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 () ;
}

Write a ‘C’ program to check whether the entered number is a perfect or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=1,n,sum=0,tmp;
clrscr();
printf("enter any number:");
scanf("%d",&n);
tmp=n;
while(i<n)
{
if(n%i == 0)
{
sum =sum+i;
}
i++;
}
if(tmp == sum)
{
printf("%d is a perfect number",tmp);
}
else
{
printf("%d is not a perfect number",tmp);
}
getch();
}

Write a ‘C’ program to find the sum of two given numbers without using ‘+’ sign.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,r;
clrscr();
printf(“enter two numbers: “);
scanf(“%d%d”,&a&b);
r=a - ~ b – 1;
printf(“sum is %d”,r);
getch();
}

Write a ‘C’ program to find the difference between two numbers without using ‘ – ‘ sign.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,r;
clrscr();
printf(“enter any two numbers: “);
scanf(“%d”,&n);
r = a + ~ b + 1;
printf(“Difference is %d”,r);
getch();
}

Write a ‘C’ program to check whether the entered number is strong number or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,f,r,I,tmp,sum = 0;
clrscr();
printf(“enter any number: “);
scanf(“%d”,&n);
tmp = n;
while(n) or (n!=0)
 {  
  f=1,i=1;
  r=n%10;
while(i<=r)
   {
    f=f*I;
i++;
   }
sum=sum+f;
   n=n/10;
  }
if(sum==tmp)
printf(“%d is a strong number“,tmp);
else
printf(“%d is not a strong number”,tmp);
getch();
}

Write a ‘C’ program to illustrate Fibonacci series.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
intn;i,past=0,present=1,future;
clrscr();
printf(“enter any number: “);
scanf(“%d”,&n);
printf(“%d%d”,past,present);
future=past+present;
printf(“%d”,future);
for(i=0;i<n-3;i++)
 {
past=present;
present=future;
future=past+present;
printf(“%d”,future);
 }
getch();
}

Input: n=5
Output: 0 1 1
          Past           present          future
             0 1            1 2
                   Past           present          future
0 1    1               2             3
                                              Past           present          future

No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...