Thread: loop

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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,815
    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, Buttercup, 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
    39,659
    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.

  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
    39,659
    > 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.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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, 03: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, 01:46 AM