easy
0 views
Print 2N and 4N
Given an integer N, print the values of 2N and 4N.
Understand the Problem
Problem Statement
The program must accept an integer N as the input. The program must print 2N and 4N as the output.
For example, if N = 8, the output should be 16 and 32.
Constraints
- Input Range: N can be any valid 32-bit signed integer (-2,147,483,648 ≤ N ≤ 2,147,483,647)
- Output: Two integers printed on separate lines
- Time Complexity: O(1) - constant time operations
- Space Complexity: O(1) - no additional space required
- Edge Cases: Handle negative numbers, zero, and maximum/minimum integer values
Examples
Example 1
Input
8Output
16
32Explanation
When N = 8, 2N = 8 × 2 = 16 and 4N = 8 × 4 = 32. The output shows these two values on separate lines.
Example 2
Input
5Output
10
20Explanation
When N = 5, 2N = 5 × 2 = 10 and 4N = 5 × 4 = 20. The output shows these two values on separate lines.
Example 3
Input
-3Output
-6
-12Explanation
The algorithm works with negative numbers too. When N = -3, 2N = -3 × 2 = -6 and 4N = -3 × 4 = -12.
Solution
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
// Calculate and print 2N
printf("%d\n", N * 2);
// Calculate and print 4N
printf("%d\n", N * 4);
return 0;
}Time:O(1) - Constant time operations (multiplication and print)</li>
Space:O(1) - Only uses one integer variable N
Approach:
This C solution:
- Declares an integer variable N
- Uses scanf() to read the input value
- Calculates 2N by multiplying N by 2 and prints it
- Calculates 4N by multiplying N by 4 and prints it
- Each result is printed on a separate line using \n (newline)
Visual Explanation
Loading diagram...