![]() |
VOOZH | about |
In C++, when working with user inputs we often need to take input of multiple numbers from a user in a single line. In this article, we will learn how to read multiple numbers from a single line of input in C++.
Example
Input: 1 7 0 4 6 8 Output: Entered Number: 1, 7, 0, 4, 6, 8
To read multiple numbers from a user in one line, we can use the std::cin object as it by default takes the input separated by a whitespace as a separate input. So, if the numbers are separated by a whitespace, we can take them as input in multiple number variables.
The below example demonstrates the use of cin to read the whole line and take the input as separate variables.
Output
Enter multiple numbers separated by spaces: 1 2 3 4 5
Processed numbers: 1 2 3 4 5Time Complexity: O(n)
Space Complexity: O(n)