This is a Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop.
Enter any integer as an input. After that we use operations like modulus and division to reverse a number and find sum of its digits along with the help of do-while loop.
Here is the source code of the Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Use_Do_While
{
public static void main(String[] args)
{
int n, a, m = 0, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
do
{
a = n % 10;
m = m * 10 + a;
sum = sum + a;
n = n / 10;
}
while( n > 0);
System.out.println("Reverse:"+m);
System.out.println("Sum of digits:"+sum);
}
}
Output:
$ javac Use_Do_While.java $ java Use_Do_While Enter any number:456 Reverse:654 Sum of digits:15
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Practice Programming MCQs
- Check Java Books
- Apply for Computer Science Internship
- Practice BCA MCQs