Thread: Beginner with C: While Loop Question

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    1

    Beginner with C: While Loop Question

    Hi all,

    I had recently been given an assignment, in which I was asked to write a C program that1. prompts the user to enter two character values;2. prints out the sequence of characters between the two values (inclusive). I solved this with the following code:

    Code:
     #include <stdio.h>
    int main() {
        char A = 0, B = 0;
        printf("enter two values: ");
        scanf("%c%c", &A, &B);
        while(A <= B) printf("%c %c ", A++,);
        return 0;
    }
    Now I have been asked to add a feature in which the two characters are entered is not significant. Therefore,regardless of whether the higher or lower character is the first entered the sequence isstill displayed in ascending order from the lowest to the highest.


    Could anyone give me some tips on how to achieve this?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    297
    Think about your loop condition. You use something to compare two characters. So, maybe compare and find the greater of the two. If A > B, swap them and print.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    6
    Code:
    #include <stdio.h>
    int main() {
        char A = 0, B = 0;
        printf("enter two values: ");
        scanf("%c %c", &A, &B);  
        while(B < A) printf("%c ", B++);
        while(A <=B) printf("%c ", A++);    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    297
    Are you sure that that's what you want? It should be more like:

    Code:
    #include <stdio.h>
    int main() {
        char A = 0, B = 0;
        printf("enter two values: ");
        scanf("%c %c", &A, &B);  
        if (A > B) {
             char temp = A;
             A = B;
             B = temp;
        }
        while(A <= B) printf("%c ", A++);    
        return 0;
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. Beginner question: infinite loop
    By c++urious in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2010, 03:07 AM
  3. Replies: 9
    Last Post: 07-14-2010, 02:04 PM
  4. beginner question about stopping a for-loop.
    By Techboy10 in forum C Programming
    Replies: 3
    Last Post: 12-11-2008, 05:15 PM
  5. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM

Tags for this Thread