How to Add Numbers Input by User in C Program

sum by c

Introduction :

In this article, I will show you how to add two numbers in C program. In Addition we combine two numbers and get the final result, that is called sum. In C programing, we add two numbers and print the sum on the screen. Here user is asked to enter two number and display the sum of two numbers.

Add Two Integer Numbers In C :

First, I show you about adding two numbers of the integer type. Integer number means whole number without decimal (5, 78, 458 etc.). In this program ask the user to enter any two integer numbers. By scanf() function Two number store in two int variable name as first_int and second_int. In a third int variable named result_of_sum store the sum of two number by simple arithmetic operation. Finally, print the value of the result_of_sum as output using printf() function.

For example,
first_int = 8
second_int = 9
result_of_sum = 17
Now output 17 is display on screen.

How Run the Program :

For run the program, open VS Code. if you not install VS Code , install it. In VS Code create a file name as integer_sum.c

You must be create file with .c extension . and paste the following code in it. Now run the program .

#include <stdio.h>
int main() {    
    // take three int variable where data store
    int first_int, second_int, result_of_sum;
    // user asked for input data
    printf("Enter First integers: ");
    scanf("%d", &first_int);
    printf("Enter Second integers: ");
    scanf("%d", &second_int);
    // add two integer
    result_of_sum = first_int + second_int;      
    // display sum in screen
    printf("Sum of %d and %d is %d\n", first_int, second_int, result_of_sum);
    return 0;
}

Here is the picture of the sum of integer program.

integer sum

Add Two Float Numbers In C :

Adding float number is same as above example, here you use float in place of integer. First you must be know about floating number.
Floating number means number with decimal (5.54, 78.24, 458.05 etc.) In this program ask the user to enter any two float numbers. By scanf() function Two number store in two float variable name as first_float and second_float. In a third float variable named result_of_fsum store the sum of two number by simple arithmetic operation. Finally, print the value of the result_of_fsum as output using printf() function.

For example,
first_float = 7.4
second_float = 9.5
result_of_fsum = 16.9
Now output 16.9 is display on screen.

How Run the Program :

For run the program, open VS Code. In VS Code create a file name as float_sum.c
You must be create file with .c extension and paste the following code in it. Now run the program .

#include <stdio.h>
int main() {    
    // take three float variable where data store
    float first_float, second_float, result_of_sum;
    // user asked for input data
    printf("Enter First float number: ");
    scanf("%f", &first_float);
    printf("Enter Second float number: ");
    scanf("%f", &second_float);
    // add two float number
    result_of_sum = first_float + second_float;      
    // display sum in screen
    printf("Sum of %f and %f is %f\n", first_float, second_float, result_of_sum);
    return 0;
}

Here is the picture of the sum of float program.

float sum

CONCLUSION :

In this article you learn how you add two number by c programing. Thanks for visit my site .

You can see my latest project :

You can see my latest blog :

Scroll to Top