- 1

Infinite sequence

Write the first numbers of the infinite sequence 1 2 3 4 5 4 3 2 1 2 3 4 5 4... Examples: Input: 1 Output: 1 Input: 5 Output: 1 2 3 4 5 Input: 7 Output: 1 2 3 4 5 4 3 Please I need help with this

24th Jun 2021, 3:10 AM
TheScan
TheScan - avatar
2 Antworten
0
TheScan You can use loops to do this. #include <iostream> using namespace std; int main() { int j=1,i=1,k=0; int n; cin>>n; while(k<n){ if(i<=5){ cout<<i<<' '; k++; i++; } else {cout<<5-j<<' '; j++; k++; if(j==4){ i=1; j=1; } } } return 0; }
24th Jun 2021, 3:33 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
0
Thank you so much :)
24th Jun 2021, 3:39 AM
TheScan
TheScan - avatar