Tuesday, October 4, 2011

Finding the sum of a number and the number formed by reversing its digits (Java)

//This Java program accepts a number from the
//user and stores it. The program then finds
//the sum of the original number and the
//number formed by reversing its digits.
//For example if 521 is entered, the sum of 521
//and 125 is found out and printed.
import java.util.*;
class sumOfOriginalAndReverseNumber
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int num_orig=sc.nextInt();
    int num_new=0,n=num_orig;
    while(n>0)
    {
        num_new=(num_new*10)+(n%10);
        n=n/10;
    }
    System.out.println("The reversed number is "+num_new);
    int sum=num_orig+num_new;
    System.out.println("The sum of the original and reversed number is "+sum);
}
}
//Author : Mayank Rajoria

No comments:

Post a Comment