SAMPLE C Programs
1. To print HELLO WORLD
#include<stdio.h>
Int main()
{
printf(“Hello World”);
return 0;
}
2. To initialize and to display the value of type int
#include<stdio.h>
Int main()
{
int a; /* declaration of variable a */
a=10; /* initialization of variable a*/
printf(“The value of a is %d”, a);
return 0;
}
3. To initialize and to display the value of type float
#include<stdio.h>
Int main()
{
float a;
a=10.50;
printf(“The value of a is %f”, a);
return 0;
}
4. To initialize and to display the value of type char
NOTE: while printing character or strings donot use & symbol in printf statement
#include<stdio.h>
Int main()
{
char a;
a = ‘S’;
printf(“The value of a is %c”, a);
return 0;
}
5. To initialize and to display the value in same line
#include<stdio.h>
Int main()
{
int a=10; /* declaration and initialization are done in same line */
printf(“The value of a is %d”, a);
return 0;
}
6. To initialize and display two or more variable
#include<stdio.h>
Int main()
{
int a=10, b=50 ; /* declaration and initialization are done in same line */
printf(“The value of a is %d and b is %d”, a,b);
return 0;
}
7. To perform addition of two values
#include<stdio.h>
Int main()
{
int a = 10, b = 50, c ; /* declaration and initialization are done in same line */
c = a + b;
printf(“The sum is %d”, c);
return 0;
}
8. To perform addition of two values by reading the value from user
#include<stdio.h>
Int main()
{
int a , b, c ; /* declaration part*/
printf(“Enter the values for a and b : ”);
scanf(“%d %d”, &a, &b); /* scanf is used to read input from user */
c = a + b;
printf(“The sum is : %d”, c);
return 0;
}
Similarly write the program for subtraction, multiplication
9. To perform simple mathematical calculation by reading the values from user
#include<stdio.h>
Int main()
{
int a , b, sum,sub,mul ; /* declaration part */
printf(“Enter the values for a and b”);
scanf(“%d %d”, &a, &b); /* scanf is used to read input from user */
sum = a + b;
sub = a - b;
mul = a * b;
printf(“The result of addition is sum is : %d”, sum);
printf(“The result of subtraction is : %d”, sub);
printf(“The result of multiplication is : %d”, mul);
return 0;
}
10. To calculate the area of a circle
#include<stdio.h>
Int main()
{
float pi = 3.148 , area, radius; /* declaration part */
printf(“Enter the value for radius of a circle : ”);
scanf(“%f”, &radius);
area = pi * radius * radius;
printf(“the area of a circle is : %f”, area);
return 0;
}
11. To swap two numbers
#include<stdio.h>
Int main()
{
int a,b,temp; /* declaration part */
printf(“Enter the value for a and b”);
scanf(“%d %d”, &a,&b);
printf(“The value of a and b before swapping %d %d”, a, b);
temp = a;
a = b;
b = temp;
printf(“The value of a and b after swapping %d %d”, a, b);
return 0;
}
12. To convert degree Celsius to Fahrenheit
#include<stdio.h>
main()
{
float celsius, fahrenheit ; /* declaration part */
printf(“Enter the value for celsius”);
scanf(“%f”, &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf(“The temperature in fahrenheit is %f”, fahrenheit);
return 0;
}
13. To find area and perimeter of a rectangle
#include<stdio.h>
Int main()
{
int length,breadth; /* declaration part */
printf(“Enter the value for length and breadth”);
scanf(“%d %d”, &length,&breadth);
printf(“The area of rectangle is: %d”,(length * breadth));
printf(“The perimeter of rectangle is: %d”, 2 * (length + breadth));
return 0;
}
14. To find square root of a number
#include<stdio.h>
#include<math.h>
Int main()
{
int a , res;
printf(“enter the value of a”);
scanf(“%d”,&a);
res = sqrt(a);
printf(“the square root of a is %d”, res);
return 0;
}
15. To find ASCII value of a character
#include<stdio.h>
Int main()
{
char c;
printf(“enter the character”);
scanf(“%c”, c);
printf(“ASCII value of %c = %d”,c,c);
return 0;
}
16. To find area of triangle
#include<stdio.h>
Int main()
{
float b,h,area;
printf(“enter the values of b and h”);
scanf(“%f %f”,&b , &h);
aea = (b * h)/2;
printf(“the area of a triangle is %f”, area);
return 0;
}
17. To work with all the functions in math.h header file
#include<stdio.h>
#include<stdio.h>
Int main()
{
float x;
printf(“enter the values of x”);
scanf(“%d”, &x);
x = x*3.14/180; //For radian conversion
printf(“The sin value of x is %f”, sin(x));
printf(“The cos value of x is %f”, cos(x));
printf(“The tan value of x is %f”, tan(x));
return 0;
}
18. To Round-up and Round-down the value
#include<stdio.h>
#include<math.h>
Int main()
{
foat x = 4.7;
printf(“The Round-up value of x is %f”, ceil(x));
printf(“The Round-down value of x is %f”, floor(x));
return 0;
}
19. To find the average of two numbers
#include<stdio.h>
Int main()
{
float a, b;
printf(“enter the value of a”);
scanf(“%f”,&a);
printf(“enter the value of b”);
scanf(“%f”,&b);
float avg=(a+b)/2;
printf(“the average of two values is %f”,avg);
return 0;
}
20. To convert total days to year, month and days
#include<stdio.h>
Int main()
{
int d,y,m,nd;
printf(“enter the number of days”);
scanf(“%d”,&d);
y=d/365;
d=d%365;
m=d/30;
nd==d%30;
printf(“%d years,%d month,%d days”,y,m,nd);
return 0;
}