How to Add Numbers Input by User in C Program

sum by c

Introduction :

Adding numbers which input by the user is a basic task in C programming language. It is helpful for beginners to learn the basic arithmetic operations by C language. Here, you have to read input from the user. Then, calculate these inputs and display the result on the console screen.

In this article, I shall show you how to add two types of numbers (integer and floating point number) in C programming language. Here, you can see two addition program by C language, one for Integer numbers and another for floating point numbers.

Add two integer numbers in C language :

In this program, I shall show you how to add two integer numbers by C programming language. Integer number means a whole number without decimal (5, 78, 458). The program asks the user to enter any two integer numbers using printf() function. Using scanf() function, you can store two numbers in two int variable names as “first_int” and “second_int”.

In a third int variable named “result_of_sum”, store the sum of two numbers by simple arithmetic operation. Finally, print the value of the result_of_sum as output on the console screen using printf() function.

To run the program on your PC, open VS Code. If you do not install VS Code, install it. In VS Code, create a new file name as “integer_sum.c”. You must create the file with .c extension. Now, paste the following code in it and run the program.

Source code of the program :

The following code is the source code of the sum of integer numbers in C programming language.

/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
   int first_int, second_int, result_of_sum;
   printf("Enter first integer number\n");
   scanf("%d", &first_int);
   printf("Enter second integer number\n");
   scanf("%d", &second_int);
   result_of_sum = first_int + second_int;
   printf("Sum of %d and %d is %d\n", first_int, second_int, result_of_sum);
   return 0;
}

Output of the program :

The below image is the output of the sum of integer numbers in C programming language.

output of the sum of integer numbers in C programming language

Add two floating point numbers in C language :

Adding floating point numbers is same as an above example. Here, you have to use floating point numbers in place of integer. Floating point number means a number with decimal (5.54, 78.24, 458.05). The program asks the user to enter any two floating point numbers. After that, you have to store two numbers in two float variable names as “first_float” and “second_float”.

Now, calculate the sum of two floating point numbers by simple arithmetic operation and store in a third float variable named “result_of_fsum”. Finally, print the value of the “result_of_fsum” as output using printf() function.

If you run the program, create a new file name as “float_sum.c” and paste the following code in it. After that, run the program.

Source code of the program :

The following code is the source code of the sum of floating point numbers in C programming language.

/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
   float first_float, second_float, result_of_sum;
   printf("Enter first floating point number\n ");
   scanf("%f", &first_float);
   printf("Enter second floating point number\n");
   scanf("%f", &second_float);
   result_of_sum = first_float + second_float;
   printf("Sum of %f and %f is %f\n", first_float, second_float, result_of_sum);
   return 0;
}

Output of the program :

The below image is the output of the sum of floating point numbers in C programming language.

output of the sum of floating point numbers in C programming language

Conclusion :

In this above article, you have learned how you add two types of numbers (integer and floating point number) by c programming language. Thank you for visiting my site.

Scroll to Top