C program to add two numbers

devquora
devquora

Posted On: Jan 26, 2023

C program to add two numbers

 

Overview:

Before, Starting this article First of all, I want to introduce you to Mathematics, Arithmetic, Then I will explain What is a Number? and Definition of Addition. Furthermore, I will demonstrate and write logic for the addition process with an example. After all, I will show a C Program to add two numbers using 3 way i.e. (1) C program to add two integers in a simpler way with output. (2) C program to add two real numbers by using a function with output. (3) C program to add two numbers according to the user's choice with output. After that, I will explain the internal execution of the program.

Table of contents:

  1. What is Mathematics?
  2. What is Arithmetic?
  3. What is a Number?
  4. Definition of Addition.
  5. Main Logic for Addition
  6. Demonstration of Addition process.
  7. C program to add two integers in a simpler way with output.
  8. C program to add two real numbers by using the function with output.
  9. C program to add two numbers according to the user's choice with output.
  10. Explanation of the internal execution of the program as Conclusion.

What is Mathematics?

Mathematics is a approach of calculation that uses numerals to perform any task.

What is Arithmetic?

Arithmetic is the branch of mathematics dealing with the properties and manipulation of numbers. It has properties of operations such as addition, subtraction, Multiplication, division, modulus, increment and decrement, etc.

What is a Number?

Number is an object that uses digits to perform mathematical tasks. Calculus, which is a branch of mathematics includes many numbers such as integers, whole numbers, real numbers, and imaginary numbers, etc. Here, I will discuss about number to perform arithmetic addition operation. Moreover, Number is a combination of digits, some symbols and decimal point. For instance, 127.23 is a decimal number.

Definition of Addition.

Addition is the arithmetic operation which is the action or process of adding the first number to another. Addition is written using the plus sign "+" between the terms. For instance, If you want to add two integers such as 24 and 12 the summation of these two numbers is 36 i.e. 24+12=36.

Main Logic for Addition

  • Step 1: Set up the numbers vertically
  • Step 2: Star from the last right digits of numbers and add the digits in the ones place. Write the one's digit of it in the answer’s ones place and carry the other in the next tens place and add it with ten's digits.
  • Step 3: Repeat Step 2 for all places until the first left digit of numbers.

Demonstration of Addition process:

Demo of Addition

C program to add two integers in a simpler way with output.

#include<stdio.h>
#include<conio.h>
void main()
{
	//ADDITION IN SIMPLER WAY
	int num1,num2,add;
	printf("Please!Enter First number to that you want to add: ");
	scanf("%d",&num1);
	printf("Please!Enter Second number to that you want to add: ");
	scanf("%d",&num2);
	add=num1+num2;
	printf("The equivalent addition is %d.",add);
} 

The output of the Program:

Please!Enter First number to that you want to add: 28

Please!Enter Second number to that you want to add: 92

The equivalent addition is 120.

Addition

C program to add two real numbers by using a function with output.

#include<stdio.h>
#include<conio.h>
 
int add(int num1, int num2);
 
int main()
{
    int num1, num2, res;
    printf("\n Enter First Number:\t");
    scanf("%d", &num1);
    printf("\n Enter Second Number:\t");
    scanf("%d", &num2);
    res = add(num1, num2);
    printf("\n Addition of Two Numbers: %d", res);
    printf("\n");
    return 0;
}
 
int add(int num1, int num2)
{
    int sum;
    sum = num1 + num2;
    return sum;
}

The output of the Program:

Addition through function

C program to add two numbers according to the user's choice with output.

#include<stdio.h>
#include<conio.h>
int main()
{
  double num1, num2, add;
  char ch;

  while (1) {
    printf("Input First integer! ");
    scanf("%lf", &num1);
    printf("Input Second integer! ");
    scanf("%lf", &num2);
    getchar();

    add = num1 + num2;

    printf("(%lf) + (%lf) = (%lf)\n", num1, num2, add);

    printf("Do you wish to add more numbers (y/n)\n");
    scanf("%c", &ch);

    if (ch == 'y' || ch == 'Y')
      continue;
    else
      break;
  }

  return 0;
}

The output of the Program:

Addition through choice

Conclusion: Explanation of This Program with output. The internal working of this program depends upon the previous demonstration of addition of numbers.

This Program stores two integers into the int datatype by using to variables. After that store the sum of these two variables into another variable and display it on the screen.

After the successful compilation of the program, Program sums up the two entered numbers at once.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..