Saturday 19 October 2013

Write a Program to Reverse a 5 Digit Number (No Modulus)

I once posted a program to reverse a 5 digit number (Write A Program To Reverse The Number). It used the modulus operator to do the reversal. I mentioned that I had first written the program to do the reversal without using the modulus operator.

Well here's the program. This does not use the modulus operator. But Firrrssstttt...
The Logic:

Again, a 5 digit number is in the 10,000th unit (not hundred thousand and above or nine thousand nine hundred and ninty nine or below). So 

Divide any 5 digit number by 10,000 and you'll get the FIRST DIGIT<POINT>THE REMAINING FOUR NUMBERS. If we typecast this in an int, it is saved without the decimal places.

Eg: Let us assume the user enters 12345 or 23456
1) 12345/10000= 1.2345 (Send the 1.2345 as an int and not a float, it is seen as 1)
2) 23456/10000= 2.3456 (Send the 2.3456 as an int and not a float, and it is seen as a 2)
We do this by typecasting the result:
float result; //result is now a float.
result=(int)12345/10000; //result now holds 1. The .2345 is ignored
result=(int)23456/10000; //result now holds 2. The .3456 is ignored.
Without typecasting (the 'int' in the braces after the 'equals' sign) the results into an int, 'result' would have held 1.2345 and 2.3456 respectively.
So, we now have the first digit.

If we Divide the original the original input by 1000, we get FIRST 2 DIGITS<POINT>REMAINING THREE NUMBERS. As before, typecast this into an int and the decimal places are ignored.
1) 12345/1000=12.345 (typecasted into 12)
2) 23456/1000=23.456(typecasted into 23)
Subtract the typecasted result by the first digit multiplied by 10, and you have the second digit.
1) 12-(1*10) = 12-10 = 2 (Second digit)
2) 23-(2*10) = 23-20 = 3 (Second Digit)

If we Divide the original the original input by 100, we get FIRST 3 DIGITS<POINT>REMAINING TWO NUMBERS. Typecast this into an int and the decimal places are ignored.
1)12345/100=123.45 (typecasted into 123)
2)23456/100=234.56 (typecasted into 234)
Subtract the typecasted result by the first digit multiplied by 100, and you have the SECOND AND THIRD digits.
1)123-(1*100) = 123-100 = 23 (Second and third digits)
2)123-(2*100) = 234-200 = 34 (Second and third digits)
Subtract the result by the second digit multiplied by 10, and you have the THIRD DIGIT.

Just follow this through. With every new digit, the subtracting digit gains a new zero. I won't explain every digit. You'll get the flow from the program.
It's a lot easier to figure out the logic than try to memorize it. Once you get it, it's pretty easy. I wish my math teacher could see me doing these stuff.....

As before, the program displays the result as a series of 5 numbers and then as a single number. Enter 10000 and look at its reversal to see the difference.

/*Reverse A 5 Digit Number!*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


int main()
{

    int state,rev_num,z;
    float a,b,c,d,e,x,y;

    puts("Enter a five digit number (Cannot start with zero): ");
    scanf("%f",&x);

    if(x>9999 && x<100000) //Check that the entered number is a valid input
    {
        y=x; //make sure the original input is saved in x.

        a=(int)y/10000; //(First Digit)
        printf("%.0f\n",a);

        b=(int)y/1000-(a*10); //Second
        printf("%.0f\n",b);

        c=(int)y/100-(a*100);
        c=(int)c-(b*10);     //Third
        printf("%.0f\n",c);

        d=(int)y/10-(a*1000);
        d=(int)d-(b*100);
        d=(int)d-(c*10); //Fourth
        printf("%.0f\n",d);

        e=(int)y-(a*10000);
        e=(int)e-(b*1000);
        e=(int)e-(c*100);
        e=(int)e-(d*10);//Fifth
        printf("%.0f\n",e);

        printf("%.0f%.0f%.0f%.0f%.0f\n",e,d,c,b,a); //print as a series of digits.
        rev_num=(e*10000)+(d*1000)+(c*100)+(b*10)+a; //find the reverse number as a single integer
        z=(a*10000)+(b*1000)+(c*100)+(d*10)+e; //Find the original input for the heck of it although it is saved in x & y
        printf("Reverse of %i=%i",z,rev_num);

        return 0;
    }
    else //if user hasn't entered a valid input, tell 'em, exit.
    {
        printf("Invalid input!\n");
        getch();
        exit(0);
    }
}

No comments:

Post a Comment