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
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main(String[] args)
{
int num, rem;
int rev = 0 , sum = 0 ;
System.out.println( "Enter the number: 25 " );
num = 25 ;
do {
rem = num % 10 ;
rev = rev * 10 + rem;
sum = sum + rem;
num = num / 10 ;
}
while (num > 0 );
System.out.println( "Reverse of given number: "
+ rev);
System.out.println( "Sum of digits of given number: "
+ sum);
}
}
|
Output
Enter 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.*;
import java.util.Scanner;
class GFG {
static int reverseDigits( int num)
{
num = 25 ;
int rev = 0 , rem;
do {
rem = num % 10 ;
rev = rev * 10 + rem;
num = num / 10 ;
}
while (num > 0 );
return rev;
}
static int sumDigits( int num)
{
num = 25 ;
int sum = 0 , rem;
do {
rem = num % 10 ;
sum = sum + rem;
num = num / 10 ;
}
while (num > 0 );
return sum;
}
public static void main(String[] args)
{
int num = 25 ;
System.out.println(num);
System.out.println( "Reverse of given number: "
+ reversDigits(num));
System.out.println( "Sum of digits of given number: "
+ sumDigits(num));
}
}
|
Output
25
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)
Share your thoughts in the comments
Please Login to comment...