In the previous article, we have seen Java Program to Check Pointer Prime Number
In this article we are going to see how we can write a program to find out whether the number is Canada number or not.
Java Program to Check Canada Numbers
The number whose sum of square of digit is equal to the sum if of non- trivial factor of that number is called Canada number.
Let’s see different ways to check whether the number is Canada number or not.
Method-1: Java Program to Check Canada Numbers By Using Static Value
Approach :
- Declare a number.
- Calculate its sum of the square of the digits.
- Calculate its sum of non trivial factors.
- Check if both are same or not. If same print number as Canada number else not Canada number .
Program :
import java.util.*; public class Main { // Driver Code public static void main (String[] args) { // Given Number int num = 8549; // Function Call if (isCanada(num)) System.out.println("Number is Canada Number"); else System.out.println("Number is not Canada Number"); } // Function to return sum of squares of digits of a number static int gets(int num) { int s = 0; while (num != 0) { int r = num % 10; s = s + r * r; num = num / 10; } return s ; } // Function to calculate sum of all trivial divisors of given number static int divs(int numm) { // Final result of sum of trivial divisors int res = 0; // Find all divisors which divides 'numm' for (int x = 1; x <= Math.sqrt(numm); x++) { // if 'x' is divisor of 'numm' if (numm % x == 0) { // if both divisors are same then add it only once else add both if (x == (numm / x)) res += x; else res += (x + numm / x); } } return (res - 1 - numm); } // Function to check if N is a Canada number static boolean isCanada(int num) { return divs(num) == gets(num); } }
Output: Number is Canada Number
Method-2: Java Program to Check Canada Numbers By User Input Value
Approach :
- Take input of a number.
- Calculate its sum of the square of the digits.
- Calculate its sum of non trivial factors.
- Check if both are same or not. If same print number as Canada number else not Canada number .
Program :
import java.util.*; public class Main { // Driver Code public static void main (String[] args) { Scanner s = new Scanner(System.in); // entering the number through user input System.out.print("Enter a number : "); int num= s.nextInt(); // Function Call if (isCanada(num)) System.out.println("Number is Canada Number"); else System.out.println("Number is not Canada Number"); } // Function to return sum of squares of digits of a number static int gets(int num) { int s = 0; while (num != 0) { int r = num % 10; s = s + r * r; num = num / 10; } return s ; } // Function to calculate sum of all trivial divisors of given number static int divs(int numm) { // Final result of sum of trivial divisors int res = 0; // Find all divisors which divides 'numm' for (int x = 1; x <= Math.sqrt(numm); x++) { // if 'x' is divisor of 'numm' if (numm % x == 0) { // if both divisors are same then add it only once else add both if (x == (numm / x)) res += x; else res += (x + numm / x); } } return (res - 1 - numm); } // Function to check if N is a Canada number static boolean isCanada(int num) { return divs(num) == gets(num); } }
Output: Enter a number : 16999 Number is Canada Number
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: