Search code examples
c++sequential

c++ shorter way to display the next 5 numbers by given inputs?


i've just learned cpp and want to know is there a shorter way to display a sequential number.

this is my code

#include<iostream>

using namespace std;

int main(){
    int n;

    cout<<"Input number: ";
    cin>>n;

    cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}

Solution

  • A simple loop should solve your problem:

    int main(){
        int n;
    
        cout << "Input number: ";
        cin >> n;
    
        cout << "The next 5 rows of number are: ";
    
        for (int i = n + 1; i <= n + 5; i++) {
            cout << i << ' ';
        }
    }