Addition Program in C
The first thing you must do is include the following line of code at the beginning of your file that includes the standard input/output header file stdio.h:
#include<stdio.h>
A preprocessor directive is this line. programme contains the standard input and output header file stdio.h. The functions required to read user input and print outputs to the console are provided by this header file.
The main function has to be defined next. Your program's main function serves as its entry point and is where execution will start. This is how the main function can be defined:
Int main(){
//set of lines of code
Return 0;
}
The primary purpose of your program is this. It serves as your program's entrance point and is where it will start running. The function's return type is indicated by the int prefix before main(). The program will run the code that you put inside the curly brackets following main(). The main function's closing return 0; declaration confirms that the program ran correctly.
You can set up variables that store the two figures you want to add inside the main function. The data type int can be used for this, as shown below:
int x, y;
printf("Enter the first number: ");
The numerical values of number 1 and num2 can then be read in from the user using the scanf() method. Input from the keyboard is read by the scanf() method and saved in a variable. It can be used as follows:
scanf("%d", &x);
printf("Enter the second number: ");
scanf("%d", &y);
Once you have the values of num1 and num2, you can add them together and store the result in a third variable called sum. You can do this using the + operator, like so:
int z= x+ y;
Finally, you can use the printf() function to display the result of the addition. The printf() function displays output to the console. You can use it like this:
printf("The sum of %d and %d is %d.\n", x, y,z);
This line makes use of the printf() characteristic to display the result of the addition to the console. The message "The sum of " is printed, followed with the aid of the values of x and y, after which the end result of the addition (saved inside the y variable) is outlined. The n at the end of the message is an get away sequence that adds a newline person to the output, so that the following message will start on a brand new line.
The complete code for the addition program would look like this:
#include <stdio.h>
int main() {
int x, y, z;
printf("Enter the first number: ");
scanf("%d", &x);
printf("Enter the second number: ");
scanf("%d", &y);
z = x + y;
printf("The sum of %d and %d is %d.\n", x, y, z);
return 0;
}
Output:
Conclusion:
In conclusion, a simple yet fundamentally important program that all programmers should be able to build is an addition program in C. We discussed the syntax, source code, and a C example for an addition program. A person input is required, two integer variables are defined, addition is accomplished, the end result is stored in a special variable, and the consumer is shown the end result.As a result of the code's simplicity and readability, beginners learning how to program in C are advised to start with it.
The main function, and which acts as the program's entry point, and the required header file are introduced first. The next section of the describes how to create variables that are read user input, add, and show the result using the printf() function.
Data types are a crucial idea to bear in mind when designing C programs. In this program, the values that will be added are stored in integer variables. Given the kind of information you intend to keep in your variables, it's critical to select the appropriate data type. There are numerous modifications you can create addition to the fundamental addition program to advance your programming abilities. You could, for instance, design a program that sums more than two numbers or one that employs floating-point variables rather than integers. Overall, creating an addition program in C is a great way to begin programming and understand some of the fundamental principles of programming. You can build more sophisticated programs and advance your programming abilities with a little practice.