Search Your Topic

config
Thursday, 21 September 2017 05:56

C -for-Program

Written by
Rate this item
(0 votes)

Print the first five numbers starting from I together with their squares.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  I;

       Clrscr ( );

       For (i=l; <=5; i++)

       Printf (“\n  Number:  %5d  it’ s  Square:  %5d”, I, i*i);

      getch();

}

OUTPUT:

Number:  1  it’ s  Square:  1

Number:  2  it’ s  Square:  4

Number:  3  it’ s  Square:  9

Number:  4  it’ s  Square:  16

Number:  5  it’ s  Square:  25      

#include<stdio.h>

#include<conio.h>

Valid main ( )

{

        Int  I;

        Clrscr ( ) ;

        For ( i=l; i<=15; i= i+l)

        Printf (“%5d”,i);

        getche ( );

}

OUTPUT:

1 2 3 4 5 6 7 8 9 10 11 12 13 14  15

Write a program to display even numbers from 0 to 14.  Declare the initial counter value before the for loop statement.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

        Int  i=o ;

        Clrscr ( );

        For (i<=15;)

        {

                  Printf (“% 5 d “ , i) ;

                  i+=2;

        }

}

OUTPUT:

O 2 4 6 7 8 10 12 14

Write a program to count numbers between 1 to 100 not divisible by 2, 3, and 5.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

     Int  x, c=0;

     Clrscr ( );

     Printf (“\n  Numbers  from  1  to  100  not  divisible  by  2, 3  &  5 \n \n”);

     For (x=0  ;x<=100 ;x ++)

     {

            If (x%2 !=0   &&  x%3!=0  && x%5!=0)

            {

                Printf (%”%d\t” ,x),

                C++;

            }

     }

     Printf (“\nTotal numbers  :  %d”, c );

}

OUTPUT:

Numbers  from  1  to  100  not  divisible  by 2, 3  &  5

1    7    11    13    17    19    23    29    31    37     37

41   43    47    49    53     59    61    67    71     73

77    79    83    89   91    97

Total  Numbers  :  26

Write a program to display the numbers in increasing and decreasing order using the infinite for loop.    

#include<stdio.h>

#include<conio.h>           

Void main ( )

{

       Int  n, a, b;

       Clrscr ( );

       Printf (“Enter  a  number  :”);

       Scanf (“, &);

       A=b=n;

       Printf (“ (++)  ( - - ) \n”);

       Printf (“============”);

       For (;  ;  (a++, b - - ) )

       {

             Printf (“\n%d\t%d”, a, b);

             If (b==0)

             Break;

       }

}

OUTPUT:

Enter  a  number  :  5

(++)         ( - - )

===========

5    5

6    4

7    3

8    2

9    1

10  0   

create an infinite for loop.  Check each value of the for loop.  If the value is even display it otherwise continue with iterations.  Print Even numbers from 1 to 21.  Use break statement to terminate the program.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  i=l;

       Clrscr ( );

       Printf (“\n \t \t  Table  of  Even  numbers  from  1  to  20”);

       Printf (“\n \t \t  ===== == ==== ======= ==== = == == \n”);

       For (;  ;)

       {

           If (i==21)

           Break;

           else  if (i%2==0)

           {

                 Printf (“%d\t”, i);

                 I++;

                 Continue;

           }

           else

           {

                I++;

                Continue;

           }

    }

    getche ( );

}

OUTPUT:

Table  of  Even  numbers  from  1  to  20

====  ==  ====  =======  ===  =  ==  ==

2  4  6  8  10  12  14  16  18  20   

Calculate the sum of the first five numbers and their squares. Display their results.

Void main ( )

{

        Int  I,  sum=0, sqsum=0;

        Clrscr ( );

        For (i=l; i<-5; ++)

        {

               Sum+-I;

               Sqsum+=i*I;

               Printf (“\n  number:  %5d  it’ s  Square  :  %8d”, I, i*i);

       }

       Printf (“\n===================================”);

       Printf (“\n  The  sum  of  the  five  numbers (1  t0  5)  : -%6d”, sum);

       Printf (“\n  The  sum  of  their  Squares: -%9d”,  sqsum);

       getche ( );

}

OUTPUT:

Number:             1                 it’ s  Square:             1

Number:             2                 it’ s  Square:             4

Number:             3                 it’ s  Square:             9

Number:             4                 it’ s  Square:            16

Number:             5                 it’ s  Square:            25

=========================================

The  sum  of  the  five  numbers (1  t0  5) : -  15

The  sum  of  their  Squares: -  55       

 Write a program to find the number in between 7 and 100 which is exactly divisible by 4 and

