Add Two Integers
Problem
You are given two integers num1 and num2. Return their sum, num1 + num2.
num1 = 12, num2 = 517def sum(num1, num2):
total = num1 + num2
return total
function sum(num1, num2) {
const total = num1 + num2;
return total;
}
class Solution {
public int sum(int num1, int num2) {
int total = num1 + num2;
return total;
}
}
int sum(int num1, int num2) {
int total = num1 + num2;
return total;
}
Explanation
This is about as direct as a problem gets: we are handed two integers and asked for their sum. There is no clever trick, no edge case to chase down — the + operator already does exactly what we need.
We compute num1 + num2 and hand the result straight back. Storing it in a local variable called total is purely for readability; you could just as well return num1 + num2 in a single line.
The only thing worth a moment's thought is the value range. The inputs sit comfortably inside the signed 32-bit range, and their sum never overflows it, so a plain int is enough in Java and C++ (in Python and JavaScript integers grow as needed anyway).
Example: num1 = 12, num2 = 5. Adding them gives 12 + 5 = 17, which is the answer we return.