# Algorithm Flowchart and C program for Adding Two Numbers

To write a C program for adding two numbers, the problem should first be identified and analyzed to determine the step-by-step solution flowchart. These steps are part of program design. In general, the program can be written quickly by reading books or searching online or watching videos. Doing so is called memorization. Even if you get full marks in the exam book, you won’t understand how the program actually works, and you won’t be able to write a program for a new problem if you don’t understand the basics. Just think, how does it do it? So let’s learn a few things.

Our problem is to create a program or software to add two numbers, just like a calculator adds up. If we analyze the problem, we will find that a person wants to add two numbers with a computer (not a calculator because the calculator is also a program created by another person, and we use it, just like that, we will make a calculator with which someone else can add).

Therefore, we need to assume two variables for the two numbers. For example, while doing arithmetic, we often make assumptions such as thinking that the number “x” is like that, and an input box to enter two numbers. From the mathematical formula, we know that the addition formula is “x = a + b,” where “x” is the answer, and “a” and “b” are the two numbers.

Now let’s figure out the flowchart. Algorithms are written in flowcharts and points in the figure.

Step-1: Start the program.

Step-2: Determine the variables. Here, in the case of two numbers, the first number = “a” and the second number = “b.” Also, take “answer = x” to get the answer.

Step-3: Take input. Ask the user for the first number. If the user enters the first number, it should continue with “a.” Then ask the user for the second number. If the user enters the second number, it should continue with “b.”

Step-4: Process. Here is the formula to add. Then the numbers placed in “a” and “b” will be added according to that formula. The sum will be stored in the variable “x.”

Step-5: Display the answer stored in “x” to the user.

Step-6: End the program.

We have done the solution of the problem, i.e., the addition rule step-by-step algorithm flowchart. Now write it in the C language, i.e., write the actual program in the C language to add. Let’s get started.

#include<stdio.h>

int main( ){

int a, b, x ;

printf(“enter your 1st number”);

scanf(“%d”,&a);

printf(“enter your 2nd number”);

scanf(“%d”,&b);

x = a + b ;

printf(“total number is=%d”,x);

return 0;

}

Did you not understand something? Let’s come to the explanation.

The first line is “#include .” The “#” is called a preprocessor. In simple words, the program is ready to start from a location in the computer’s RAM. Remember that everything a computer does is stored in RAM. Are you reading a book? The book is placed on the table. Cutting curry for cooking? Everything has to be put into something, cut, and put back into something as well. When something is turned on in the computer, it comes into RAM and starts from RAM. Keeping it in the hard disk or memory? There is only accumulation. When something needs to run, it goes into RAM and starts. So program codes are kept in RAM by defining “#”. Next is “include,” which simply means instructions to add. Thinking about what to add? Next is “” where “std” means standard, and “io” means input-output. That is, a library of all functions of what will be input and what will be output.Suppose I gave you an egg and told you to fry it and eat it. Note that I only gave you the egg and nothing else, but for frying, you need a stove, a pan, oil, salt, and even fire. However, I didn’t give them because they are in the kitchen. So, pick up only what you need and work with it.

This is precisely what a library does in C programs. What happens in the library, why it happens, and how it happens are all connected, so this library must be added.

The second line, int main( ), although the second bracket should end here, I took it like this because there are many things inside the bracket. Here, the first int means initialization, and the process of getting started is straightforward. main( ) A program must have only one main function, and if any other function is needed, it must be inside this main function. After the second bracket starts, enter the instruction code required by your program.

Int a, b, x; a semicolon must be placed at the end of each complete statement in a C program. Don’t forget to do this; otherwise, the program won’t run. Although this line has an int at the beginning, it is not the int before main. Here, int stands for integer (some say integer), meaning a whole number, preferably a decimal integer. That is, the number given for addition must be a whole number, not a fraction. There are other rules for fractions. Then a, b, x are variables that you assume to do the math. When the user enters the first number, it should be placed in a, i.e., a = the first number. Similarly, b = the second number, and x is the answer, so the answer to adding the two numbers should be placed in x, i.e., x = the answer. Since int a, b, x; is an instruction or statement, the semicolon is given at the end of the line, and it is mandatory; otherwise, it will not happen.

Next, see printf(“Enter your 1st number”);

Here, printf(” “); is a library function, and I added include to it. The job of this function is to display the contents on the screen, e.g., printf(“Enter your 1st number”);. In this line, printf(” “); the function displays the message “Enter your 1st number” on the screen. What the user will understand is that they are being asked to enter the first number. You can write whatever you want after it; only what you type will show.

The next line, scanf(“%d”, &a);, is also a library function. This function can hold the number entered by the user. % is in decimal form, and d is the data type. So %d represents a decimal number, i.e., 1, 2, 3, etc. Then, &a represents the first number you assumed. In scanf(” “), the function takes the number entered by the user and places it in a’s position, i.e., a = the number entered.

printf(“Enter your 2nd number”);

scanf(“%d”, &b);

These two lines are similar to the explanation of the above two lines, but here, only the second number is meant.

Then x = a + b; This is the formula that tells what to do with the two numbers. Vyas library will understand. It adds the numbers in the position of a and b and leaves the result in the position of x, but it won’t show. You will not see it.

Then, printf(“The total number is= %d”, x); Here, printf(” “); shows the contents on the screen,After executing the line printf(“total number is=%d”,x);, a space is printed to the screen with the printf(” “); command. As mentioned before, this displays the contents on the screen. However, note that the line total number is= will also be displayed on the screen. The %d, followed by x inside the line, will not be shown directly. This is because the line x = a + b; contains the sum of a and b in the x variable. Therefore, x=answer will display the answer number instead of x. Moreover, %d is the data type of the answer, which is a decimal integer.

The return 0; statement is interpreted differently by different people. When we call the library by using #include, return 0; is used to indicate that the program has successfully completed its work. It means that the program no longer returns anything internally. If 0 is not given, it means the program has failed. However, this does not change the functions. It can be replaced by something else, which will be shown later. Using return 0; causes the program to stop executing immediately after displaying the result.