Thread: program to find reverse number

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    program to find reverse number

    I wrote below program to find reverse number but it's not working. what's the problem ?
    Code:
    #include<stdio.h>
    int main (void)
    {
     unsigned int number, reverse; 
        printf("Enter number to reverse \n");
        scanf("%d",number);  
      
     while (number != 0)
     {
      reverse = reverse * 10;
      reverse = reverse + number % 10;
      number   = number / 10;
     }
     printf("Reverse number is = %d \n", reverse);
      
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try initialising your variables before using them.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    Try initialising your variables before using them.
    unsigned int number = 0;
    unsigned int reverse = 0;

    If I put these two lines in program still I am not getting output

    result

    Enter number to reverse
    125

    error hello.c file stopped working

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What about your scanf line?

    Do you recall anything about having to use an &
    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
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    What about your scanf line?

    Do you recall anything about having to use an &
    Thank you for point out my mistake. I forgot to put & in program

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should look to using GCC as your compiler. One of the many useful features is warning you about printf and scanf problems such as that.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Replies: 15
    Last Post: 12-01-2012, 11:15 AM
  3. 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
  4. 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