- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program To Reverse A Number And Find the Sum of its Digits Using Do-While Loop
What Is A Do-While Loop?
Do-while loop is a Java environment used to iterate a portion of a code in a repeat manner. The process will run in a continuous way until the specified condition becomes satisfied as a true condition. It is always recommended for a do-while loop to execute at least one when there is no fixed number of iteration mentioned.
In the Java environment, the do-while loop is known as an exit control loop. At the end of a loop structure, the do-while checks the whole condition to make the condition satisfied. For this the do while loop needs to be executed at least one time to check the encoded condition. In this article, we will discuss the concept of the Java code to reverse a number as well as find the Sum of its digits using a do-while loop.
Algorithm to Reverse a Number and get the sum of the Digits
The number entered by a user can be any arbitrary number as a primitive one of data. First the user needs to code how to reverse that entered number and then add the digits to get the sum.
The do-while loop executes a statement and at last checks the condition. Here; point to be noted that the given condition fails in the running process, still the loop will execute just for one time. The whole structure of the do-while loop is totally simple and easy to grab. Regardless of the condition it is the most accepted loop. Do-while loop is suitable for unknown amount of iterations.
Step 1 − Take a number as an input
Step 2 − Declare two variables named as reverse number and sum
Step 3 − Initialize the both operation to 0
Step 4 − Reverse the number
Step 5 − Print the reverse number
Step 6 − Print sum of the digits
Syntax of a Do-While Loop
do { // statement for execution encoded in the code } while (condition check)
In this method we will learn how reverse a number and get the sum of those digits by using a do-while loop. First, we need to input a random number first to perform the division operation.
Then, we need to put the logic into the code to reverse that number and after sum of its digits.
To accomplish the whole operation, we need to follow the step by step approaches mentioned below.
By reversing and adding the numbers of a particular number without build functions.
By reversing a number using a do-while loop.
By finding out the sum of the digits of that reversed number using class and object.
By Reversing and Summing up the Digits of a Number Without Making Functions
In a Java environment code is to reverse and sum up digits of the number without making functions. Where reversed number multiplied by 10 and add the reminder of the number.
Example
import java.io.*; import java.util.Scanner; public class RDDARB { public static void main(String[] args){ int num, rem; int rev = 0, sum = 0; System.out.println("Enter the desired number: 07102001 "); num = 07102001; do { rem = num % 10; rev = rev * 10 + rem; sum = sum + rem; num = num / 10; } while (num > 0); System.out.println("Reverse of the given number is : " + rev); System.out.println("Sum of digits of the given number is: " + sum); } }
Output
Enter the desired number: 07102001 Reverse of the given number is: 1088681 Sum of digits of the given number is: 32
By Reversing the Number Using Do-While loop
Steps to follow in reversing a number by Do-While Loop −
Declare and initialize int num,rem, rev_Num=0
Enter a number as input
Sorting of int variable
Use do-while loop to generate a reverse number
The do-while will run until the number is greater than 0
Get a reverse number as result
Example
import java.util.Scanner; public class Reversed_NumDoWhile{ public static void main(String args[]){ int num,rem,reversed_Num=0; Scanner scan=new Scanner(System.in); System.out.print("Enter the number for find reverse as you want to: "); num=scan.nextInt(); do{ rem=num%10; reversed_Num=reversed_Num*10+rem; num=num/10; } while(num>0); System.out.print("Hi!You entered: "+num); System.out.print("Reverse of the entered number is: "+reversed_Num); } }
Output
Enter the number for find reverse as you want to: 4444 Hi!You entered: 0 Reverse of the entered number is: 4444
By Finding the sum of the Digits of that Reversed Number Using Class and Object
You may take any number having more than one digit. Using do-while loop, we can find the reverse number of an input and also thr sum of all the digits of that number without calling or defining any functions. This loop uses modulus, division and operators process to reverse that particular number.
Example
import java.io.*; import java.util.Scanner; public class Tutorialspoint { // to reverse use iterative function static int reversDigits(int num){ num = 25; // else initialize num option omitted // reverseNo with 0 // remainder variable done int rev = 0, rem; //executed in do loop do { // Reversal of a number rem = num % 10; rev = rev * 10 + rem; num = num / 10; } while (num > 0); // Returning reverse return rev; } static int sumDigits(int num){ // Making input hard coded num = 160710; // not initialize num (else) int sum = 0, rem; do { // Retrieving // steps revert rem = num % 10; sum = sum + rem; num = num / 10; } // condition check for a while loop while (num > 0); // a reversed number's sum of digits return sum; } // run the main driver method asap public static void main(String[] args){ int num = 25; System.out.println(num); // num = sc.nextInt(); System.out.println("Reverse result of given number: " + reversDigits(num)); System.out.println("Sum of digits of given number is: " + sumDigits(num)); } }
Output
25 Reverse result of given number: 52 Sum of digits of given number is: 15
Conclusion
From this article, we have learnt how to write a Java Program to reverse a number and find the sum of its digits using a do-while Loop. By solving these two different processes together we build a Java code together by using a do-while loop.
Hope you have understood the whole process, algorithms and the approaches to solve this problem respectively.