Very basic C programs examples
1. C program to print "Hello world" or any text.
#include<stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
2. C program to add two numbers and display the sum.
#include<stdio.h>
main() {
int a=5, b=7, sum;
sum = a+b;
printf("Sum = %d",sum);
return 0;
}
3. C program take input of two numbers and add them and display the sum.
#include<stdio.h>
main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d",&a, &b);
sum = a+b;
printf("Sum = %d",sum);
return 0;
}
4. C program perform simple mathematical operations.
#include <stdio.h>
main() {
int a, b, sum, sub, mul, div, mod;
printf("Enter two numbers: ");
scanf("%d %d",&a, &b);
sum = a+b; //for addition
sub = a-b; //for substraction
mul = a*b; //for multiplication
div = a/b; //for division
mod = a%b; //for finding reminder
printf("%d + %d = %d\n",a, b, sum);
printf("%d - %d = %d\n",a, b, sub);
printf("%d x %d = %d\n",a, b, mul);
printf("%d / %d = %d\n",a, b, div);
printf("%d %% %d = %d",a, b, mod);
return 0;
}
5. C program calculate area of circle taking radius as input radius.
#include <stdio.h>
main() {
float r, area;
printf("Enter the radius: ");
scanf("%f",&r);
area = 3.1415 * r *r;
printf("Area = %f",area);
}
6. C program calculate circumference of circle having radius as input radius.
#include <stdio.h>
main() {
float r, circum;
printf("Enter the radius: ");
scanf("%f",&r);
circum = 2*3.1415*r;
printf("Area = %f",circum);
}
[Any fool can write code that a computer can understand. Good programmers write code that humans can understand.] – Martin Fowler
Prepared by : Subash Gautam.