Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   confusINg code in C++ (http://www.go4expert.com/forums/confusing-code-cpp-t20898/)

Shentella 7Feb2010 13:29

confusINg code in C++
 
Hey guys ,I'm a beginner of C++,

I'm stuck on a perticular problem in which a user is asked to input a number less than 10.
For instance 8.

then the output will be like 1 2 3 4 5 6 7 8 6 5 4 3 2 1 get it? it will count till the input number and then count back to one.


The main issue about this problem is that everything has to be done in a If/esingle for-loop.lf/else statements are not allowed and using any extra variable outside the loop is also not allowed.

xpi0t0s 9Feb2010 13:13

Re: confusINg code in C++
 
Hint: abs.

virxen 11Feb2010 02:56

Re: confusINg code in C++
 
Code:

#include <iostream>

using namespace std;

int main(){
    int i,number=11;
    while (number>=10){
        cout<<"enter a number <10:";
        cin>>number;
    }
    for (i=-number+1;i<=number-1;i++){
        cout<<(number-abs(i))<<" ";
    }
    return 0;
}



All times are GMT +5.5. The time now is 01:55.