The Wayback Machine - https://web.archive.org/web/20240710002409/https://www.geeksforgeeks.org/c-program-to-add-two-integers/
Open In App

C Program to Add Two Integers

Last Updated : 02 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to write a C program for the addition of two integers. In C, the sum of two numbers can be done by the addition operator (+). This operator works by taking two operands and returning their sum as the result.

C Program to Add Two Integers

C Program to Add Two Numbers

Below is the program to find the sum of two numbers in C (more specifically integers) entered by the user.

C
// C program to add two numbers
#include <stdio.h>

int main()
{
    int A, B, sum = 0;

    // Ask user to enter the two numbers
    printf("Enter two numbers A and B : \n");

    // Read two numbers from the user || A = 2, B = 3
    scanf("%d%d", &A, &B);

    // Calculate the addition of A and B
    // using '+' operator
    sum = A + B;

    // Print the sum
    printf("Sum of A and B is: %d", sum);

    return 0;
}

Output
Enter two numbers A and B : 
Sum of A and B is: 0

Complexity Analysis

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

Explanation

In the above program, the user is first asked to enter two numbers, and the input is scanned using the scanf() function and stored in the variables A and B. Then, the variables A and B are added using the arithmetic operator + (addition operator), and the result is stored in the variable sum.



Similar Reads

C Program to Find Armstrong Numbers Between Two Integers
Prerequisite: Program for Armstrong Numbers A positive integer of n digits is called Armstrong number of order n (order is the number of digits) if abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + …. Here, we will build a C Program to print Armstrong Numbers between two integers. Example: 153 is an Armstrong number 1*1*1 + 5*5*5 + 3*3*3 = 153 37
3 min read
C Program to Add Two Complex Numbers
Complex numbers are those numbers that can be expressed in the form of "a+ib" where a and b are the real numbers and i is the imaginary part called "iota" The value of i is √-1. In this article, we are going to add two complex numbers using a C program. Example of Add Two Complex NumberInput: a = ( 2 + 3i ) b = ( 4 + 5i ) Output: Sum = ( 6 + 8i ) E
3 min read
C Program To Add Two Numbers Represented By Linked Lists- Set 1
Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5-&gt;6-&gt;3 // represents number 563 List2: 8-&gt;4-&gt;2 // represents number 842 Output: Resultant list: 1-&gt;4-&gt;0-&gt;5 // represents number 1405 Explanat
4 min read
How to sum two integers without using arithmetic operators in C/C++?
Given two integers a and b, how can we evaluate the sum a + b without using operators such as +, -, ++, --, ...? Method 1 (Using pointers)An interesting way would be: C/C++ Code // May not work with C++ compilers and // may produce warnings in C. // Returns sum of 'a' and 'b' int sum(int a, int b) { char *p = a; return (int)&amp;amp;p[b]; } Despite
4 min read
C program to input an array from a sequence of space-separated integers
Given a string S consisting of space-separated integers, the task is to write a C program to take the integers as input from the string S and store them in an array arr[]. Examples: Input: S = "1 2 3 4"Output: {1, 2, 3, 4} Input: S = "32 12"Output: {32, 12} Approach: The idea is to solve the given problem is to use getchar() function to check if a
2 min read
Add two numbers using ++ and/or --
Given two numbers, return a sum of them without using operators + and/or -, and using ++ and/or --.Examples: Input: x = 10, y = 5 Output: 15 Input: x = 10, y = -5 Output: 10 We strongly recommend you to minimize your browser and try this yourself first The idea is to do y times x++, if y is positive, and do y times x-- if y is negative. C/C++ Code
4 min read
C Program to Add 2 Binary Strings
Given two Binary Strings, we have to return their sum in binary form. Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit. Input: 11 + 11 Output: 110 C/C++ Code // C Program to Add 2 Binary Strings // and Print their Binary Sum #include &lt;stdio.h&gt; #
8 min read
LEX program to add line numbers to a given file
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Example: Input: #include&lt;stdio.h> // Driver Code int
2 min read
C Program to Add N Distances Given in inch-feet System using Structures
Given an array arr[] containing N distances of inch-feet system, such that each element of the array represents a distance in the form of {inch, feet}. The task is to add all the N inch-feet distances using structures. Examples: Input: arr[] = { { 10, 3.7 }, { 10, 5.5 }, { 6, 8.0 } }; Output: Feet Sum: 27 Inch Sum: 5.20 Input: arr[] = { { 1, 1.7 },
3 min read
Number of N digit integers with weight W
Given N, the number of digits of an integer which is greater than or equal to 2 and a weight W. The task is to find the count of integers that have N digits and weight W.Note: Weight is defined as the difference between the consecutive digits of an integer. Examples: Input : N = 2, W = 3 Output : 6 Input : N = 2, W = 4 Output : 5 In the above examp
7 min read
Article Tags :
three90RightbarBannerImg