C Program to Add Two Numbers

In this article, we will learn about the addition of two numbers in C. we will show different approaches to adding two numbers. All these methods do the same thing, that is to add two numbers given by the user or take it on our own. Let’s discuss the C program to add two numbers.

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 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 2 numbers using Arithmetic

#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 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 2 numbers 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: Add two numbers 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 numbers 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: Add two numbers 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 2 numbers 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

In this blog, we have discussed about the program of two number in C language. We hope this article will help you clear all your doubts. In this article we have discussed different approaches of how to add two numbers in C language. Also you can practice more problems on our MYCODE platform. May we enhance your knowledge?

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 *