addition of two numbers in c
Addition of Two Numbers in C
To add two numbers in the C programming language, you can follow these steps:
Declare variables: Start by declaring two variables to store the numbers you want to add. For example, you can declare two variables
num1
andnum2
of typeint
to store integer values.Input values: Use the
scanf
function to input the values ofnum1
andnum2
from the user. This allows the user to enter the numbers to be added.Addition: Create a new variable, let's call it
sum
, to store the sum of the two numbers. Assign the sum ofnum1
andnum2
to thesum
variable using the addition operator+
.Output the result: Use the
printf
function to display the result to the user. You can use the format specifier%d
to print the value of thesum
variable.
Here's an example code snippet that demonstrates the addition of two numbers in C:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
In this example, the user is prompted to enter two numbers. The program then calculates the sum of the two numbers and displays the result to the user.
Please note that this is a basic example and assumes that the user will input valid integer values. Additional error handling and input validation can be added for more robust code.
[[SOURCE 7]]