Ques: Write a program where user gives two inputs digit , the program reverse the inputs and add them and again reverse there sum.
For Exp:
Input:
Enter first number: 56
Enter second number:89
Output:
reverse of first number: 65
reverse of second number:98
sum of two reverse no is: 163
reverse of sum: 361
Source Code:
Note: we can also optimize the code by adding a variable reverseSum and store the value of reverse of sum (Line-29) in it.
For Exp:
Input:
Enter first number: 56
Enter second number:89
Output:
reverse of first number: 65
reverse of second number:98
sum of two reverse no is: 163
reverse of sum: 361
Source Code:
- using System;
- public class SumOfDigit
- {
- public static void Main()
- {
- int number1, number2;
- int sum=0;
-
- Console.Write("Enter the first Number: ");
- number1=Convert.ToInt32( Console.ReadLine() );
-
- Console.WriteLine("Reverse of first Number: {0}\n", ReverseNumber( number1 ));
- Console.Write("Enter the second Number: ");
- number2=Convert.ToInt32( Console.ReadLine() );
-
- Console.WriteLine("Reverse of second Number: {0}\n", ReverseNumber( number2 ));
-
- sum = ReverseNumber( number1 )+ReverseNumber( number2 );
-
- Console.WriteLine("\n\nSum of Two reverse number is: \t{0}", sum);
-
- sum = ReverseNumber( (ReverseNumber( number1 )) + (ReverseNumber( number2 )) );
- Console.WriteLine("Reverse of sum of Two number is: \t{0}",sum);
- }
- public static int ReverseNumber( int digit )
- {
- int x,reverse=0;
-
- while(digit !=0)
- {
- x=digit%10;
- reverse=reverse*10+x;
- digit=digit/10;
- }
- return (reverse);
- }
- }
Fig: Screenshot of Output |
Note: we can also optimize the code by adding a variable reverseSum and store the value of reverse of sum (Line-29) in it.