Addition of Two Numbers in C

This program explains how to write the addition of two numbers in c. For an explanation, we have used only integer variables. This program accepts two inputs using the scanf input function. Then adds the two numbers and store them in the third variable. The output is displayed using the printf statement.

//SUM of two numbers in C Programming
main()
{
int a,b,c;
printf("Welcome to program of Addition");
printf("\nEnter value for a : ");
scanf("%d", &a);
printf("\nEnter value for b : ");
scanf("%d", &b);
c=a+b;
printf("Result : %d",c);
}

Adding two numbers in C is the primary program taught while learning C Programming in each and every C Programming course. Adding two numbers in C teacher’s basics of variable declaration and very basic of mathematical operators.

C Program to Add Two Numbers explains the use of basic functions of input and output (scanf and printf) in programming. This blog demonstrates two methods to add two numbers in C programming.

One is a static method, and the other is dynamic, where the program accepts input from the user. To learn more about C Programming and its history, please read this blog first, “C Programming Complete Guide”. You can watch the video over here, which explains the blog in a simple video with VFX animation.

If you want to see the complete course with all the topics of C programming covered, you can refer to this link for C Programming with Certification.

C Program to Add Two Numbers using scanf() Statement

How to achieve this. Now we will be learning a new function called scanf(). scanf() takes the input from the users and does the processing.

Declaration of Variables

Here we will declare the a,b, and c variables in one line. Since no initialization is required, we can declare the variables in one line. Since we are assuming all the values to be integers, we will use the word int.

Accepting User Inputs using scanf statement

C uses a scanf statement to accept two numbers. Since we are accepting input in variables a,b and both are integers, our input statement to add two numbers in C Programming will look like the below code.

Simple Logic to add two numbers in C Programming

Simple logic is very, very easy to add two numbers. First, We will write a small mathematical expression “c = a+ b”, and then we will use the printf statement to display the final result.

main()
{
int a,b,c;
scanf("%d", &a);
scanf("%d", &b);
c=a+b;
print("%d",c);
}
Output

Let’s look at the output. When we execute the program, a black output screen will appear. It’s because of a scanf. It’s waiting for our input. Now since we have specified scanf twice, it will wait for two inputs. Let’s give 21 as input. One input is gone, and now the program is waiting for the second input. Now enter value 4 and again press enter. You will see 25 as output.

Only scanf waits for input. Once the input is given, the program runs the expression a+b, stores the result in c, and then prints the value of c.

Making Your Program User-Friendly

Before understanding the scanf and this program in detail, let’s modify it so that it’s more user-friendly. After declaring variables, we write a printf statement to give a title and another line to ask the user to give the first input. Same for the second input.

main()
{
int a,b,c;
printf("Welcome to program of Addition");
printf("\nEnter value for a : ");
scanf("%d", &a);
printf("\nEnter value for b : ");
scanf("%d", &b);
c=a+b;
printf("Result : %d",c);
}

“Need more clarity? Watch our YouTube video for detailed insights:

Let’s execute the program. As we execute it, the system displays the title first. You can view the source code from the Newtum GitHub repository.

Output :
Welcome to program of Addition 
Enter value for a : 12 
Enter value for b : 18 
Result : 30

I hope you got some more understanding of the printf and business logic or problem definition in the C programming statement. Let’s explore the scanf statement. I am saying again that scanf takes the input. It has two parameters, the First is placeholders of variables, and the second is the variable followed by an ampersand (&). Ampersand is the biggest and most difficult part of the C language.

For now, just assume that it’s a syntax, I mean a way for writing a scanf statement. We have used the printf statement to give the output and the scanf statement to take input. You can download the code from here.

For Other Countries: To watch C Programming Content in another country, please visit the URL:https://newtum.com/course-details/c-programming-online.

Add Two Numbers in C without using scanf statement

This method doesn’t use the scanf function and embeds hardcoded values in the variable. To change the program, we have to edit the program to put new values. Though this method is easy, it’s not good for professional C programming. Since we need to understand this concept, we have explained this method.

Declaring Variables in C Program

Here, to start, we will create a program that will add two numbers. Here we will create 3 integers using the keyword int variables a, b, and c. We want to tell you that we can create as many variables in a single line while using int keywords only one time.

Initialize variable

Now we put value 10 in a and value 20 in b. Initialization is a process in C Programming where we initialize variables at the start of the program or business logic. Though we have used it to add two numbers in C Programming, this is mainly used in loops in C.

Simple Logic to add two numbers in C Programming

Business logic is very, very easy to add two numbers. First, We will write a small mathematical expression “c = a+ b”, and then we will use the printf statement to display the final result.

main()
{
int a=10;
int b=20;
int c=a+b;
printf("%d",c);
}

Please make sure that all the statements are followed by a semicolon. I have explained the reason in the very first chapter. Now we print the variable c. Execute the program.

Output : 
30

Well, you got your answer as 30.

So what have we done? We have created 3 variables. A and b act as input variables, while C stores the result of an expression, i.e. a+b, and we print the result. It’s a pretty simple program. But we can’t have fixed values, i.e. a=10 and b=10. And everyone doesn’t know programming languages. We need to prepare a program that can be used by anyone. We don’t have to write values of a and b via editing the program.

Frequently Asked question about adding two numbers in C Programming

What is the important function required to write a program to add two numbers in C

For the addition of two numbers, the scanf and printf function is important.

scanf():
  • scanf() function is used to take input from the user.
  • This function allows you to accept input from standard in, which for us is generally the keyboard.
  • The simplest application of scanf looks like this: scanf(“%d”, &a);
print():
  • printf() function is used to display output to the user.
  • printf() function is used to print the “character, string, float, integer values” onto the output screen.

Number of a variable required to add two numbers in C Programming

At least three variables are required for the addition of two numbers. In which two variables are used for the stored integer value and a third variable for the result.

About The Author