Add two numbers in C Programming

Add two numbers in c programming, step by step guide with an easy explanation and detailed source code.

Introduction: Add two numbers in C

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

Add two numbers in C, is the basic 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.

The addition of two numbers also explains the use of basic functions of input and output (scanf and printf) in programming.

This blog demonstrates two methods two add two numbers. One is a static method and the other is dynamic where the program accepts input from the user. To know more about C Programming and it’s history please read this blog first “C Programming Complete Guide 2020

You can watch the video over here which explains the blog in simple video with VFX animation.

If you want to see the complete course with all the topics of C covered you can refer this link of C Programming

Add Two Number in C without scanf statement

This method doesn’t use scanf function and embed 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, c.

main()
{
int a;
int b;
int c;
}

we want to tell you that we can create as many variables in a single line while using int keywords only one time.

main()
{
int a, b, c;
}

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 program or business logic. Though we have used it to add two numbers in C Programming, this is mainly used in loops in C.

main()
{
int a=10;
int b=20;
int c;
}

Business 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 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 we have done? We have created 3 variables. A and b act as input variables, while C stores the result of 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. 

Add two numbers in C Programming with scanf Statement.

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

Declaration of Variables

Here we will declare the a,b,c variable 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 integer we will use the word int

main()
{
int a,b,c;
}

Accepting User Inputs using scanf statement

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

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

Business 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 printf statement to display the final result

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

The output of addition Program in C

Let’s look at the output

When we will 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.

25

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 are writing a printf statement to give a title, and another line to ask the user to give the first input.

Same for the second input. Now we will print results in a decent way.

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);
}

Let’s execute the program. As we execute it, the system displays the title first.

You can view the source code from Newtum github repository  link over here.

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 scanf takes the input. It has two parameters, First are placeholders of variables and 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 its a syntax, I mean a way for writing a scanf statement.

We have used the printf statement to give the output and scanf statement to take input.

You can download the code from below link

  {github.com/newtum.}

How to view all topics on C Programming

Watch full programming series on Amazon Prime free with Amazon Prime Membership.

Amazon Prime link for the USA: https://www.amazon.com/dp/B07WFYJW6G

Amazon Prime link for the UK: https://www.amazon.co.uk/dp/B07WFYQZ1G


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

Frequently Asked question about adding two numbers in C Programming

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

For the addition of two number, 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);

printf():
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 to the stored integer value and third variable for the result.



Do we need to write different functions to add two numbers in C

Facebook Comments
(Visited 55 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *