Course
Introduction
C "Hello, World!" ProgramPrint an Integer (Entered by the User)Add Two IntegersMultiply Two Floating-Point NumbersFind ASCII Value of a CharacterCompute Quotient and RemainderFind the Size of int, float, double and charSwap Two NumbersDecision Making and Loops
Check Whether a Number is Even or OddCheck Whether a Character is a Vowel or ConsonantFind the Largest Number Among Three NumbersFind the Roots of a Quadratic EquationCheck Leap YearCheck Whether a Number is Positive or NegativeCheck Whether a Character is an Alphabet or notCalculate the Sum of Natural NumbersFind Factorial of a NumberGenerate Multiplication TableDisplay Fibonacci SequenceFind GCD of two NumbersFind LCM of two NumbersDisplay Characters from A to Z Using LoopCount Number of Digits in an IntegerReverse a NumberCalculate the Power of a NumberCheck Whether a Number is Palindrome or NotCheck Whether a Number is Prime or NotDisplay Prime Numbers Between Two IntervalsCheck Armstrong NumberDisplay Armstrong Number Between Two IntervalsDisplay Factors of a NumberMake a Simple Calculator Using switch...casePrint Pyramids and PatternsFunctions
Demonstrate the Working of Keyword longDisplay Prime Numbers Between Intervals Using FunctionCheck Prime or Armstrong Number Using User-defined FunctionCheck Whether a Number can be Expressed as Sum of Two Prime NumbersFind the Sum of Natural Numbers using RecursionFind Factorial of a Number Using RecursionFind G.C.D Using RecursionConvert Binary Number to Decimal and vice-versaConvert Octal Number to Decimal and vice-versaConvert Binary Number to Octal and vice-versaReverse a Sentence Using Recursioncalculate the power using recursionArrays and Pointers
Calculate Average Using ArraysFind Largest Element in an ArrayCalculate Standard DeviationAdd Two Matrices Using Multi-dimensional ArraysMultiply Two Matrices Using Multi-dimensional ArraysFind Transpose of a MatrixMultiply two Matrices by Passing Matrix to a FunctionAccess Array Elements Using PointerC Program Swap Numbers in Cyclic Order Using Call by ReferenceFind Largest Number Using Dynamic Memory AllocationStrings
Find the Frequency of Characters in a StringCount the Number of Vowels, Consonants and so onRemove all Characters in a String Except AlphabetsFind the Length of a StringConcatenate Two StringsCopy String Without Using strcpy()Sort Elements in Lexicographical Order (Dictionary Order)Structures and Unions
Store Information of a Student Using StructureAdd Two Distances (in inch-feet system) using StructuresAdd Two Complex Numbers by Passing Structure to a FunctionCalculate Difference Between Two Time PeriodsStore Information of Students Using StructureStore Data in Structures DynamicallyJava Program to Reverse a Number
To understand this example, you should have the knowledge of the following Java programming topics:
Example 1: Reverse a Number using a while loop in Java
class Main {
public static void main(String[] args) {
int num = 1234, reversed = 0;
System.out.println("Original Number: " + num);
// run loop until num becomes 0
while(num != 0) {
// get last digit from num
int digit = num % 10;
reversed = reversed * 10 + digit;
// remove the last digit from num
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}
}
Output
Reversed Number: 4321
In this program, while loop is used to reverse a number as given in the following steps:
- First, the remainder of the num divided by 10 is stored in the variable digit. Now, the digit contains the last digit of num, i.e. 4. digit is then added to the variable reversed after multiplying it by 10. Multiplication by 10 adds a new place in the reversed number. One-th place multiplied by 10 gives you tenth place, tenth gives you hundredth, and so on. In this case, reversed contains 0 * 10 + 4 = 4. num is then divided by 10 so that now it only contains the first three digits: 123.
- After second iteration, digit equals 3, reversed equals 4 * 10 + 3 = 43 and num = 12
- After third iteration, digit equals 2, reversed equals 43 * 10 + 2 = 432 and num = 1
- After fourth iteration, digit equals 1, reversed equals 432 * 10 + 1 = 4321 and num = 0
- Now num = 0, so the test expression
num != 0
fails and while loop exits. reversed already contains the reversed number 4321.
Example 2: Reverse a number using a for loop in Java
class Main {
public static void main(String[] args) {
int num = 1234567, reversed = 0;
for(;num != 0; num /= 10) {
int digit = num % 10;
reversed = reversed * 10 + digit;
}
System.out.println("Reversed Number: " + reversed);
}
}
Output
Reversed Number: 7654321
In the above program, the while loop is replaced by a for loop where:
- no initialization expression is needed inside the loop since
num
is already initialized before the loop and we want to avoid resetting it with each iteration - test expression remains the same (
num != 0
) - update/increment expression contains
num /= 10
.
So, after each iteration, the update expression runs which removes the last digit of num.
When the for loop exits, reversed will contain the reversed number.
Also Read: