img

Numbers In Java : 10 Practice Problems and Solutions

This is the solution for the practice question set on different numbers (like Harshad, Armstrong, Perfect, and so on) and their properties as well as the method for checking given numbers. You can find the corresponding list of questions here. Try solving the questions yourself before you go through the solutions.


Palindrome Number


Problem: Write a program that takes an integer and checks if it’s a palindrome (the number reads the same forwards and backwards).

Example: `121` is a palindrome, but `123` is not.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,copyn,num=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //storing a copy of n

        copyn=n;

        //reversing n

        while(n>0)

        {

            num = num * 10 + (n % 10);

            n = n / 10;

        }

        //comparing original n and reversed n

        if(copyn==num)

            System.out.println("Palindrome Number!");

        else

            System.out.println("Not a Palindrome Number!");

    }

}


Perfect Number


Problem: A perfect number is one where the sum of its proper divisors (excluding the number itself) equals the number. Write a program to check if a given number is perfect.

Example: `6` (1 + 2 + 3 = 6) is a perfect number, but `10` is not.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,i,sum=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //finding sum of the divisors of n

        for(i=1;i<n;i++)

        {

            if(n%i==0)

                sum+=i;

        }

        //comparing n and sum of divisors

        if(n==sum)

            System.out.println("Perfect Number!");

        else

            System.out.println("Not a Perfect Number!");

    }

}


Armstrong Number


Problem: An Armstrong number (or Narcissistic number) for a 3-digit number is one where the sum of each digit raised to the power of 3 equals the number. Extend this definition to check if any `n`-digit number is an Armstrong number.

Example: `153` is an Armstrong number because 1³ + 5³ + 3³ = 153.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,copyn,sum=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //storing a copy of n

        copyn=n;

        //finding the required sum

        while(n>0)

        {

            sum += (int)Math.pow(n%10,3);

            n = n / 10;

        }

        //comparing original n and sum of each digit raised to the power of 3

        if(copyn == sum)

            System.out.println("Armstrong Number!");

        else

            System.out.println("Not an Armstrong Number!");

    }

}


Amicable Number


Problem: Two numbers are amicable if the sum of the proper divisors of one equals the other, and vice versa. Write a program to check if two numbers are amicable.

Example: `220` and `284` are amicable because the sum of divisors of `220` is `284`, and vice versa.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n1,n2,i,sum1=0,sum2=0;

        //taking input

        System.out.print("Enter the first number : ");

        n1=sc.nextInt();

        System.out.print("Enter the second number : ");

        n2=sc.nextInt();

        //finding the sum of divisors for n1

        for(i=1;i<n1;i++)

        {

            if(n1%i==0)

                sum1+=i;

        }

        //finding the sum of divisors for n2

        for(i=1;i<n2;i++)

        {

            if(n2%i==0)

                sum2+=i;

        }

        //comparing the sum of divisors with the numbers

        if(sum1 == n2 && sum2 == n1)

            System.out.println("Amicable Numbers!");

        else

            System.out.println("Not Amicable Numbers!");

    }

}


Automorphic Number


Problem: An automorphic number is one whose square ends in the number itself. Write a program to check if a given number is automorphic.

Example: `5` is automorphic because 5² = 25, and `76` is also automorphic because 76² = 5776.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,copyn,digits=0,sqN,powerof10,lastOfSq;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //storing a copy of n

        copyn=n;

        //finding the number of digits

        while(copyn>0)

        {

            digits++;

            copyn = copyn / 10;

        }

        //finding last digits of square of n

        sqN=(int)Math.pow(n,2);

        powerof10=(int)Math.pow(10,digits);

        lastOfSq=sqN%powerof10;

        //comparing n and last digits of square of n

        if(n == lastOfSq)

            System.out.println("Automorphic Number!");

        else

            System.out.println("Not an Automorphic Number!");

    }

}


Harshad Number


Problem: A Harshad (or Niven) number is divisible by the sum of its digits. Write a program to check if a given number is a Harshad number.

Example: `18` is a Harshad number because 1 + 8 = 9, and `18` is divisible by `9`.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,sum=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //finding the sum of digits

        while(n>0)

        {

            sum += (n % 10);

            n = n / 10;

        }

        //is the sum of digits equal to 9?

        if(sum == 9)

            System.out.println("Harshad Number!");

        else

            System.out.println("Not a Harshad Number!");

    }

}


Happy Number


