Reverse a Number in .Net
Reverse a Number in .Net:-
it is a simple way to reverse a number where we take an input by user to reverse. by the following step:-
1. Take the number which you have to reverse as the input.
2. Obtain its quotient and remainder.
3. Multiply the separate variable with 10 and add the obtained remainder to it.
4. Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
5. Repeat the process until quotient becomes zero.
6. Print the output and exit.
Problem Description:-
Here is source code of the C# program to reverse a number.The C program is successfully compiled and run on a Win7 operating system.The program output is also shown below.
1. using System;
2. public class ReverseExample.
3. {
4. public static void Main(string[] args)
5. {
6. int n, reverse=0, rem;
7. Console.Write("Enter a number: ");
8. n= int.Parse(Console.ReadLine());
9. while(n > 0)
10. {
11. rem = n%10;
12. reverse = reverse * 10 + rem;
13. n=n/10;
13. }
14. Console.Write("Reversed Number: "+reverse);
15. }
16. }
Output :-
Enter a number: 234
Reversed Number: 432
Program Explanation
1. Take the number which you have to reverse as the input and store it in the variable n.
2. Multiply the variable reverse with 10 and add the Obtained rem to it and store the result in the same variable.
3. Divide the n by 10.
4. Now check the condition the n is less the or equal to 0. or not and repeat it again. until it is not equal or less than 0 by the while loop.

Comments
Post a Comment