Thursday 13 June 2013

Write A Program To Reverse The Number

Another exercise in Let Us C (Chapter 1, Exercise I-h) asks this:

If a five digit number is input through the keyboard, write a program to reverse the number.

I saw this when I first went through the chapter, but skipped it since I didn't think I could do it (I am very bad at Math see?).

Anyway, I went back to it, and here's what I came up with. I wrote a program that does this reversal without using the modulus operator but I wanted to do it using modulus (%), so that it would also solve exercise I-g (If a five digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator '%')).

The one that does not use modulus is HERE

What the modulus operator does is it returns the remainder when a number is divided by another.

Example: x=8%3 (x is equal to the REMAINDER of eight divided by 3), the remainder is two, so now x=2.

or: x=5%2 (x is equal to the REMAINDER of five divided by two), the remainder is one, so now x=1

The logic I used is this:

A five digit number is in the 10,000th unit. So if we divide the 5 digit number by 10,000 (the smallest 5 digit number), we get all but the 10,000th unit as a remainder. As an example let us say the input is 12345 or 23456.

Example: 1)      12345/10000 (Remainder) = 2345
               2)      23456/10000 (Remainder) =3456

And we subtract it from the original input giving us the 10,000th unit of the first digit:

Example: 1)     12345-2345=10,000
               2)     23456-3456=20,000

Divide the 10,000th unit by 10,000 and we get the first digit:

Example: 1)     10000/10000=1 First digit of first input
               2)     20000/10000=2 First digit of second input.

We just continue doing this eliminating the 10000th, 1000th, 100th and 10th units. Eventually we know all the digits.

The program first displays the reverse number as a string of single variables:
 printf("Reverse of %i = %i%i%i%i%i\n",input,fifth,fourth,third,second,first);

Then as a single variable
printf("Reverse number again= %i",rev);
Try entering 10000 as the input and look at the output to see the difference between them; then choose which one is best, I prefer to leave them both in.


Here is the program:

#include <stdio.h>

main()
{
    int input,rev,temp,remain_digits,first,second,third,fourth,fifth;

    printf("Enter a five digit number:");
    scanf(" %i",&input);
    if(input>9999 && input<=99999) //Make sure it is a 5 digit number
    {
        temp=input;
        remain_digits=temp%10000;
        printf("remaining %i\n",remain_digits);
        first=(temp-remain_digits)/10000;
        printf("first %i\n",first); //first digit

        temp=remain_digits;
        remain_digits=temp%1000;
        printf("remaining %i\n",remain_digits);
        second=(temp-remain_digits)/1000;
        printf("second %i\n",second); //second digit

        temp=remain_digits;
        remain_digits=temp%100;
        printf("remaining %i\n",remain_digits);
        third=(temp-remain_digits)/100;
        printf("third %i\n",third);

        temp=remain_digits;
        remain_digits=temp%10;
        printf("remaining %i\n",remain_digits);
        fourth=(temp-remain_digits)/10;
        printf("fourth %i\n",fourth);

        fifth=remain_digits;

        printf("fifth %i\n",fifth);

        printf("Reverse of %i = %i%i%i%i%i\n",input,fifth,fourth,third,second,first);

        /*If you want the reverse number as a single variable*/
        rev=(fifth*10000)+(fourth*1000)+(third*100)+(second*10)+first;
        printf("Reverse number again= %i",rev);

    }

    else
    {
        printf("Only a 5 digit number please.");
    }

}



IF you get negative or strange values as an output change the line:
int input,rev,temp,remain_digits,first,second,third,fourth,fifth;

To:

unsigned input,rev,temp,remain_digits,first,second,third,fourth,fifth;
and change every occurrence of %i to %u (Pressing Ctrl+R will bring up Search And Replace in Code::Blocks).


Older systems have a limited range for an int, I think its from -32768 to Positive 32767. Mine is at around 2 billion, go figure.

We can also use this to solve the exercise I-g:
If a five digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator '%')
In this case, once we have all the digits, we can simply say:

printf("Sum of the digits= %i",first+second+third+fourth+fifth);

and that will output the sum of the digits.

No comments:

Post a Comment