Tuesday, July 17, 2018

SIMPLE SAMPLE PROGRAMS FOR C BEGINNERS



SAMPLE PROGRAM -1

PROGRAM TO PRINT WHETHER THE INPUT GIVEN IS A SQUARE OR RECTANGLE:



STATEMENT:

The length L and breadth B of a rectangle is passed as input. If L is equal to B, the program must print SQUARE. Else the program must print RECTANGLE.

PROGRAM:

#include<stdio.h>

#include <stdlib.h>

int main()

{   

int length,breadth;   

scanf("%d%d",&length,&breadth);   

if(length == breadth)

{       

printf("SQUARE");   

}

else   

{       
printf("RECTANGLE"); 

  }

}

Input:
22
22

Output:

SQUARE



SAMPLE PROGRAM -2

PROGRAM TO PRINT THE REVERSE ORDER FROM 1 TO N:

STATEMENT:

A number N is passed as the input. The program must print the numbers from 1 to N with each number separated by a space.

PROGRAM;


#include <stdio.h>

int main()

{

    int X,Y;

    scanf("%d%d",&X,&Y);

    while(X!=Y)

    {

        if(X > Y)

        {

            X = X-Y;

        }

        else

        {

            Y = Y-X;

        }

    }

    /* You can also print Y as both X and Y will be equal at this step */
 
printf("%d",X);
}

Input:
10

Output:
1 2 3 4 5 6 7 8 9 10

No comments:

Post a Comment