Search This Blog


Wednesday, October 2, 2019

Looping program practice


Program practice of looping in c
Looping Program practice


P3. Program to print the series of natural no. from 100 to 1.

#include<stdio.h>
#include<conio.h>
  void main()
  {
    clrscr();
    int i;
  printf( “ series of natural no. from 100 to 1 \n “) ;
  for ( i= 100 ; i >= 1 ; i -- )
  {
    printf( “ %d “, i );
   }
  getch();
  }

P4. Program to print  the series of even natural no. up to 100 without if condition .

  #include<stdio.h>
  #include<conio.h>
   void main()
   {
     clrscr();
     int i ;
    printf( “ series of even no. upto 100 are : “);
     for ( i =2 ; i <= 100 ; i = i + 2)
     {
       printf( “ %d “, i );
       }
   getch();
   }

P5. If we have to write the above program by if condition we have :-

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
     clrscr();
     int i ;
     printf( “ series of even no. upto 100 are : “);
      for ( i =1 ; i <= 100 ; i = i ++ )
       {
        if ( i % 2 == 0 )
         printf( “ %d\n “, i );
        }
   getch();
   }

P6. Program to print the series of odd natural no. from 100 to 50 using if condition .

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i;
   printf( “ series of odd natural no from 100 to 50 \n “ );
    for ( i =100 ; i >= 50 ; i -- )
     {
      if ( i % 2 == 1 )
      printf( “ %d \n “, i ) ;
     }
   getch();
  }

P7. Program to print the series of those natural nos. that are divisible by 2 & 4 upto 100.

 #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i;
   printf( “ natural no divisible by 2 & 4 \n “ );
    for ( i =1 ; i <= 100 ; i ++ )
     {
      if ( i % 2 == 0 &&  i  % 4 == 0 )
      printf( “ %d \n “, i ) ;
     }
   getch();
  }

P8. Program to find sum of even and odd natural no. upto hundred.

 #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i , esum = 0 , osum = 0;
    for ( i =1; i <= 100 ; i ++ )
    {
      if ( i % 2 == 0 )
       esum = esum + i ;
      else
       osum = osum +i ;
     }
   printf( “ sum of even upto 100 = %d \n“ , esum );
   printf( “ sum of odd upto 100 = %d \n “, osum );
   getch();
  }

P9. Program to input any no. after that print all factors.

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i , n ;
   printf( “ enter any no. “ );
   scanf ( “ %d “, &n );
    printf( “ all factors are \n “, );
     for ( i =1 ; i <= n ; i ++ )
      {
       if ( n % i == 0 )
       printf( “ %d \n “, i ) ;
      }
   getch();
  }

P10. Program to input any no. after that find sum of all factors.

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i , n , fsum = 0 ;
   printf( “ enter any no. “ );
   scanf ( “ %d “, &n );
     for ( i =1 ; i <= n ; i ++ )
      {
       if ( n % i == 0 )
       fsum = fsum + i ;
      }
   printf( “ sum of all factors = %d “ , fsum );
   getch();
  }

P11. Program to input any no. after that check no. is perfect or not .

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i , n , sum =0 ;
   printf( “ enter any no. “ );
   scanf ( “ %d “, &n );
    for ( i =1 ; i <= n ; i ++ )
     {
      if ( n % i == 0 )
      sum = sum + i ;
     }
   if ( sum == n )
     printf( “ perfect no. “ );
   else
     printf( “ not perfect no. “ );
   getch();
  }

P12.  Program to input any no. after that check no. is prime or not .

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int i , n , count = 0 ;
   printf( “ enter any no. “ );
   scanf ( “ %d “, &n );
     printf( “ all factors are \n “, );
      for ( i =1 ; i <= n ; i ++ )
      {
        if ( n % i == 0 )
        count = count + 1 ;
      }
   if ( count == 2 )
    printf( “ Prime no. “);
   else
    printf( “ not prime no. “ );
   getch();
  }

P13. Program to input two nos. after that check no. is amicable or not .

  #include<stdio.h>
 #include<conio.h>
  void main()
   {
   clrscr();
   int a , b , i , sum1 = 0 , sum2 = 0 ;
   printf( “ enter any two no. “ );
    scanf ( “ %d %d “, &a, &b );
     for ( i =1 ; i <= a ; i ++ )
      {
       if ( a % i == 0 )
       sum1 = sum1 + i ;
      }
     for ( i = 1 ; i < b ; i ++ )
      {
       if ( b % i == 0 )
       sum2 = sum2 + i ;
      }
    if ( sum1 = b && sum2 = a )
      printf( “ Amicable no. “ );
    else
      printf( “ not amicable no. “ ) ;
   getch();
  }


No comments:

Post a Comment