The WP Debugging plugin must have a wp-config.php file that is writable by the filesystem.

C Program to Add Two Numbers

C Program to Add Two Numbers

In this article, we will learn about the addition of two numbers in C also C program to add two numbers using different approaches. 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.

What is the C program to add two numbers?

The C program to Add two numbers means, given two integer numbers, we have to add those two numbers and have to print the sum of those two numbers. Add two numbers in C is a simple task, lets see the algorithm to add two numbers in C programming language.

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 2 numbers in C 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 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 2 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 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 2 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

In this blog, we have
discussed the c program
to add two numbers. 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 *