Thread: Reverse number program

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    23

    Reverse number program

    If we know logic how do we write code for it

    For an example

    quotient = 987 / 10 --> 98
    remainder = 987 % 10 --> 7 -> first number
    quotient = 98 / 10 --> 9 --> third number
    remainder = 98 % 10 --> 8 --> second number

    first ->second -> third = 7 8 9

    I do not want the code, I want to find out way how I can write my code if I know the logic.

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    306
    If you know your logic, then the only way you can code it is by starting to type, I guess?
    "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
    Feb 2020
    Posts
    23
    Quote Originally Posted by Zeus_ View Post
    If you know your logic, then the only way you can code it is by starting to type, I guess?
    Normally I know How do the Reverse Number. The problem is that how do I implement the program ?

    Code:
    #include<stdio.h>
    
    int main ()
    {
    	int number = 987;
    	int reminder;
    	int reverse = 0;
    	
    	reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
    	
    	number = number / 10; // quotient = 987 / 10 --> 98
    	reminder = number % 10; //remainder = 98 % 10 --> 8 --> second number
    	
    	number = number / 10; // quotient = 987 / 10 --> 98 quotient = 98 / 10 --> 9 --> third number
    	
    	return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Do you know about while loops?

    Now would be a good time to find out.

    As in, while you have some number > 0, you do your % and / thing.
    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
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    you do your % and / thing.
    I am using % and / operator

    Code:
    #include<stdio.h> 
    int main ()
    {
        int number = 987;
        int reminder;
        int reverse = 0;
         
         while ( number > 0 )
         {
            reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
            number = number / 10; // quotient = 987 / 10 --> 98
            reminder = number % 10; //remainder = 98 % 10 --> 8 --> second number
            number = number / 10; // quotient = 987 / 10 --> 98 quotient = 98 / 10 --> 9 --> third number
         }
        return 0;
    }
    What will be the algorithm inside of the while loop
    Last edited by Parth12; 02-20-2020 at 11:47 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Try it with only one % and one / inside the loop.

    Maybe just this to begin with.
    Code:
    while ( number > 0 ) {
      printf("Number=%d\n", number);
      number = number / 10;
    }
    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
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    Try it with only one % and one / inside the loop.

    Maybe just this to begin with.
    Code:
    #include<stdio.h> 
    int main ()
    {
        int number = 987;
        int reminder;
        int reverse = 0;
         
        while ( number > 0 ) {
               reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
    		   printf("Number=%d\n", number); 
               number = number / 10;
    		//   reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
    		   printf("reminder =%d\n", reminder);
    		  
    }
         
        return 0;
    }
    Number=987
    reminder =7
    Number=98
    reminder =8
    Number=9
    reminder =9

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > reminder =7
    > reminder =8
    > reminder =9
    Good, now can you put them all into reverse ?
    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.

  9. #9
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    > reminder =7
    > reminder =8
    > reminder =9
    Good, now can you put them all into reverse ?
    I tried but I do not understand how to do that

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    What about
    reverse = reverse * 10 + reminder
    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.

  11. #11
    Registered User
    Join Date
    Feb 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    What about
    reverse = reverse * 10 + reminder
    I got it

    Code:
    #include<stdio.h>
    
     int main ()
    {
        int number = 987;
        int reminder;
        int reverse = 0;
          
        while ( number > 0 ) {  
         
          reverse = reverse * 10 + number % 10;
          number   = number / 10;
     
         }
          
         printf("Reverse number is = %d \n", reverse);
       
        return 0;
    }
    Reverse number is = 789

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    717
    Quote Originally Posted by Parth12 View Post
    I got it
    One little thing, thou... this:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      int number = -987;
      int reverse = 0;
    
      while ( number > 0 )
      {
        reverse = reverse * 10 + number % 10;
        number   = number / 10;
      }
    
      printf( "Reverse number is = %d \n", reverse );
    }
    Will result in 0!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to find reverse number
    By vead in forum C Programming
    Replies: 5
    Last Post: 12-08-2017, 02:04 AM
  2. C program to find the reverse of a Given number
    By riteshkrjain in forum C Programming
    Replies: 2
    Last Post: 10-02-2017, 11:44 PM
  3. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  4. having trouble with reverse of a number program code
    By cooldude in forum C Programming
    Replies: 3
    Last Post: 09-07-2009, 05:49 AM
  5. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM

Tags for this Thread