Thread: Can't get my program to repeat until 0 is entered

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    Question Can't get my program to repeat until 0 is entered

    Basically what I was asked to do is enter a number and then create a program to reverse said number and the program continues to do that until the user enters the digit 0. While my program does reverse any entered number the user enters, it, however, doesn't ask me again for another number to reverse. The program just ends.

    This is what I have so far:


    Code:
    #include <stdio.h>
    
    
    main(){
    
    
        int n, reverse=0;
    
    
        printf("Enter a number to reverse (enter 0 to end): \n");
        scanf("%d", &n);
    
    
        while (n !=0)
        {
    
    
           reverse=reverse*10;
           reverse=reverse+n%10;
           n=n/10;
    
    
        }
    
    
        printf("The reversed digit is: %d\n", reverse);
    
    
    return 0;
    }
    What can I do to

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,684
    Add another loop the contains the first loop and the scanf.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    What would I need to put for the conditions to do so? (sorry I'm still pretty new to all of this)

  4. #4
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Code:
    int main(array<System::String ^> ^args)
    {
    	int n, reverse=0;
    
    
        printf("Enter a number to reverse (enter 0 to end): \n");
        scanf("%d", &n);
    
    	while(n!=0) {
    
    		while(n != 0) {
    		if( n == 0)
    			break;
    
    
    		reverse=reverse*10;
    		reverse=reverse+n%10;
    		n=n/10;
    		}
    
    	printf("The reversed digit is: %d\n", reverse);
    
    	n = 0;
    	reverse = 0;
    
    	printf("Enter a number to reverse (enter 0 to end): \n");
        scanf("%d", &n);
    
    	}
    
    
        return 0;
    }
    Add a while loop within a while loop, and if n == 0, break out of the loop. And set n and reverse to 0 after the second loop.
    Last edited by Terrance; 03-18-2017 at 08:29 PM.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    Thank you very much for your help! I understand how to it properly now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program acts up if a decimal is entered. Please help.
    By Vanessa in forum C Programming
    Replies: 2
    Last Post: 09-30-2012, 10:57 AM
  2. How to repeat program in C
    By i6472 in forum C Programming
    Replies: 2
    Last Post: 04-01-2010, 05:59 PM
  3. Check wheather the entered number is prime also repeat it
    By rajesh10071986 in forum C Programming
    Replies: 6
    Last Post: 01-11-2010, 04:34 AM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. repeat number program
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 10:50 PM

Tags for this Thread