- On
- By
- 0 Comment
- Categories: addition, Calculations
C program to the sum of two numbers
C program to the sum of two numbers
In this tutorial, we will discuss the C program to the sum of two numbers.
In this topic, we will learn how to find the sum of two integer number in C language
Sum of two integer
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num_1=56,num_2=34,sum; //value already assigned sum=num_1+num_2;//calculate sum printf("\sum of these numbers: %d ",sum);//display sum value getch(); return 0; }
When the above code is compiled and executed, it produces the following results
sum of these numbers: 90
Sum of two integer- get user input
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num_1,num_2,sum;//declared variable printf("\Enter two integer value: "); scanf("%d %d",&num_1,&num_2); //call the scanf() function to store values entered by user sum=num_1+num_2; //calculate sum of two numbers in stored in variable sum printf("%d + %d =%d:",num_1,num_2,sum);//display sum of numbers getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter two integer value: 45 65 45 + 65 = 110
In the above program, the user is entered two integer point value for find sum.
we use the pre-defined function scanf() for getting value from the user. So two integer values entered by the user and it is stored in variables num_1 and num_2
Then, te variables num_1 and num_2 are added using plus(+) operator and then the result displaying on the screen using printf() function
Suggested for you