NUMBER PROGRAMS while(num !
= 0) {
1️⃣ Reverse a number int digit = num % 10;
2️⃣ Palindrome number rev = rev * 10 + digit;
3️⃣ Fibonacci series num = num / 10;
4️⃣ Prime number }
5️⃣ Factorial if(temp == rev)
6️⃣ Swap 2 numbers without third variable [Link]("Palindrome
7️⃣ Count digits in number Number");
8️⃣ Armstrong number else
9️⃣ Sum of digits [Link]("Not a
🔟 Generate random numbers Palindrome");
}
}
1. Reverse a Number
3. Fibonacci Series
Take an integer input.
Initialize a variable rev = 0. Take input for number of terms.
Use a loop until the number becomes Initialize first two terms as 0 and 1.
0. Print them.
Extract the last digit using % 10. Use a loop to generate next terms by
Multiply rev by 10 and add the digit. adding previous two.
Divide the number by 10.
Print the reversed number. Sample Code:
Sample Code: public class FibonacciSeries {
public static void main(String[] args) {
public class ReverseNumber { int n = 10, a = 0, b = 1;
public static void main(String[] args) { [Link]("Fibonacci Series: " + a
int num = 1234, rev = 0; + " " + b);
while(num != 0) { for(int i = 2; i < n; i++) {
int digit = num % 10; int c = a + b;
rev = rev * 10 + digit; [Link](" " + c);
num = num / 10; a = b;
} b = c;
[Link]("Reversed Number: " }
+ rev); }
} }
}
4. Prime Number
2. Palindrome Number
Take an integer input.
Take an integer input. Check divisibility from 2 to sqrt(n).
Reverse the number using the same If divisible, not prime.
logic as above. Else, prime.
Compare original number with
reversed number. Sample Code:
If equal, it is a palindrome.
public class PrimeNumber {
Sample Code: public static void main(String[] args) {
int num = 29;
public class PalindromeNumber { boolean isPrime = true;
public static void main(String[] args) {
int num = 121, temp = num, rev = 0; for(int i = 2; i <= [Link](num); i++) {
if(num % i == 0) { Take an integer input.
isPrime = false; Initialize count = 0.
break; Use loop until number becomes 0.
} Divide by 10 each time and increment
} count.
if(isPrime) Print count.
[Link](num + " is Prime");
else Sample Code:
[Link](num + " is Not
Prime"); public class CountDigits {
} public static void main(String[] args) {
} int num = 12345, count = 0;
while(num != 0) {
5. Factorial num = num / 10;
count++;
Take an integer input. }
Initialize result = 1. [Link]("Number of Digits: "
Multiply result with numbers from 1 + count);
to n. }
Print factorial. }
Sample Code: 8. Armstrong Number
public class Factorial { Take an integer input.
public static void main(String[] args) { Count digits.
int num = 5, fact = 1; Extract each digit, raise to power of
for(int i = 1; i <= num; i++) { count, add to sum.
fact *= i; Compare sum with original number.
}
[Link]("Factorial: " + fact); Sample Code:
}
} public class ArmstrongNumber {
public static void main(String[] args) {
6. Swap 2 Numbers Without Third Variable int num = 153, temp = num, sum = 0;
int digits = [Link](num).length();
Take two numbers.
Use arithmetic operations to swap. while(num != 0) {
Print swapped values. int digit = num % 10;
sum += [Link](digit, digits);
Sample Code: num = num / 10;
}
public class SwapNumbers { if(sum == temp)
public static void main(String[] args) { [Link](temp + " is
int a = 10, b = 20; Armstrong");
a = a + b; else
b = a - b; [Link](temp + " is Not
a = a - b; Armstrong");
[Link]("After Swap: a = " + a }
+ ", b = " + b); }
}
} 9. Sum of Digits
7. Count Digits in Number Take an integer input.
Initialize sum = 0.
Extract digits using % 10.
Add to sum.
Divide number by 10.
Print sum.
Sample Code:
public class SumOfDigits {
public static void main(String[] args) {
int num = 1234, sum = 0;
while(num != 0) {
sum += num % 10;
num = num / 10;
}
[Link]("Sum of Digits: " +
sum);
}
}
10. Generate Random Numbers
Import [Link].
Create Random object.
Use nextInt() or nextDouble() to
generate random values.
Print random numbers.
Sample Code:
import [Link];
public class RandomNumbers {
public static void main(String[] args) {
Random rand = new Random();
[Link]("Random Integer: " +
[Link](100));
[Link]("Random Double: " +
[Link]());
}
}