Problem: A number is called "happy" if repeatedly summing the squares of its digits eventually leads to `1`. Write a program to check if a given number is a happy number.

Example: `19` is a happy number because 1² + 9² = 82, 8² + 2² = 68, 6² + 8² = 100, and 1² + 0² + 0² = 1.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,sum=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //while the sum of the square digits is not a single digit

        while(n>9)

        {

            //resetting sum

            sum = 0;

            //finding the sum of the square of digits

            while(n>0)

            {

                int num = (int)Math.pow(n % 10, 2);

                sum += num;

                n = n / 10;

            }

            //new number is the sum of the square of digits of previous number

            n = sum;

        }

        //is the final sum of digits equal to 1?

        if(n == 1)

            System.out.println("Happy Number!");

        else

            System.out.println("Not a Happy Number!");

    }

}


Kaprekar Number


Problem: A Kaprekar number is a number whose square can be split into two parts that add up to the original number. Write a program to check if a given number is a Kaprekar number.

Example: `45` is a Kaprekar number because 45² = 2025, and `20 + 25 = 45`.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,copyn,digits=0,sqN,powerof10,sumOfSq;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //storing a copy of n

        copyn=n;

        //finding the number of digits

        while(copyn>0)

        {

            digits++;

            copyn = copyn / 10;

        }

        //finding sum of parts of square of n

        sqN=(int)Math.pow(n,2);

        powerof10=(int)Math.pow(10,digits);

        sumOfSq=(sqN/powerof10)+(sqN%powerof10);

        //comparing n and sum of parts of square of n

        if(n == sumOfSq)

            System.out.println("Kaprekar Number!");

        else

            System.out.println("Not a Kaprekar Number!");

    }

}


Fascinating Number


Problem: A number `n` is considered "fascinating" if the concatenation of `n`, `2*n`, and `3*n` contains all digits from 1 to 9 exactly once. Write a program to check if a number is fascinating.

Example: `192` is fascinating because 192 x 1 = 192, 192 x 2 = 384, and 192 x 3 = 576, and concatenating `192384576` contains all digits from `1` to `9`.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,copyn,powerof10,rem,newNumber,i,digits=0,count=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //storing a copy of n

        copyn=n;

        //finding number of digits in n

        while(copyn>0)

        {

            digits++;

            copyn = copyn / 10;

        }

        //finding power of 10 for new number

        powerof10 = (int)Math.pow(10,digits);

        //finding the new number

        // eg. 192 -> 192000000 + 384000 + 576

        newNumber = (n * powerof10 * powerof10) + ((n*2) * powerof10) + (n*3);  

        //checking for all digits

        while(newNumber>0)

        {

            //finding the last digit

            rem = newNumber % 10;

            //comparing to all digits for an occurrence

            for(i=1;i<=9;i++)

            {

                if(rem==i)

                {

                    count++;

                }

            }

            newNumber /= 10;

        }

        //count should be exactly 9 for one occurrence of each digit

        if(count == 9)

            System.out.println("Fascinating Number!");

        else

            System.out.println("Not a Fascinating Number!");

    }

}


Pronic Number


Problem: A pronic (or rectangular) number is the product of two consecutive integers. Write a program to check if a given number is pronic.

Example: `12` is a pronic number because it can be expressed as 3 x 4 = 12.


CODE : 


import java.io.*;

import java.util.*;


class Main {

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int n,i,count=0;

        //taking input

        System.out.print("Enter the number : ");

        n=sc.nextInt();

        //performing calculations

        for(i=1;i<n;i++)

        {

            //checking for divisor

            if(n%i==0)

            {

                //checking product of consecutive numbers

                if(i * (i+1) == n)

                {

                    count++;

                    break;

                }

            }

        }

        //at least one pair of consecutive numbers are divisors of n

        if(count >= 1)

            System.out.println("Pronic Number!");

        else

            System.out.println("Not a Pronic Number!");

    }

}



post-img

For While Loops: 10 Practice Problems an...

This is the solution for the practice question set on the concept of iteration statements (for and while loops).

Read More

post-img

Switch Case Simplified: 5 Solved Practic...

This is the solution for the practice question set on the concepts of switch case and fall through.

Read More

post-img

Mastering If-Else Statements: 8 Solved P...

This is the solution for the practice question set on conditional (if else) statements.

Read More

philomathiq

© 2023 philomathiq. All rights reserved.