
Problem
Solution
Submissions
Swap Two Numbers
Certification: Basic Level
Accuracy: 60.58%
Submissions: 104
Points: 5
Write a C++ program that swaps two numbers using pointers.
Example 1
- Input: a = 5, b = 10
- Output: a = 10, b = 5
- Explanation:
- Step 1: Declare pointers for variables a and b.
- Step 2: Use pointers to access and swap the values.
- Step 3: After swapping, a = 10 and b = 5.
Example 2
- Input: a = -3, b = 7
- Output: a = 7, b = -3
- Explanation:
- Step 1: Declare pointers for variables a and b.
- Step 2: Use pointers to access and swap the values.
- Step 3: After swapping, a = 7 and b = -3.
Constraints
- The values of a and b are integers
- Use pointers to swap the values
- Time Complexity: O(1)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use pointers to access the memory locations of the variables.
- Swap the values stored at the memory locations using a temporary variable.
- Ensure that the original variables are modified after the swap.