loop

This is a discussion on loop within the C Programming forums, part of the General Programming Boards category; is it possible to output like this using only one loop? if yes, how? target output Code: ABCDE EDCBA here ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    loop

    is it possible to output like this using only one loop? if yes, how?

    target output
    Code:
    ABCDE
    EDCBA
    here is my code but using one loop im not getting my target output
    Code:
    #include <stdio.h>
    
    main()
    {
        int x, y;
        
        for(x='a', y='e'; x<='e'; x++, y--)
        {
            printf("%c\n%c", x, y);
        }
        getch();
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Because of how standard streams work, you will have to print one line at a time. There are ways to use the same loop to print both of the lines, but you will have to print one line at a time.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,660
    Yes it is possible. Various ways. One is to have some logic in the loop so, when it gets to the end of the first line, it changes step direction and goes backward rather than forward.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    okay, thanks you =)

    and to Salem, yep, been posting to different programming forums so, making sure my questions will be answered.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > and to Salem, yep, been posting to different programming forums so, making sure my questions will be answered.
    Good for you - not so good for someone else.
    How To Ask Questions The Smart Way

    Our time is a finite resource, so with you filling your cup twice, someone else has to go away empty handed.
    Trust me, you don't want to get labelled a cross-poster, you'll just end up being ignored everywhere (we really don't like finding our answers already written by someone else after the event).

    Pick ONE forum you like (for whatever your topic is) and stick with it. If you get no luck after say 48h, then move on to somewhere else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,342
    You could use an array for the output:

    Code:
    #include <stdio.h>
    
    int main()
    {
    char array[64];
    int n = 26;
    int i;
    
        for(i = 0; i < n; i++){
            array[    i] = 'A' + (char) i;
            array[i+n+1] = 'A' + (char) (n-i-1);
        }
    
        array[    n] = '\n';
        array[2*n+1] = '\n';
        array[2*n+2] = 0;
        printf("%s", array);
    
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 04:40 PM
  4. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21