Showing posts with label Reverse of Number. Show all posts
Showing posts with label Reverse of Number. Show all posts

Tuesday, April 01, 2014

Number Reverser – C Program

Problem Question


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

Explanation of Problem


In this program the user enters a 5 digit number. The program should be able to reverse the number. For example if the user enters 12345, the output should be 54321.

Code


#include <stdio.h>
/*Number Reverser*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 01-04-2014**/
int main()
{
    printf("\n\nEnter a five digit number: ");
    int number, reverseNumber = 0, digit1, digit2, digit3, digit4, digit5;
    scanf("%d", &number);
    printf("\nCalculating reverse of number......");
    digit1 = (number % 100000) / 10000;
    digit2 = (number % 10000) / 1000;
    digit3 = (number % 1000) / 100;
    digit4 = (number % 100) / 10;
    digit5 = (number % 10);
    reverseNumber = (digit5 * 10000) + (digit4 * 1000) + (digit3 * 100) + (digit2 * 10) + digit1;
    printf("\nThe reverse of number is: %d\n", reverseNumber);
    system("pause");
    return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

digit1 = (number % 100000) / 10000;
digit2 = (number % 10000) / 1000;
digit3 = (number % 1000) / 100;
digit4 = (number % 100) / 10;
digit5 = (number % 10);

The above piece of code reflects the part of the code in which we extract all the digits of the 5-digit number. ‘%’ operator (called the modulus operator) finds the remainder of the division of the number on left with the number on right of the operator. What I did here is, first I applied the modulus operator between the number and 100000. On dividing, the remainder found, with 10000, I found the digit at the ten-thousand position of the number. This is possible because I am using the ‘int’ data type, which thus rejects the digits after the decimal. Similarly, the other digits are found. You can try it on paper to justify my statement to yourself.

reverseNumber = (digit5 * 10000) + (digit4 * 1000) + (digit3 * 100) + (digit2 * 10) + digit1; -> In this statement I calculate the final number we required. So I just did the necessary multiplications which are quite self-explanatory.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



Thursday, March 20, 2014

Reverse of a Number – Linux Shell Scripting

Problem Question


Write a script to display the given number in reverse order.

Explanation of Problem


Here we wish to write a Linux Shell Script that would accept one command line parameter (integer) and reverse it. A sample run should yield the output of the following kind:
sh ReverseNumber.sh 123
the output should be 321.

Code



#*ReverseNumber*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 20-03-2014
n=$1
a=""
while [ $n -gt 0 ]
 do
  a=$( echo ${a}$(( $n % 10)) )
  n=$(( $n / 10))
 done
echo $1 in Reverse Order is $a

Explanation of Code


Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.

n=$1 -> -> We save the command line argument into a variable named ‘n’. This variable is required since we are going to use it to segregate the digits by recursively dividing it with 10 and storing the remainder.

a="" -> ‘a’ is our string variable here which is going to hold he answer, the reverse number.

while [ $n -gt 0 ] -> Our main loop of the program which is going to extract the digits one by one from the variable ‘n’ and store it into our variable ‘a’. For this purpose we us the line a=$( echo ${a}$(( $n % 10)) ) where we do the following –
The value of the last digit is extracted by applying modulus function to the number. We use the echo command to display the current value of variable ‘a’ followed by this extracted digit. But since we enclose this echo inside another $, we store the result of the echo to the variable ‘a’ rather than displaying it on the terminal. Thus we build up the string ‘a’ holding the reverse of the number as we traverse through the loop.

n=$(( $n / 10)) -> Since we have used the last digit in the above line, thus we reject it using this line of code so that we are left with the remaining part of the number.

do done -> While loop block delimiters.

echo $1 in Reverse Order is $a -> We this time use the echo command of the linux shell to display the output to the screen. The output, i.e. the reverse number is stored in variable ‘a’ while ‘$1’ denotes the command line argument, i.e. our original number.

Output(s)