Adding two numbers is one of the first things you’ll tackle in C programming. Let’s check out how to add two integers in C. Picture this: you've got 5 and 3. The program gives you their sum, which is 8.
This article shows you how to whip up a simple C program to add two integers, and it'll dive into each step involved.
Prerequisites for the C Program to Add Two Integers
Before we start, make sure you're familiar with these:
- Basic understanding of C programming syntax
- The int data type to store integer values
- Standard input and output functions like
scanfandprintf - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Add Two Integers
Here's the complete source code for adding two integers in C:
#include <stdio.h>
int main() {
int a, b, sum;
// Input two integers from the user
printf("Enter the first integer: ");
scanf("%d", &a);
printf("Enter the second integer: ");
scanf("%d", &b);
// Calculate the sum
sum = a + b;
// Print the sum
printf("Sum = %d\n", sum);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Add Two Integers Program
When you run this program, it will ask for two integers and display their sum. Here's a sample execution:
Enter the first integer: 5
Enter the second integer: 3
Sum = 8
Algorithm to Add Two Integers in C
Adding two integers involves reading them from the user, calculating their sum, and then displaying the result. Here is the step-by-step logic:
- Read the first integer
afrom the user. - Read the second integer
bfrom the user. - Calculate the sum of
aandb. - Display the result of the addition.
flowchart TD A([Start]) --> B[/Read first integer a/] B --> C[/Read second integer b/] C --> D[sum = a + b] D --> E[/Print sum/] E --> F([End])
How the C Program to Add Two Integers Works
Let's break down the code line by line to see how it works.
- The line
#include <stdio.h>includes the Standard Input Output library needed for functions likeprintfandscanf. - The
int main()function is where the program execution starts. - We declare integer variables
a,b, andsumto store the user inputs and the result. printfis used to prompt the user to enter the first integer.scanf("%d", &a)reads the integer input from the user and stores it ina.- The process is repeated for the second integer
b. - The statement
sum = a + bperforms the addition ofaandb. printf("Sum = %d\n", sum)prints the result on the screen.- The program ends with
return 0indicating successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the 'sum = a + b' line do in this program?
Time and Space Complexity of the C Program to Add Two Integers
This program efficiently handles the addition of two numbers with minimal resources.
Time Complexity: O(1). The program performs a constant amount of work independent of the input, as it only does a single addition operation.
Space Complexity: O(1). It uses only three integer variables: a, b, and sum.
Common mistake: Not including the <stdio.h> header can cause errors when using printf and scanf.
Key takeaway: This program's a simple intro to arithmetic in C, showing how user input and basic calculations work together.
FAQs About C Program to Add Two Integers
Here are some common questions beginners have about this program:
int data type's used to hold integer values here.scanf gets the input, while cin is what you see in C++. Since this program's in C, scanf is the way to go.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Multiply Two Floating-Point Numbers
- C Program to Find Factorial of a Number
- C Program to Display Fibonacci Sequence with Example
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
Conclusion
When you’re dealing with user input, think about input validation — it'll make sure the entries are integers. It’ll help avoid unexpected behavior. Try tweaking this code to work with more numbers or even floating-point values. These skills will come in handy as you dive into more complex stuff.
Get more practice with C programming through our C programming quiz and try out new programs in the online compiler.