Maths Java

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

public class Main{

static int countDigit(int num) {


int count =0;
if(num == 0){//base case
count =1;
System.out.println("No of digits in "+num+" is : "+count);
return count;
}
while(num>0 ) {
num/=10;
count++;
}
System.out.println("No of digits is : "+count);
return count;
}

static void countDigitUsingLog(int num) {


int count = 0;
if(num==0){
count=1;
System.out.println("Number of digits is: "+count);
return;
}
count = (int)(Math.log(num)/Math.log(10)) + 1;
System.out.println("Number of digits is: "+count);
}

static void isArmstrong(int num) {


int originalNum = num;
int res=0;
while(num > 0){
int digit = num%10;
res += digit*digit*digit;
num /= 10;
}
System.out.println(res == originalNum);
}

static int revNum(int num){


int res =0;
while(num>0){
int digit = num%10;
res = res*10 + digit;
num /= 10;
}
return res;
}

static void isPalindrome(int num){


System.out.println(num==revNum(num));
}

static void printAllDivisors(int num){


int counter=1;
while(counter <= (int)Math.sqrt(num)){
if(num%counter == 0){
System.out.println(counter+" is divisor of "+num);
}
int otherNum = num/counter;
if(otherNum!=counter){
System.out.println(otherNum+" is divisor of "+num);
}
counter++;
}
}

public static void main(String[] args){


// countDigit(1001);
// countDigitUsingLog(1034);
// isArmstrong(158);
// isPalindrome(12212);
// printAllDivisors(36);
}
}

You might also like