Sunday, 4 September 2016

SAMPLE OF C PROGRAM

Sample C Program

1.Write a program to print 'hello world'.


      #include<stdio.h>
      void main()

      { printf("hello world");
}

Output 

hello world

 2.Write a program which initialize a variable to value 1000 print the value back.

      #include<stdio.h>
      void main()
      
     { int num =1000;
        printf("value is %d",num);

}
 
 Output 

 Value is  1000

3.  Write a program which allows user to enter an integer and print it back.

      #include<stdio.h>
      void main()
  {
      int x,
      printf("enter an integer");
      scanf("%d",&x);
      printf("value is %d",x);
}

Output

Enter an integer 33
value is 33

4. Write a program which allows users to enter an integer and print its square.

    #include<stdio.h>
    void main()
   {  
        int x,sq;
       printf("Enter an integer");
       scanf("%d",&x);
       sq=x*x;
       printf("Square is %d",sq);
       
}

 Output

 Enter an integer 10
 Square is 100

5.  Write a program which print sum and difference of two user entered integer.

      #include<stdio.h>
      void main()
    {
            int x,y,sum,diff;
         printf("Enter two integer");
         scanf("%d%d",&x,&y);
         sum=x+y;
         diff =x-y;
         printf("sum is %d \n Difference is %d",sum,diff);

}

Output 

Enter two integer 10 2
sum= 12
Difference= 8

6.  write a program which allow user to enter three integer and print their average

  #include<stdio.h>
  void main()

{  int x,y,z;
    float avg;
    printf("Enter three integer");
    scanf("%d%d%d",&x,&y,&z);
    avg=(x+y+z)/3.0;
    printf("Average is %f",avg);


}

Output

Enter three Integer 2 3 4
Average 3.0000




No comments:

Post a Comment