Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Add Two Complex Numbers
Next article icon

C Program to Add Two Integers

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given two integers, the task is to add these integer numbers and return their sum.

Examples

Input: a = 5, b = 3
Output: 8
Explanation: The sum of 5 and 3 is 8.

Input: a = -2, b = 7
Output: 5
Explanation: The sum of -2 and 7 is 5.

Add Two Numbers in C

In C, we can add two numbers easily using addition operator (+). This operator works by taking two operands and returns their sum as the result.

Program to Add Two Numbers using + Operator

C
// C program to add two numbers #include <stdio.h>  int main() {     int a, b, sum = 0;      	// Read two numbers from the user     printf("Enter two integers: ");     scanf("%d %d", &a, &b);      // Calculate the addition of a and b     // using '+' operator     sum = a + b;      printf("Sum: %d", sum);      return 0; } 


Output

Enter two integers: 5 3
Sum: 8

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

Explanation: In the above program, the user is first asked to enter two numbers. The input is taken using the scanf() function and stored in the variables a and b. Then, the variables a and b are then added using the arithmetic operator + (addition operator), and the result is stored in the variable sum.

Table of Content

  • Other Different Methods of Adding Two Numbers in C
    • Addition Using Increment Operator (++)
    • Addition Using Bitwise Operators in C

Other Different Methods of Adding Two Numbers in C

There are few more methods that we can use to add two integer numbers in C:

Addition Using Increment Operator (++)

We can also add two numbers in C++ using the increment operator by repeatedly increasing the first value based on the value of the second number.

Program to Add Two Numbers using Bitwise Operators

C
// C Program to add two numbers using increment operator #include <iostream> using namespace std;  int addUsingIncrement(int a, int b) {           return a; }  int main() {     int a, b;      // Input two integers     cout << "Enter two integers: ";     cin >> a >> b;      	// If b is positive, increment a b times     for (int i = 0; i < b; i++) {         a++;     }          // If b is negative, decrement a |b| times     for (int i = 0; i > b; i--) {         a--;     }      // Output the sum     cout << "Sum = " << a << endl;      return 0; } 


Output

Enter two integers: 5 3
Sum = 8

Addition Using Bitwise Operators

We can also add two numbers using bitwise operations without using the + operator. This involves using the XOR and AND operation along with bit shifting.

Step-by-Step Approach

  • Read two integers from the user.
  • Use bitwise XOR (^) operator to add the bits of the numbers without carry.
  • Use bitwise AND (&) operator to calculate the carry.
  • Left shift (<<) the carry by one place to add it.
  • Repeat the process until there is no carry.

Program to Add Two Numbers using Bitwise Operators

C
// C program to add two numbers using Bitwise operators #include <stdio.h>  int main() {     int a, b, sum, carry;      // Take two integer numbers from user     printf("Enter two integers: ");     scanf("%d %d", &a, &b);      while (b) {          // Carry is AND of a and b         carry = a & b;          // Sum without carry is XOR of a and b         a = a ^ b;          // Carry is shifted by one so that it can be         // added in the next iteration         b = carry << 1;     }        printf("Sum = %d\n", a);     return 0; } 


Output

Enter two integers: 5 3
Sum = 8

Conclusion

We discussed the problem of adding two integers and provided three methods to solve it: using direct addition, using bitwise operations, and using recursion. While direct addition is the simplest and most efficient method for most cases, bitwise operations and recursion provide interesting alternatives that can be useful in specific scenarios.



Next Article
C Program to Add Two Complex Numbers
author
harsh.agarwal0
Improve
Article Tags :
  • C Language
  • C Programs
  • C Basic Programs

Similar Reads

  • 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 = (
    3 min read
  • C Program To Merge Two Arrays
    Merging two arrays means combining/concatenating the elements of both arrays into a single array. Example Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]Output: res = [1, 3, 5, 2, 4, 6]Explanation: The elements from both arrays are merged into a single array. Input: arr1 = [10, 40, 30], arr2 = [15, 25, 5]
    3 min read
  • C Program to Swap Two Numbers
    Swapping two numbers means exchanging their values. In this article, we will learn how to swap values of two numbers in a C program. The easiest method to swap two numbers is to use a temporary variable. First, we assign the value of first variable to temporary variable, then assign the value of sec
    2 min read
  • C Program for Two Pointers Technique
    Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Let’s see the naive so
    3 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 + 11Output: 110[GFGTABS] C // C Program to Add 2 Binary Strings // and
    8 min read
  • Program that allows integer input only
    Given an input value N, the task is to allow taking only integer input from the user. Now, if the user enters any input other than an integer, that is, a character or symbol, it will not be accepted by the program. Below is the C program to implement this approach: [GFGTABS] C // C program for the a
    3 min read
  • C Program to Multiply two Floating Point Numbers
    Floating point numbers are a way to represent real numbers with both whole parts and fractional parts. In this article, we will learn how to write a C program to find the product of two floating-point numbers. The below image shows an example of floating point multiplication in C: C Program To Multi
    1 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->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1-
    4 min read
  • C program to set K-th bit of a number N
    Given a number N and an integer K, the task is to set the Kth bit of the number N, i.e., if the Kth bit is 0, then set it to 1 and if it is 1 then leave it unchanged. Examples: Input: N = 5, K = 2Output: 7Explanation: 5 is represented as 101 in binary and has its second bit 0, so setting it will res
    3 min read
  • How to Write a Command Line Program in C?
    In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences