Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
Last Updated :
13 Apr, 2023
Problem Statement: The number is supposed to be entered by the user be it any random number lying within the primitive data-type holding the number. First, the number needs to be reversed. Secondary the sum of the number is to be calculated with the constraint to use a do-while loop.
do-while loop: Now the user sometimes does get confused between a while and a do-while loop. While loop checks the condition first and then executes the statement whereas the Do-while loop executes the statement then check the condition. An important point arising out from Do-while is even if the condition fails out still it will be executed once.

Syntax of do-While Loop:
do
{
// statement to be executed
}
while(condition check)
Use of Do-while Loop:
Showing some menu to the user in other words the game is implemented where the goal is to show the user press 1 to do this, 2 to do that, and so on where there is an option press Q to quit this game. So do-while loop wants to show the menu at least once to the user. When the user has taken the action appropriate steps are done. So, while the condition should be if the user presses Q to quit and the menu is there inside the do-while loop.
Approach: Using do-while Loop, find the reverse of a number as well as the sum of its digits. Enter any number as an input and after that use modulus and division, operators to reverse that particular number and find the sum of its digits.
Algorithm:
- Taking a number from the user
- Creating two variables namely reverse_number and sum and initializing both of them to 0
- Reversing a number
- Printing Reversed number
- Printing the final sum of the digits as procured from smaller sums.
Reversal Algorithm as discussed as follows:
- Multiply rev by 10 and add remainder of number that is remainder to reverseNumber. // rem = num%10 ; rev = rev*10 + rem;
- Add rem to the current sum to find the sum of digits and storing them in a variable that will be returned holding the final sum. Final_sum=current_sum where current_sum=current_sum + remainder
- Dividing number by 10 to access digit preceding the current digit of the number as follows. // num=num/10; while(num > 0);
Example 1: Java program to reverse and sum up digits of the number without making functions
Java
// Java program to reverse and and sum up digits of number
// Importing generic Classes/Files
import java.io.*;
// Importing Scanner Class
import java.util.Scanner;
class GFG {
// Main driver method
public static void main(String[] args)
{
// creating variables
int num, rem;
// Creating variables and initializing at same time
int rev = 0, sum = 0;
// Using scanner to take input from user
// Scanner sc = new Scanner(System.in);
// Remove comments from lineNo20
// to take dynamic user input
// Displaying message
System.out.println("Enter the number: 25 ");
// Taking input from user
// num = sc.nextInt();
// Hard coded input
num = 25;
// Do-while loop for iteration over digits of number
do {
// Step1: Modulo with 10 to get last digit
rem = num % 10;
// Step2: Reverse the number
rev = rev * 10 + rem;
// Sum of the digits of number
sum = sum + rem;
// Step3: Dividing number by 10 to lose last
// digit
num = num / 10;
}
// Condition check
// Remember: By this time 1 iteration is over even
// if conditions false
while (num > 0);
// Printing the reverse number
System.out.println("Reverse of given number: "
+ rev);
// Summing up digits of number as shown in above
// steps
System.out.println("Sum of digits of given number: "
+ sum);
}
}
OutputEnter the number: 25
Reverse of given number: 52
Sum of digits of given number: 7
Example 2: In this example separately do-while loops are shown as an illustration by creating functions of them and later on calling them in the main driver method.
Java
import java.io.*;
// Importing specific Scanner Class to show menu
import java.util.Scanner;
class GFG {
// Iterative function to reverse digits of number
// static int reverseDigits(int num)
// Hardcoded input where input number equals 25
static int reverseDigits(int num)
{
// Making input hard coded else
num = 25;
// else do not initialize num
// creating and Initialising reverseNo with 0
// Creating remainder variable
int rev = 0, rem;
// Statements to be executed in do loop
do {
// Reversal of a number as discussed in
// algorithm
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
// Condition check
while (num > 0);
// Returning reverse of the user enter number
return rev;
}
// Iterative function findingsum of the digits of number
// static int sumDigits(int num)
// Hardcoded input where input number equals 25
static int sumDigits(int num)
{
// Making input hard coded
num = 25;
// else do not initialize num
// creating and Initialising final_sum with 0
// Creating remainder variable
int sum = 0, rem;
// Statements to be executed in do loop
do {
// Retrieving steps as discussed in above 3
// steps
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
// condition check
while (num > 0);
// Returning Sum of digits of a reversed number
return sum;
}
// Main driver method
public static void main(String[] args)
{
// declaring variable to store user entered number
// int num;
// Scanner Class to read the entered number
// Commented out to hard code the input
// else remove comments
// Scanner sc = new Scanner(System.in);
// Input is hardcoded for display
// else remove comments from line no 77
// Considering hard coded input
int num = 25;
// Printing message
System.out.println(num);
// Taking input from user
// Making input hard coded else
// remove comments from line no 82
// num = sc.nextInt();
// Calling above functions to print reversed number
System.out.println("Reverse of given number: "
+ reversDigits(num));
// Calling above functions to print reversed number
System.out.println("Sum of digits of given number: "
+ sumDigits(num));
}
}
Output25
Reverse of given number: 52
Sum of digits of given number: 7
Time complexity: O(logn) where n is given input number.
Auxiliary space: O(1)
Similar Reads
Java Program to Find Sum of Natural Numbers Using While Loop While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test conditi
3 min read
Java Program to Find Reverse of a Number Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Step 1: Assume the smaller problem from the problem that is similar to the bigger/origi
5 min read
Java Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
Java Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to
6 min read
Java Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re
4 min read
Java Program to Increment by 1 to all the Digits of a given Integer Given an integer, the task is to generate a Java Program to Increment by 1 All the Digits of a given Integer. Examples: Input: 12345 Output: 23456 Input: 110102 Output: 221213 Approach 1: In this approach, we will create a number which will be of the same length as the input and will contain only 1
2 min read
Reverse Number Program in Java In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on, till the middle element.Example of reversing a number:Input: n = 357Output: 753Input n = 100Output: 1 ( leading zeros
3 min read
Java Program To Write Your Own atoi() The atoi() function in Java converts a numeric string into an integer. Unlike Javaâs built-in methods like Integer.parseInt(), we will implement our own version of atoi() with different levels of error handling and special case handling.Syntax: int myAtoi(String str);Parameter: str The numeric strin
6 min read
Palindrome Number Program in Java A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.Example of Palindrome Number:Input : n = 121Output: Reverse of n = 121Palindrome : YesIn
7 min read
How to Convert a String Class to an Integer Class in Java? String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1
3 min read