C Program Example No.
The following is the simplest C program that will print "Hello Compiler, I am C" on the
screen :
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello Compiler, I am C");
getch(); // holds the output screen until the user press a key
return 0;
}
C Program Example No. 2
Here is the second program, where the user is allowed to enter any input.
#include<stdio.h>
#include<conio.h>
int main()
{
char name[20];
printf("Enter your Name: ");
scanf("%s", name);
printf("Hello, %s\nWelcome to ABC", name);
getch();
return 0;
}
C Program Example No. 3
#include <stdio.h>
void main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num); // %d is used for an integer
printf("The number is: %d\n", num);
}
C Program Example No. 4
Addition program in C ( Adding two integers)
#include <stdio.h>
void main()
{
//declare three variables, for firstNumber+secondNumber=TotalSumOfBoth
int firstNumber, secondNumber, TotalSumOfBoth;
//ask the user to get first number to add
printf("Enter first number: ");
//store it in first variable using scanf() function
scanf("%d", &firstNumber);
//ask the user to get second number to add
printf("Enter second number:");
//store it in second variable using scanf() function
scanf("%d", &secondNumber);
// sum of two numbers in stored in variable TotalSumOfBoth
TotalSumOfBoth = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, TotalSumOfBoth);
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b ,c;
printf("Enter any two number: ");
scanf("%d%d", &a, &b);
c = a+b;
printf("\nSum of %d and %d is %d", a, b, c);
getch();
return 0;
}
C Program Example No. 5
The above program is only applicable for integer values. If the user enters any two numbers
that contain a decimal, the above program will not be applicable. So here is the modified
version of adding two numbers in C without regard to whether the entered number is an
integer value or a real number.
#include<stdio.h>
#include<conio.h>
int main()
{
float num1, num2, add;
printf("Enter any two number: ");
scanf("%f%f", &num1, &num2);
add = num1+num2;
printf("\nSum of %0.2f and %0.2f is %0.2f", num1, num2, add);
getch();
return 0;
}
As you can see from the above program, in place of int, declare all three variables of the float
type.
Here, %0.2f is used to print a real number up to the second decimal place only.
C Program Example No. 6
Add, Subtract, Multiply, and Divide in C
The question is: Write a program in C that performs all four basic mathematical operations,
such as addition, subtraction, multiplication, and division.
Here is the program in C that solves the above question, but it is only applicable for integer
data.
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2, res;
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);
res = num1+num2;
printf("\nAddition = %d", res);
res = num1-num2;
printf("\nSubtraction = %d", res);
res = num1*num2;
printf("\nMultiplication = %d", res);
res = num1/num2;
printf("\nDivision = %d", res);
getch();
return 0;
}
Main steps used in the above program
Here are some of the main steps used in the above program:
• Initialize three variables of integer type, say num1, num2, and res.
• Variables num1 and num2 will be used to hold the value of the first and second numbers
entered by the user at run-time. And the variable res will be used to store the value after
performing the operation, such as add, subtract, multiply, and divide between two numbers.
• Receive the input from the user.
• Now initialize the addition result of the given two numbers stored in variables num1 and
num2, respectively, to the variable res.
• Print the value of res as the sum of the two numbers given.
• Do the same for subtraction, multiplication, and division operations.
C Program Example No. 7
Add, Subtract, Multiply, and Divide in C
#include<stdio.h>
#include<conio.h>
int main()
{
float num1, num2, res;
printf("Enter any two number: ");
scanf("%f%f", &num1, &num2);
res = num1+num2;
printf("\nAddition = %.2f", res);
res = num1-num2;
printf("\nSubtraction = %.2f", res);
res = num1*num2;
printf("\nMultiplication = %.2f", res);
res = num1/num2;
printf("\nDivision = %.2f", res);
getch();
return 0;
}
Here, we have changed the data type to "float" to handle real data or any real number that may
include a decimal or not. After running the above program, enter any two numbers,
To receive any floating-point (real number) data, use %f as a format specifier.
To print the value of the res variable, we have placed. 2 between the format
specifiers, say % and f, to print only two digits after the third decimal.
C Program Example No. 8
C Program to Multiply Two Numbers Given by User and Display
Product
#include<stdio.h>
#include<conio.h>
int main()
{
float a, b, prod;
clrscr(); // Clears the screen
printf("Enter the value of a: ");
scanf("%f", &a);
printf("Enter the value of b: ");
scanf("%f", &b);
prod = a * b;
printf("Product of %f and %f is %f.",a,b,prod);
getch(); // Holds the output
return 0;
}
C Program Example No. 9
C Program to Find Area and Circumference of Circle
#include<stdio.h>
#include<conio.h>
#define PI 3.141592
int main()
{
float radius, area, circum;
clrscr(); // Clears the screen
printf("Enter radius of circle: ");
scanf("%f", &radius);
area = PI*radius*radius;
circum = 2*PI*r;
printf("Area = %f\n",area);
printf("Circumference = %f",circum);
getch(); // Holds the output
return 0;
}
C Program Example No. 10
C Program to Calculate Area & Perimeter of the Rectangle
#include<stdio.h>
#include<conio.h>
int main()
{
/* Declaring variables */
float length, breadth, area, perimeter;
/* Reading length and breadth from user */
printf("Enter length of the rectangle = ");
scanf("%f", &length);
printf("Enter breadth of the rectangle = ");
scanf("%f", &breadth);
/* Calculating area and perimeter */
area = length * breadth;
perimeter = 2 * (length * breadth);
/* Displaying result */
printf("Area of rectangle = %f", area);
printf("\nPerimeter of rectangle = %f", perimeter);
return 0;
}
C Program Example No. 11
C Program to check whether the given integer is positive or negative
#include <stdio.h>
void main()
{
int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
C Program Example No. 12
C Program to reverse a given number using Recursive function
Logic of this function: In this function, we are dividing the input number by 10 and
assigning the remainder in a variable sum, the remainder is the last digit of the number. This
function then calls itself for the number/10, the number/10 ensures that the number without
last digit is passed to the same function again.
In the next iteration the second last digit becomes the second digit of sum (because of this
logic sum=sum*10+rem). This process keeps going on until all digits are removed from
original number in reverse order and appended to the variable sum. At the end of the
recursion, this sum variables contains the reverse of the original number.
#include <stdio.h>
int sum=0,rem;
int reverse_function(int num){
if(num){
rem=num%10;
sum=sum*10+rem;
reverse_function(num/10);
}
else
return sum;
return sum;
}
int main(){
int num,reverse_number;
//Take the number as an input from user
printf("Enter any number:");
scanf("%d",&num);
//Calling user defined function to perform reverse
reverse_number=reverse_function(num);
printf("The reverse of entered number is :%d",reverse_number);
return 0;
}
C Program Example No. 13
C Program to find greatest of three numbers
1. User is asked to enter three numbers one by one. The program store
these numbers into three variables num1, num2 and num3 using scanf()
function.
2. Program compares num1 to other two variables num2 & num3 and if num1
is grater than both of these numbers then print num1 is the largest
number.
3. Similarly compares num2 with num1 & num3 and if greater print num2 is
the largest number.
4. Similar to step 2 and 3, compare num3 with num1 and num2 and if
greater print num3 is the largest number.
#include <stdio.h>
int main() {
double num1, num2, num3;
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter third number: ");
scanf("%lf", &num3);
// if num1 is greater than num2 & num3, num1 is the largest
if (num1 >= num2 && num1 >= num3)
printf("%lf is the largest number.", num1);
// if num2 is greater than num1 & num3, num2 is the largest
if (num2 >= num1 && num2 >= num3)
printf("%lf is the largest number.", num2);
// if num3 is greater than num1 & num2, num3 is the largest
if (num3 >= num1 && num3 >= num2)
printf("%lf is the largest number.", num3);
return 0;
}
C Program Example No. 14