Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Add Two Numbers

Last Updated on May 9, 2023 by Prepbytes

You will learn how to create a C program that adds two integers in this tutorial. This is a very simple C program that asks the user to enter two integers, saves those inputs in two different variables, and then shows the total of the two values.
To determine the sum of two integer numbers provided by the user, we shall create two variables. The user is prompted to enter two integer values in the first variable, after which the variable shows the total of the two integers. Using a user-defined function, we are carrying out the identical action in the second C program.

What is the C Program to Add Two Numbers?

The C program to Add Two Numbers instructs us to sum two integer numbers after adding them together, and then output the result. Let’s look at the C programming language algorithm for adding two integers, which is a straightforward process.

Algorithm to Add Two Numbers in C

  1. Start
  2. Declare variables num1, num2, and res
  3. Read values for num1 and num2
  4. Add the values of num1 and num2 and assigned the result to a res variable
  5. Print res
  6. Stop

Method 1: Add Two Numbers in C using the Arithmetic Addition(+) Operator

In this method, we will see a C program to add two numbers using the plus(+) arithmetic operator.

Code Implementation to Add Two Numbers in C using Arithmetic Operator

#include <stdio.h>

int main()
{
    int num1 = 10, num2 = 20, res;
    
    res = num1 + num2;
    printf("Sum of numbers %d + %d = %d", num1,num2,res);
    
    return 0;
}

Output

sum of number 10 + 20 = 30

Method 2: Add Two Numbers in C using a User-defined Function.

In this method, we will see a C program to add two numbers in which the user allows to insert the values of two numbers using a user-defined function.

Code Implementation to Add Two Numbers in C using User Defined Function

#include <stdio.h>

int main()
{
	int A, B, sum;
	
	printf("\nEnter the First Number: ");
	scanf("%d",&A);
	
	printf("\nEnter the Second Number: ");
	scanf("%d",&B);
	
	sum = A + B;
	printf("Sum of Numbers %d + %d = %d", A,B, sum);
	
	return 0;
}

Output:

Enter the First Number: 20
Enter the Second Number: 20
Sum of Numbers 20 + 20 = 30

Method 3: Sum Of Two Numbers In C Using Functions

In this method, we will create a function that accepts two arguments and return the sum of those two arguments. We will call that function inside out the main() function.

Code Implementation to Add Two Numbers in C using Functions

#include <stdio.h>


int add(int x, int y)
{
	return x + y;
}

int main()
{
	int A, B, sum;
	
	printf("Enter the First Number: \n");
	scanf("%d",&A);
	
	printf("Enter the Second Number: \n");
	scanf("%d",&B);
	
	sum = add(A, B);
	printf("Sum of Numbers %d + %d = %d",A,B,sum);
	
	return 0;
}

Output:

Enter the First Number: 10
Enter the Second Number: 10
Sum of Numbers 10 + 10 = 20

Method 4: Sum Of Two Numbers In C Using Recursion

In this method, we will use a recursive function to apply the addition of two numbers. In which the function calls itself from its definition part.

Code Implementation to Add Two Numbers in C using Recursion

#include <stdio.h>
int addFun(int, int);
int main()
{
    int A, B, sum;
    printf("Enter Two Numbers: ");
    scanf("%d%d",&A,&B);
    sum = addFun(A, B);
    printf("\nResult = %d",sum);
    return 0;
}
int addFun(int a, int b)
{
    if(b==0)
        return a;
    else
        return (1+addFun(a, b-1));
}

Output:

Enter Two Numbers: 10 10
Result = 20

Time Complexity: O(1)
Space Complexity: O(1)

Conclusion
We have covered the C program to add two integers in this blog. We hope that the article will assist you to understand everything. The many methods for adding two numbers in C have been covered in this article.

FAQs related to the C Program to Add Two Numbers

Q1. How to add variables in C?
Ans. Where type is one of the C types, like int, and variableName is the variable’s name, like x or myName, type variableName = value. The variable is given a value by using the equal sign.

Q2. What is %d in C?
Ans. In the C programming language, the format specifiers %d and %i specify the type of the variable as either decimal or integer, respectively. When printing a number with %d or %i, there is no change in the output of the printf() function; but, with %scanf, there is a difference.

Q3. What is C program syntax?
Ans. The collection of guidelines guiding the creation of programs in the C programming language is known as syntax. It’s intended to enable programs that be incredibly brief, closely related to the resultant object code, and still offer a fair amount of high-level data abstraction.

Q4. What is an array in C language?
Ans. The definition of an array in C is a way to bring together many items of the same type. These things or things can have data types like int, float, char, double, or user-defined data types like structures.

Other C Programs
C program to calculate percentage of 5 subjects
C program to convert binary number to decimal number
C program to convert celsius to fahrenheit
C program to convert infix to postfix
C program to find area of circle
C program to find roots of quadratic equation
C program to reverse a number
C program for merge sort for linked lists
C program for performing bubble sort on linked list
C program to reverse a linked list

Leave a Reply

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