If divided by 5 and 6 remainders obtained should be 4.

#include<stdio.h>

#include<conio.h>

#  include  <process . h>

Void main ( )

{

       Int  x;

       Clrscr ( );

       For (x=7;x<100;x++)

       {

              If (x%4==0  &&  x%5==4  &&  x%6==4)

              {

                     Printf (“\n  Number  :  %d”, x);

              }

      }

     getche ( );

}

OUTPUT:

Number  :  64   

Write a program to find the sum of the following series.

/*  1.  1+2+3+ ..n  */

/*  2.  12+22+32+..n2  */

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  sum=0, ssum-0, I, j;

       Clrscr ( );

       Printf (“Enter  Number  :”);

       Scanf (“%d”,  &j);

       Clrscr ( );

       Printf (“  Numbers:”);

       For (i=1; i<=j; i++)

       Printf (“%5d”, i);

       Printf (“\n \nSquares:”);

       For (i=1; i<=j; i++)

       {

            Printf (“%5d”, i*i);

            Sum=sum+I;

            Sum=ssum+i*i;

       }

       Printf (“\n \nSum  of  Numbers  from  1  to  %d  :%d”, j, sum);

       Printf (“\nSum  of  Squares  of  1  to  %d  Numbers  :%d”, j, ssum);

}

OUTPUT:

Enter  Number:  5

Numbers:  1  2  3  4  5

Squares:  1  4  9  16  25

Sum  of  Numbers  from  1  to  5:  15

Sum  of  Squares  of  1  to  5  Numbers:  55 

Write a program to find the perfect squares from 1 to 500.

#include<stdio.h>

#include<conio.h>

# include  <math . h>

Void maid ( )

{

      Int  I, count, x;

      Float  c;

      Clrscr ( );

      Printf (“\n \n”);

      Printf (“  Perfect  squares  from  1  to  500 \n”);

      For (i=1; <=500; i++)

  {

      C=sqrt (i);

       X=floor (c);  /*  For  rounding  up  floor ( )  is  used.  */

       {

              Printf (“\t%5d”, i);

              Coun ++;

      }

  }

  Printf (“\n \n  Total  Perfect  Squares  =5d\n”, conut);

  getch ( );

}

OUTPUT:

1       4      9      16      25   36   49   64   81   121   144

169   196   225    256    289   324    361    400   441

484                             

Total  Perfect  Squares  =  22

Write a program to detect the largest number out of five numbers and display it.

#include<stdio.h>

#include<conio.h>

# include<process . h>

Void main ( )

Exit (0);

{

       Int  a, b, c, e, sum=0, I;

       Clrscr ( );

       Printf (“\nEnter  Five  numbers  :”);

       Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);

       For (i=sum;  i<=sum; I - -)

       {

             If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

             {

                   Printf (“The  Largest  Number  :  %d”, i);

                   Exit (0);

             }

      }

}

OUTPUT:

Enter  Five  number  :  5  2  3  7  3 

The  Largest  Number  :  7        

Write a program to detect the smallest number our of five numbers and display it.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum=0, I;

      Clrscr ( );

      Printf (“\nEnter  Five  numbers  :”);

      Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);

      Sum=a+b+c+d+e;

      For (i=l;  i<=sum; i++)

      {

            If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

            {

            Printf (“The  Smallest  Number  :  %d”, i)

            Exit (0);

            }

      }

}

OUTPUT:

Enter  Five  numbers  :  5  2  3  7  3

The Smallest  Number:  2          

Write a program to print the five entered numbers in the ascending order.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum-0, I;

      Clrscr ( );

      Printf (“\nEnter  Five  numbers  :”);

      Scanf (“%d  %d  %d  %d  %d” &a, &b, &c, &d, &e);

      Printf (“\n  Numbers  in  ascending  order  :”);

      Sum=a+b+c+d+e;

      For (i=l;  i<=sum; i++)

  {

     If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

     {

             Printf (“%3d”, i);

     }

  }

}

OUTPUT:

Enter  Five  numbers  :  5  8  7  4  1

Numbers  in  ascending  order  :  1  4  5  7  8  

Perform multiplication of two integers by using the negative sign.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d=0;

      Clrscr ( );

      Printf (“\n  Enter  two  numbers  :”);

      Scanf (“%d  %d”,  &a, &b);

      For (c=l; c<-b; c++)

      D= (d) – ( - a);

      Printf (“Multiplication  of  %d  *  %d  :%d”, a, b, d);

      getche ( );

}

OUTPUT:

Enter  two  numbers  :  5  5

Multiplication  of  5  *  5  :  25   

 Calculate the sum and average of five subjects.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum=0, I;

      Float  avg;

      Clrscr ( );

      Printf (“\nEnter  The  Marks  of  Five  Subjects”);

      For (i=l; i<=5; i++)

      {

           Printf (“\n [%d]  Student:”, i);

           If (scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e) ==5)

           {

                 Sum=a+b+c+d+e;

                 Avg=sum/5;

                 Printf (“\n  Total  Marks  of  Student [%d]  %d”, I, sum);

                 Printf (“\n  Average  Marks  of  Student [%d]  %f\n”, I, avg);

           }

           else

           {

                Clrscr ( );

                Printf (“\n  Type  Mismatcch”);

           }

     }

}

OUTPUT:

Enter  The  Marks  of  Five  Subjects

[1] Student:  58  52  52  56  78

      Total  Marks  of  Student [1]  296

      Average  Marks  of  Student [1]  59.000000

[2] Student:             

Write a program to find perfect cubes up to a given number.

/* 1, 8, 27, 64 are perfect cubes of 1, 2, 3 and 4 */.

#include<stdio.h>

#include<conio.h>

#  include<math . h>

Void main ( )

{

      Int  I,  j,  k;

      Clrscr ( );

      Printf (“Enter  a  Number  :”);

      Scanf (“%d”, &k);

      For (i=l; i<k; i++)

      {

            For (j=l; j<=I; j++)

            {

                 If (i==pow (j,3) )

                 Printf (“\nNumber  :  %d  &   it’ s  Cube  :%d:, j, i);

            }

     }

}

OUTPUT:

Enter  a  Number  :  100

Number  :  1  &  it’ s  Cube  :  1

Number  :  2  &  it’ s  Cube  :  8

Number  :  3  &  it’ s  Cube  :  27

Number  :  4  &  it’ s  Cube  :  64

Write a program to display the stars as shown below.

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  x, I, j;

       Clrscr ( );

       Printf (“How  many  lines  stars  (*)  should  be  printed  ?  :”);

       Scanf (“%d”, &x);

       For (i=l; i<=x; i++)

       {

              For (j=l; j<=I; j++)

              {

                     Printf (“*”);

              }

              Printf (“\n”);

       }

}

OUTPUT:

How  many  lines  stars  (*)  should  be  printed  ?  :  5

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

Write a program to generate the pattern of numbers as given under.

6  5  4  3  2  1  0 

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0 

2  1  0 

1  0 

0

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  I, c=0;

      Clrscr ( );

      Printf (“Enter  a  Number  :”);

      Scanf (“%d, &i);

      For (;i>=0i - -)

      {

            C=I;

            Printf (“\n”);

            For (;  ;)

            {

                   Printf (“%3d”, c);

                   If (c==0)

                   Break;

                   C- -;

            }

      }

}

OUTPUT:

Enter  a  number:  6

6  5  4  3  2  1  0 

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0 

2  1  0 

1  0 

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  I, j, x; 

      Printf (“\nEnter  Value  of  x  :”);

      Scanf (“%d”,&x);

      Clrscr ( );

      For (j=l; j<=x; j++)

      {

             For (i=l; i<=j; i++)

             Printf (“%3d”, i);

             Printf (“\n”);

     }

     Printf (“\n”);

     For (j=x; j>=l; I - -)

     {

            For (i=j; i>=l; I- -)

            Printf (“%3d”, i);

            Printf (“\n”);

     }

OUTPUT:

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1        

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

#include<stdio.h>

#include<conio.h>

Void main ( )

{

     Int  I, j, x;

     Printf (“\nEnter  Value  of  x  :”);

     Scanf (“%d”, &x);

     Clrscr ( );

     For (j-l; j<=x; j++)

     {

           For (i=j; i>=l; I - -)

           {

                 Printf (“%3d”, i);

           }

           Printf (“\n”);

     }

}

OUTPUT:

Enter  Value  of  x  :  4

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

Write a program to generate the pyramid structure using numerical.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  k, I, j, x, p=34;

      Printf (“\n  Enter  A  number  :”);

      Scanf (“%d”, &x);

      Clrscr ( );

      For (j=0; j <=x; j++)

      {

             Gotoxy(p, j+l);

             /*  position  cursor  on  screen  (x  cordinate, y  cordinate)  */

            For (i=0 – j; I <=j; i++)

            Printf (“%3d”, abs ( I ) );

            P=p-3;

     }

}

OUTPUT:

Enter  a  number  :  3

            0

        1  0  1

    2  1  0  1  2

3  2  1  0  1  2  3    

 

Read 5228 times Last modified on Thursday, 21 September 2017 07:03
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « nested loops in C

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.