- Find no. of digits in a number
public static int count_no(int n) {
int c = 0;
while (n != 0) {
n = n/10;
c++;
}
return c;
}
- Palindrome Number Eg➖121,131

public static boolean isPalin(int n){
int rev = 0;
int temp = n;
while (temp != 0) {
int last_digit = temp%10;
rev = rev * 10 + last_digit;
temp = temp/10;
}
// if (n == rev) {
// return true;
// }
// return false;
return (rev == n);
}
- factorial of a Number
public static int fact(int n) {
if (n == 0 ) {
return 1;
}
return n * fact(n-1);
}
- Trailing Zeros in Factorial of n {Eg➖5 → 1} 120*
int sum = 0;
while(n > 0){
n = n/5;
sum = sum + n;
}
return sum;
- Greatest Common Divisor
int ans = Math.min(a, b);
while (ans>0) {
if (a%ans == 0 && b%ans == 0) {
break;
}
ans--;
}
return ans;
- Euclidean Algorithm for {GCD}
int gcd(int a , int b){
if(b == 0){
return a;
}else {
return gcd(b,a%b);
}
- LCM of Two Numbers → {Smallest no. which is divisible by both no}
public static int lcm(int a , int b) {
int ans = Math.max(a, b);
while (true) {
if (ans%a == 0 && ans%b == 0) {
return ans;
}
ans++;
}
}
- Check Number is Prime or Not
public static boolean isPrime(int n) {
if (n%2 == 0) {
return false;
}
return true;
}
- Find Prime Factor of a Number
public static void primeFact(int n) {
if (n <= 1) {
return;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
while (n%i == 0) {
System.out.println(i);
n = n/i;
}
}
if (n > 1) {
System.out.println(n);
}
}
- All Divisor of a Number