In this post, basic C Programs with Solutions are described along with the description of code used in the program. It also explains the common terms used to write a program in the C language. Some common terms are:
#include:
It is a preprocessor directive that tells the preprocessor to include header files in the program.Angular brackets(<>):
At the start & end of the header file, angular brackets are used.- Header Files: Header files are used to provide the inbuilt functions used in the program. there are many headers files which are used in c & C++ Programs. Some header files used in the following program are as follows:
stdio.h
: It Stands for Standard Input Output. This header file is used to provide inbuilt function “printf” & “scanf” to the programmath.h
– math.h header file is used to perform a mathematical function in the program. For example sqrt() – to find the square root, pow() – to find the power of the number.
int main ()
: “main()” specify that the body of the program started from here and int specifies that the program will return an integer value.return 0
: return o specifies that the function is returning the integer value. This Code is used because int is used at the starting of the body.
Some of the basic C programs with solutions are explained one by one which are as follows:
Hello World Program in C
Code of The Above Question:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
Output:
Hello World
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- Statement
printf("Hello World")
is used to display the output on the console i.e. Hello World - In the last line
return 0
is used to specify that the program is returning integer value.
- Statement
C Program For Addition of Two Numbers
Code of The Above Question:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Addition of Two Numbers is %d", sum);
return 0;
}
Output:
Enter two integers:23
37
Addition of Two Numbers is 60
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
int num1, num2, sum
- Statement
printf("Enter two integers: ")
is used to display the output Enter two integers: on the console. scanf("%d %d", &num1, &num2)
This is used to take input of the variables from the user.sum = num1 + num2
This is used to find the division.- This statement is used to display the division calculated on the console
printf("Addition of Two Numbers is %d", sum)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
Multiplication of Two Numbers in C
Code of The Above Question:
#include <stdio.h>
int main() {
int num1, num2, mul;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
mul = num1 * num2;
printf("Multiplication of Two Numbers is %d", mul);
return 0;
}
Output:
Enter two integers: 20
40
Multiplication of Two Numbers is 800
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
int num1, num2, mul
- Statement
printf("Enter two integers: ")
is used to display the output Enter two integers: on the console. scanf("%d %d", &num1, &num2)
This is used to take input of the variables from the user.mul = num1 * num2
This is used to find the division.- This statement is used to display the division calculated on the console
printf("Multiplication of Two Numbers is %d", mul)
- In the last line
return 0
is used to specify that the program is returning integer value.
Subtraction of Two Numbers in C
Code of The Above Question:
#include <stdio.h>
int main() {
int num1, num2, sub;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sub = num1 - num2;
printf("Subtraction of Two Numbers is %d", sub);
return 0;
}
Output:
Enter two integers:30
15
Subtraction of Two Numbers is 15
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
int num1,num2,
sub
- Statement
printf("Enter two integers: ")
is used to display the output Enter two integers: on the console. scanf("%d %d", &num1, &num2)
This is used to take input of the variables from the user.sub = num1 - num2
This is used to find the division.- This statement is used to display the division calculated on the console
printf("Subtraction of Two Numbers is %d", sub)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
C Program For division of Two Numbers
Code of The Above Question:
#include <stdio.h>
int main() {
int num1, num2, div;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
div = num1 / num2;
printf("Division of Two Numbers is %d", div);
return 0;
}
Output:
Enter two integers:4
2
Division of Two Numbers is 2
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
int num1,num2,
div
- Statement
printf("Enter two integers: ")
is used to display the output Enter two integers: on the console. scanf("%d %d", &num1, &num2)
This is used to take input of the variables from the user.div = num1 / num2
This is used to find the division.- This statement is used to display the division calculated on the console
printf("Division of Two Numbers is %d", div)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
C Program To Find The Modulus of Two Numbers
Code of The Above Question:
#include <stdio.h>
int main() {
int num1, num2, mod;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
mod = num1 % num2;
printf("Modulus of Two Numbers is %d", mod);
return 0;
}
Output:
Enter two integers:53
5
Modulus of Two Numbers is 3
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
int num1,num2,
mod - Statement
printf("Enter two integers: ")
is used to display the output Enter two integers: on the console. scanf("%d %d", &num1, &num2)
This is used to take input of the variables from the user.mod = num1 % num2
This is used to find the division.- This statement is used to display the division calculated on the console
printf("Modulus of Two Numbers is %d", mod)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
C Program To Find The Average of Five Numbers
Code of The Above Question:
#include <stdio.h>
int main()
{
float a,b,c,d,e,avg;
printf("enter the five numbers to find average");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
avg=(a+b+c+d+e)/5;
printf("Average of five number is %f",avg);
return 0;
}
Output:
enter the five numbers to find average 1
2
3
4
5
Average of five number is 3.00000
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
float a,b,c,d,e,avg
- Statement
printf("enter the five numbers to find average")
is used to display the output enter the five numbers to find average on the console. scanf("%f %f %f %f %f",&a,&b,&c,&d,&e)
This is used to take input of the variables from the user.avg=(a+b+c+d+e)/5
This is used to find the average of five numbers entered by the user.- This statement is used to display the average of five numbers calculated on the console
printf("Average of five number is %f",avg)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
C Program For Simple Interest
Code of The Above Question:
#include<stdio.h>
int main()
{
float p,r,t,si,amount;
printf("enter the values principle,rate,interest");
scanf("%f %f %f",&p,&r,&t);
si=(p*r*t)/100;
printf("simple interest is %f",si);
return 0;
}
Output:
enter the values principle,rate,interest 10
10
10
simple interest is 10
Description of Above Code:
- In the beginning,
#include <stdio.h>
is to provide inbuilt functions “printf” & “scanf” in the program. - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
float p,r,t,si,amount
- Statement
printf("enter the values principle,rate,interest")
is used to display the output enter the values principle, rate, interest on the console. scanf("%f %f %f",&p,&r,&t)
This is used to take input of the variables from the user.si=(p*r*t)/100
This is used to find the simple interest.- This statement is used to display the simple interest calculated on the console
printf("simple interest is %f",si)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
C Program to Find The Compound Interest
Code of The Above Question:
#include <stdio.h>
#include <math.h>
int main() {
float p,r,t,ci;
printf("enter the principal, rate and time for compound interest");
scanf("%f %f %f",&p,&r,&t);
ci=p*pow((1+(r/100)),t);
printf("compound interest is %f",ci);
return 0;
}
Output:
enter the principal, rate and time for compound interest 5
100
2
compound interest is 20
Description of Above Code:
- In the beginning,
#include <stdio.h>
is used to provide inbuilt functions “printf” & “scanf” in the program - And,
#include <math.h>
is used to provide inbuilt function “pow” in the program - In the next line,
int main()
is used to start the body of the program & specifies that the program must return an integer value. - The
;
is used to end the line. - The body of the program starts and end with curly brackets
{
}. - In the body,
- It is used to enter the type of variables used
float p,r,t,ci
- Statement
printf("enter the principal, rate and time for compound interest")
is used to display the output enter the principal, rate and time for compound interest on the console. scanf("%f %f %f",&p,&r,&t)
This is used to take input of the variables from the user.ci=p*pow((1+(r/100)),t)
This is used to find the compound interest.- This statement is used to display the compound interest calculated on the console
printf("compound interest is %f",ci)
- In the last line
return 0
is used to specify that the program is returning integer value.
- It is used to enter the type of variables used
Discover more from easytechnotes
Subscribe to get the latest posts sent to your email.