# C Program to Sum Two Given Numbers

In this article, we will learn how to sum two given numbers (integer & floating points) using C programs.  In the first program, we will define fixed numbers in the variable and sum them, and in the second program, we will read two numbers from the user then sum it. Let’s do it.

## Algorithm – Sum two given numbers

• We will sum the two numbers in the main function because it is a small operation, so there is no need to create a function to sum, but if you want then you may use functions.
• We will declare three variables called `number1` for keeping the first number, `number2` for keeping the second number and `sum` to keep the sum value of the two numbers.
• Finally, we will print the value of the `sum` using `printf` function.

## Program – Sum two given numbers

``````/* C Program to sum two given numbers */
#include <stdio.h>
int main() {
int number1, number2, sum;
number1 = 24;
number2 = 19;
sum = number1 + number2;
printf("The sum is: %d\n", sum);
return 0;
}``````

NOTE: Try to use the `\n` newline character on the every `printf` statement in general.

Sample Input Sample Output
No Input The sum is: 43

Notice that we have declared two fixed values in the variables. But now we want to read the two numbers from the users and the output will be the sum of those two numbers.

HINT: We will use the `scanf` function to read the numbers from users

## Here is the program…

``````#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter the first number: ");
scanf("%d", &number1);
printf("Enter second number: ");
scanf("%d", &number2);
sum = number1 + number2;
printf("The sum is: %d\n", sum);
return 0;
}``````
Sample Input Sample Output
Enter the first number: 14
Enter second number: 33
The sum is: 47
Enter the first number: 10
Enter second number: 40
The sum is: 50

## Sum Floating-Point Numbers

To sum fractional numbers use the following in your code…

``````int main()
{
float number1, number2, sum;
printf("Enter the first number: ");
scanf("%f", &number1);
printf("Enter second number: ");
scanf("%f", &number2);
sum = number1 + number2;
printf("The sum is: %.2f\n", sum);
return 0;
}``````
Sample Input Sample Output
Enter the first number: 22.56
Enter second number: 43.20
The sum is: 65.76
Enter the first number: 121.2
Enter second number: 332.3
The sum is: 453.50

## Footnote

• We have used `%d` for integer and` %f` for floating points numbers for taking input and print out as the format specifier. It is a C basic syntax/standard.
• For printing out floating-point numbers we have used `%.2f`, it means the output will show two decimal points of the fraction. You can use your own (eg: `%.1f - %.9f`). The default is six decimal points if you use only `%f`.

Happy Coding 🙂