Finding Number of Digits in a Number

The following Java program finds the number of digits in a number. This program converts the number to a string and then prints its length,

public class DigitsInNumber {

    public static void main(String[] args ) {
        int number = 94487;
        String s = String.valueOf(number);
        System.out.println(number + " has "+ s.length()+" digits");
    }
}

The following is an alternate Java implementation which finds number of digits in a number without converting it into a string. This program divides the number by 10 until the number becomes 0. The number of iterations is equal to the number of digits.

public class DigitsInNumber {

    public static void main(String[] args ) {
        int original_number = 1119;
        int n = original_number;
        int digits = 0;
        while(n > 0 ) {
            digits ++;
            n = n / 10;
        }
        System.out.println(original_number + " has "+ digits+" digits");
    }
}

Program to Find all Armstrong Numbers in a Range

A number n is said to be an Armstrong number if it is equal to the n-th power of its digits. For example consider the number 407,

The number of digits(n) is 3. The sum of 3rd power of the digits = 4^3+0^3+7^3 = 407. Hence 407 is an Armstrong number. All single digit numbers are Armstrong numbers while there are no 2 digit Armstrong numbers. Armstrong numbers are also known as Narcissistic numbers.

Finding all Armstrong Numbers in a Range Using Java

The following Java program finds all the Armstrong numbers in a given range. The program is coded to find Armstrong numbers between 0 and 99999.  You can easily change the range by modifying the values of starting_number and ending_number.

/* Java program to find Armstrong numbers in a range */

public class ArmstrongFinder {

    public static void main(String[] args) {
        int starting_number = 1;
        int ending_number = 99999;
        for (int i = starting_number; i <= ending_number; i++) {
            if (isArmstrong(i)) {
                System.out.println(i + " is an Armstrong number!");
            } else {
                
            }
        }
    }

    public static boolean isArmstrong(int n) {
        int no_of_digits = String.valueOf(n).length();
        int sum = 0;
        int value = n;
        for (int i = 1; i <= no_of_digits; i++) {
            int digit = value % 10;
            value = value / 10;
            sum = sum + (int) Math.pow(digit, no_of_digits);
        }
        if (sum == n) {
            return true;
        } else {
            return false;
        }
    }
}

The above program found the following Armstrong numbers between 0 and 99999,

1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084

Program to Check Armstrong Number in Java

What is Armstrong number?

An n digit number is said to be an Armstrong number if the sum of n-th power of its digits is the n digit number itself. For example consider the number 371,

  • The number of digits = 3
  • The sum of 3rd power of digits = 3^3 + 7^3 + 1^3 =  27+343+1 = 371

Hence 371 is an Armstrong number. Some of the other Armstrong numbers are 6, 153 and 1634.

 

Checking for Armstrong Number in Java

The following program checks whether a given number is an Armstrong number.

/* Java program to check whether a given number is an Armstrong number */
public class ArmstrongChecker {

    public static void main(String[] args) {
        int number = 153;
        if(isArmstrong(number)) {
            System.out.println(number + " is an Armstrong number!");
        } else {
            System.out.println(number + " is an NOT Armstrong number!");
        }
    }
    
    public static boolean isArmstrong(int n) {
        int no_of_digits = String.valueOf(n).length();
        int sum = 0;
        int value = n;
        for(int i=1;i<=no_of_digits;i++) {
            int digit = value%10;
            value = value/10;
            sum = sum + (int)Math.pow(digit, no_of_digits);
        }
        if(sum == n) {
            return true;
        }else {
            return false;
        }
    }
    
}

Factorial Program in Java

In mathematics, the factorial of an integer is defined as the product of all positive integers less than or equal to the integer. Factorial of integer n is denoted by n!. For example, factorial of 5 is (5!) = 5 * 4 * 3 * 2 * 1 = 120.

Factorial Program in Java

The following example program in Java prints the factorial of a given integer. The program uses a simple loop to generate the factorial.

/* factorial example in java */
public class Factorial {

    public static void main(String[] args) {
        printFactorial(5);
    }
    
    public static void printFactorial(int n) {
        int factorial = 1;
        for(int i=1;i<=n;i++) {
            factorial = factorial * i;
        }
        System.out.println(factorial);
    }
}

 

Factorial Program in Java Using Recursion

The following Java example generates factorial of an integer using recursion.

/* factorial example program in java using recursion */
public class FactorialRecursion {
    public static void main(String[] args) {
        int factorial = getFactorial(5);
        System.out.println(factorial);
    }
    
    public static int getFactorial(int n) {
        if(n>1) {
            return n*getFactorial(n-1);
        }else {
            return 1;
        }
    }
}

Fibonacci Series in Java

Fibonacci series is a sequence of numbers where a number in the sequence is the sum of two previous numbers. By definition, the first two numbers in the series are 0 and 1. The following are the first 12 numbers in the Fibonacci series,

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, .....

Generate Fibonacci Series in Java

The following example program generates and prints Fibaonacci numbers to the console,

/* Finabonacci series example program in Java */
public class Fibonacci {
    
    public static void main(String[] args) {
        generateFibonacci(20);  // generate the first 20 fibonacci numbers
    }
    
    public static void generateFibonacci(long limit) {
        long first = 0;
        long second = 1;
        System.out.print(first+", "+second);
        for(long i=1;i<limit;i++) {
            long next = first + second;
            System.out.print(", "+next);
            first = second;
            second = next;
        }
        System.out.println();
    }
    
}

 

Generate Fibonacci Series in Java Using Recursion

The following example shows how recursion can be used in Java to generate Fibonacci numbers. For large values of limit, the program may run out of stack space.

/* Recursive Fibonacci Series Program in Java */
public class FibonacciRecursion {
    
    public static void main(String[] args) {
        
        long limit = 20;
        for(long i =1; i <=limit;i++) {
            System.out.print(fibonacci(i)+", ");
        }
        
        
    }
    
    public static long fibonacci(long n) {
        if(n<=2) return n-1;
        return fibonacci(n-1)+fibonacci(n-2);
    }
    
}