# C Program to Sum Two Given Numbers

## Write a C Program to Sum of 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.

## Required C Concept

Variables | printf & scanf | Operators |

## 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`

.

- C program to print Integer & Decimal number given by user
- C Program to perform all arithmetic calculation
- C program to ask two numbers and find their sum
- C program to check a number is even, odd or zero
- C program to swap between two numbers
- C program to sum of natural numbers up to N
- C program to calculate the factorial of a number
- C program to make a multiplication table
- C program to calculate GCD of two numbers
- C program to calculate LCM of two numbers
- C program to count number of digits in a number
- C program to reverse a integer number
- C program to find the power of a number
- C program to check whether a number is palindrome or not
- C program to count and print prime numbers up to the N
- C program to check armstrong numbers
- C program to check armstrong numbers between two integers
- C program to find factors of a number
- C program to fined the size of all data types
- C program to print largest number among three numbers
- C program to check a number is positive or negetive
- C program to calculate the factorial of a number
- C program to form any kind of pyramid and stracture
- C program to find all the prime numbers between two numbers
- C program to check whether a number is prime or not
- C program to make a simple calculator
- C program to calculate the perimeter of a rectangle
- C program to calculate the area of a rectangle
- C program to calculate the diameter and area of a circle
- C program to convert kilometer to meter and centimeter
- C program to convert temperature celsius to fahrenheit
- C program to convert temperature fahrenheit to celsius
- C program to ask for days and convert into years, week and days
- C program to calculate the square root of a given number
- C program to find third angle of a triangle where two angles are given
- C program to calculate the area of a triangle
- C program to calculate area of an equilateral triangle
- C program to calculate average and percentage of result
- C program to calculate simple loan interest
- C program to compound loan interest
- C program to display fibonacci series of a number
- C program to swap two numbers without using third variable
- C program to print Hello World without using semicolon
- C program to insert assembly code and run it
- C program to printf Hello world without main() function
- C program to to calculate the multiplication of two matrix
- C program to convert decimal numbers into binary
- C program to form a triangle made with alphabet
- C program to form a triangle made with numbers
- C program to form a fibonacci triangle
- C program to convert a number into charecters

Happy Coding 🙂