Find the sum of two numbers

Find Sum of Two Numbers

Problem Statement

Write a program to find the sum of two numbers entered by the user and display the result.

This is one of the most basic and important problems for beginners, as it introduces:

  • Taking user input

  • Performing arithmetic operations

  • Displaying output

Explanation

In this problem:

  1. We take two numbers as input.

  2. Add both numbers using the + operator.

  3. Store the result in a variable.

  4. Print the final sum.

For example, if the input numbers are 10 and 20, their sum will be 30.

Approach

  • Read two integers from the user

  • Add them using the addition operator

  • Display the result

This logic is the same in C, C++, Java, and Python, only the syntax changes.

Find the sum of two numbers

#include <stdio.h>

int main() {
    int a, b, sum;
    scanf("%d %d", &a, &b);

    sum = a + b;
    printf("%d", sum);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;

    cout << a + b;
    return 0;
}

import java.util.Scanner;

public class SumOfTwoNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        System.out.print(a + b);
    }
}

a, b = map(int, input().split())
print(a + b)

Input


10 20

Output


30

Time and Space Complexity

  • Time Complexity: O(1)

  • Space Complexity: O(1)

💡 Tips for Beginners

  • Always store the result in a variable for clarity.

  • Make sure input format matches the code.

  • Practice this program using different values.

  • Try extending it to find the sum of three numbers.

Conclusion :

The Find Sum of Two Numbers program is the first step toward learning programming logic. It helps beginners understand input, variables, operators, and output. Mastering this simple problem makes it easier to learn more complex programs in the future.

Keep practicing with EasyHaiCode 🚀
Happy Coding 😊

0

Subtotal