Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

How to do this While Statement..

  • 28-10-2009 12:47PM
    #1
    Closed Accounts Posts: 314 ✭✭


    Write a program using a while loop to print out the numbers 1 - 10 in descending order on the same line and each number is separated by a comma (e.g. 1,2,3,4,5,6,7,8,9,10)..

    How do u do this please?




    main()

    {

    int num ;


    while ( )
    {
    printf(" " ) ;
    }

    printf(" " ) ;
    }


Comments

  • Closed Accounts Posts: 190 ✭✭jasonbourme.cs


    x=10
    do While X > 0
    x=x-1
    loop

    something like this ?


  • Closed Accounts Posts: 314 ✭✭Joebits


    Ok how do i get it in descending Order??

    This code works

    main(){

    int i = 1;

    while ( i < 11 )

    {
    printf(" %d,", i);
    i = i + 1;
    }
    getch() ;
    }


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Joebits wrote: »
    Ok how do i get it in descending Order??

    This code works

    main(){

    int i = 1;

    while ( i < 11 )

    {
    printf(" %d,", i);
    i = i + 1;
    }
    getch() ;
    }

    Start with i=10, while (i>0) and i = i - 1

    Alternatively (to ensure there's no trailing comma)

    $arr=array();
    for ($x=0;$x>0;$x--) $arr[]=$x;
    echo implode(",",$arr);


Advertisement