Sum of Two Numbers: flowchart, algorithm and program

This program reads two integers from the user, adds them and prints the result. It is the simplest example of the input, process, output pattern that almost every program follows.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Print 'Enter first number'.
  3. Step 3: Read a.
  4. Step 4: Print 'Enter second number'.
  5. Step 5: Read b.
  6. Step 6: Set sum = a + b.
  7. Step 7: Print sum.
  8. Step 8: Stop.

Pseudocode

BEGIN
    PRINT 'Enter first number'
    READ a
    PRINT 'Enter second number'
    READ b
    sum ← a + b
    PRINT sum
END

Program in C, Python and Java

Sum of Two Numbers program in C

#include <stdio.h>

int main() {
    int a, b, sum;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    sum = a + b;
    printf("Sum = %d\n", sum);
    return 0;
}

Sum of Two Numbers program in Python

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)

Sum of Two Numbers program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a, b, sum;
        System.out.print("Enter first number: ");
        a = sc.nextInt();
        System.out.print("Enter second number: ");
        b = sc.nextInt();
        sum = a + b;
        System.out.println("Sum = " + sum);
    }
}

Sample input: a = 4, b = 6
Output: Enter first number: Enter second number: Sum = 10

How it works

  • Three int variables are declared: a and b hold the inputs, and sum holds the result.
  • printf shows a prompt, and scanf("%d", &a) reads the first integer into a. The & gives scanf the address of a so it can store the value there.
  • The same prompt and scanf steps are repeated to read the second integer into b.
  • The line sum = a + b; adds the two values and stores the answer in sum.
  • printf("Sum = %d\n", sum) prints the result. With a = 4 and b = 6, sum becomes 10 and the program prints Sum = 10.

Dry run (trace table)

StepLineBoxabsumOutput
13Start
25Print 'Enter first number'Enter first number:
36Read a4
47Print 'Enter second number'4Enter second number:
58Read b46
69sum = a + b4610
710Print sum4610Sum = 10
811Stop4610

Time and space complexity

Time: O(1) · Space: O(1). The program does a fixed number of steps and uses only three integer variables.

Common mistakes

  • Forgetting the & before a or b in scanf. Without it, scanf gets the value of the variable instead of its address, and the program may crash or give garbage.
  • Using the wrong format specifier, such as %f for an int. The format in scanf and printf must match the variable type, which is %d for int.
  • Ignoring overflow. If a and b are very large, a + b can exceed the int range, so use long long for big numbers.

Viva questions

Q1. What does this program do?

It reads two integers from the user with scanf, adds them in line 9 and prints the result with printf.

Q2. Why do you write &a and &b in scanf but just sum in printf?

scanf needs the address of the variable so it can store the input there, so we use &. printf only needs the value to display, so we pass the variable itself.

Q3. What does %d mean in scanf and printf?

%d is the format specifier for a signed decimal int. In scanf it says to read an integer, and in printf it says to print an integer.

Q4. Do you really need the variable sum? Could you do it without it?

No, it is not required. You could write printf("Sum = %d\n", a + b); directly. The sum variable just makes the code clearer and lets you reuse the result.

Q5. What happens if the user enters a negative number or zero?

It works correctly, because int holds negative values and zero. For example -5 and 3 give Sum = -2, and 0 and 7 give Sum = 7.

Q6. What happens if the user types a letter like 'x' instead of a number?

scanf fails to read an integer and leaves the variable unchanged. Since a and b are uninitialised, the result is garbage. You can check scanf's return value, which is the number of items successfully read, or initialise the variables to 0.

Q7. What happens if the sum is too large for an int, for example 2000000000 + 2000000000?

It overflows. Signed integer overflow is undefined behaviour in C, and in practice you usually get a wrong negative value. Using long long for the variables and %lld would fix it.

Q8. What is the time complexity of this program?

O(1), constant time. There are no loops or recursion, and it does a fixed number of steps whatever the input values are.

Q9. What would you change to add two decimal numbers like 2.5 and 3.7?

Declare the variables as float or double, and use %f for float or %lf for double in scanf. In printf, use %f for either one.

Frequently asked questions

Why do we use & in scanf but not in printf?

scanf needs the address of the variable so it can write the typed value into it. printf only reads the value to display, so it does not need the address.

Can I add two numbers without a third variable?

Yes, you can write printf("Sum = %d", a + b); directly. Using a sum variable just makes the code clearer and lets you reuse the result.

How do I add decimal numbers instead of integers?

Declare a, b and sum as float or double and use %f (for float) or %lf (for double) in scanf. In printf, %f works for both.

More programs

← All programs

Try AI Flowchart free →