easy
0 views
Add Three Numbers
Accept three integers and print their sum.
Understand the Problem
Problem Statement
Accept three numbers and print their sum.
Constraints
- The input consists of exactly three integers separated by spaces
- Each integer can be within the standard 32-bit signed integer range (-2,147,483,648 to 2,147,483,647)
- The output should be a single integer representing the sum
Examples
Example 1
Input
5 2 4Output
11Explanation
5 + 2 + 4 = 11. The three numbers are read from input, added together, and the sum 11 is printed.
Example 2
Input
25 15 10Output
50Explanation
25 + 15 + 10 = 50. The three numbers are read from input, added together, and the sum 50 is printed.
Solution
#include <stdio.h>
int main() {
int num1, num2, num3;
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", num1 + num2 + num3);
return 0;
}Time:O(1) - Constant time operations (reading 3 integers and 1 addition)
Space:O(1) - Only uses 3 integer variables regardless of input size
Approach:
Explanation:
#include <stdio.h>- Includes standard input/output library for scanf and printfint main()- Main function entry pointint num1, num2, num3;- Declares three integer variables to store inputscanf("%d %d %d", &num1, &num2, &num3);- Reads three integers from standard inputprintf("%d", num1 + num2 + num3);- Calculates and prints the sumreturn 0;- Returns 0 to indicate successful execution
Visual Explanation
Loading diagram...