Addition of two number in c
In programming, it's important to understand the basics, and addition is one of the most fundamental concepts there is. We'll look at how to write an addition programme in the C programming language in this blog. Regardless of your level of experience, it will provide you with the syntax, sample code, and results needed to understand and build an addition programme correctly.
Let's get acquainted with the C programming language's grammar before getting into the code. An addition program written in C must adhere to the following syntax:
Syntax:
#include <stdio.h>
int main() {
// Variable declarations
// Inputs
// operation
// Output
return 0;
}
Syntax Explanation:
By using the #include <stdio.h> to include the standard input-output library, we may use methods like printf() and scanf().
The integer main() It is the main function that initiates and ends the program's execution.
Introducing variables: This section should contain the declaration of any variables required for the addition operation. Here, you request that the user input the integers that require addition.
Operation of addition: Perform the addition operation using the previously mentioned variables.
After everything is said and done, you output the result of the addition.
return 0; This message indicates that the programme execution was successful.
Example:
Let's examine some sample C code that exemplifies an addition program:
#include <stdio.h>
int main() {
int num1, num2, num3, sum;
printf("Enter three numbers: ");
scanf("%d %d", &num1, &num2&num3);
sum = num1 + num2+num3;
printf("The sum is: %d\n", sum);
return 0;
}
Output:
Enter three numbers: 15 20 30
The sum is: 65
Explanation
The user inputs the numbers 15,20 and 30 in this example. Following the addition of these numbers, the programme displays the outcome, which is sixty five.
The h header file must first be included in this code in order to allow the input and output functions. The next step is to declare num1, num2, num3 and total, the four variables that will store the input numbers and the result of the addition.
The user is then asked for three numbers, which are read and saved in num1 and num2,num3 respectively, using the printf() and scanf() functions. The scanf() function employs the %d format specifier to read integer integers.
To perform the addition operation, add each of their amounts to the sum variable.
The output is then formatted by the programme using the printf() function, and the result is then shown using the format specifier %d for integers.
Comments
Post a Comment