Addition of Two Numbers in C

Program for Addition of Two Numbers in C | Top 8 Methods

Write a Program for the Addition of Two Numbers in C Programming Language. Both inputs should be entered by the user. The program needs to pass all the conditions, for example, the number may be Integer, Float, and Double. There are many ways to get a solution but here are 8 ways to find the Sum of Two Numbers in C.

Problem Statement: The first program will ask the user to enter the first number then again the program asks for the second number, then print the sum or addition of both numbers in the new line.

Required Skills: To solve this problem you have to know about Input-output, Format Specifiers, and Addition.

Addition of Two Numbers in C Program Explanations

First Step: As we know that for this problem we need three variables, first_number, second_number, and sum, and their datatypes we are taking double for all types of addition.

Second Step: Second Step is to take input from the users and store the value in the first variable in this case the first variable is “first_number”. and repeat the step for the second value, and store the second number in the “second_number” variable.

In this article, I am going to tell you 8 methods to Program for the Addition of Two Numbers in the C Program.

Third Step: Now the most important part of the program is to add both numbers using Addition operators and assign or store the value of adding in the third variable which is “sum“.

Last Step: Print the value of the sum. This is the last step. Please read the comments in the source code for every step.

Sum of Two Numbers in C Program is tested on Dev-C++ V-5.11.

Program for Addition of Two Numbers in C

#include <stdio.h>
/* Addition of Two Numbers in C*/
int main() 
{
  double first_number, second_number, sum; 
/* Input by User */
  printf("Enter the First Number: "); 
/* First Number Entered by User */
  scanf("%lf", & first_number);
  printf("Enter the Second Number: "); 
/* Second Number Entered by User */
  scanf("%lf", & second_number); 
/* Adding First and Second Number with Addition operator */
  sum = first_number + second_number; 
/* Printing the Sum of Both Numbers */
  printf("\nSum of %lf and %lf is = %lf\n", first_number, second_number, sum);
  return 0;
}

The Sum of Two Numbers in C Using Function

#include <stdio.h>

int add(int, int);
int main() 
{
   int first_number, second_number;

   printf("Enter Two Numbers Addition: ");
   scanf("%d %d", &first_number, &second_number);

   printf("The Two Numbers Addition is : %d", addition(first_number, second_number));
   return(0);
}

int addition(int first_number, int second_number) 
{
   int i;
   for (i = 0; i < second_number; i++)
      first_number++;
   return first_number;
}

Using Subtraction for Addition

#include <stdio.h>

int main() 
{
   int first_number, second_number;

   printf("Enter Two Numbers Addition: ");
   scanf("%d %d", &first_number, &second_number);
   
   first_number = first_number - (-second_number);

   printf("The Two Numbers Addition is : %d", first_number);
   return(0);
}

Recursion: Sum of Two Numbers in C

#include <stdio.h>

int addition(int, int);
int main() 
{
   int first_number, second_number;

   printf("Enter Two Numbers Addition: ");
   scanf("%d %d", &first_number, &second_number);

   printf("The Two Numbers Addition is : %d", addition(first_number, second_number));
   return(0);
}

int addition(int first_number, int second_number) 
{
if (!first_number)
      return second_number;
   else
      return addition((first_number & second_number) << 1, first_number ^ second_number);
}

Addition of Two Numbers in C Using While Loop

#include <stdio.h>
int main() 
{
   int first_number, second_number;

   printf("Enter Two Numbers Addition: ");
   scanf("%d %d", &first_number, &second_number);
   
   while (second_number > 0) 
{
      first_number++;
      second_number--;
   }

   printf("The Two Numbers Addition is : %d", first_number);
   return(0);
}

Using Logarithm and Exponential Function

#include <stdio.h>
int main(void)
{
   int first_number, second_number;
   scanf("%d %d", &first_number, &second_number);

	printf("%g\n", log(exp(first_number) * exp(second_number)));

	return 0;
}

Half Adder Logic

#include <stdio.h>

int addition(int, int);

int main() 
{
   int first_number, second_number;
   scanf("%d %d", &first_number, &second_number);

   printf("The Two Numbers Addition is : %d", addition(first_number, second_number));
   return(0);
}

int addition(int num1, int num2)
{
	if (!num2)
		return num1;

	int s = num1 ^ num2;
	int c = (num1 & num2) << 1;
	
	return addition(s,c);
}

Addition of Two Numbers in C Using printf()

#include <stdio.h>

int addition(int, int);

int main() 
{
   int first_number, second_number;
   scanf("%d %d", &first_number, &second_number);

   printf("The Two Numbers Addition is : %d", addition(first_number, second_number));
   return(0);
}

int addition(int a, int b)
{
	// %*s  means print a character * number of times
	return printf("%*s%*s", a, "",  b, "");
}

The Output Addition of Two Numbers

Below is the Output Screenshot of the Sum of Two Numbers in the C Program.

Program for Addition of Two Numbers in C
Addition of Two Numbers in C

This is a beginner level for newbie coders or programmers. This is a part of 40+ Basic Programming Exercises and Solutions in C Language. What do think about 8 methods of Program for the Addition of Two Numbers in C Using Functions?

Similar Exercises

  • Perform All Arithmetic Operations – Addition, Substation, Multiplication, and Division | User input.
  • Input/Output of All Basic Data Types.
  • Print 1 to 100 Numbers
  • Greatest of Three Numbers
  • Print Even or Odd
  • Area of Triangle – Mathematics based
  • LCM and GCD of Two Numbers
  • Find the Area of an Equilateral Triangle
  • Print All the Odd Number Till ‘N’
  • Swap Two Numbers Using 3rd Variable

2 thoughts on “Program for Addition of Two Numbers in C | Top 8 Methods”

Comments are closed.