confusINg code in C++

Discussion in 'C++' started by Shentella, Feb 7, 2010.

  1. Shentella

    Shentella New Member

    Joined:
    Feb 7, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Hint: abs.
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice