❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Task – While Loop – Armstrong, Neon, Perfect, Prime, emirP, Strong

By: Sugirtha
13 October 2024 at 18:19
package taskPkg;

import java.util.Scanner;
public class PlayingWithNumbers {
//Code for Armstrong, Neon, Perfect, Prime, emirP, Strong Numbers
// Factorial, SumOfDigits, ReverseDigits, CountOfDigits, Power also used.
    	// FOR PRINTING 
	private static void print(int n, String numType, boolean givenNumType) {
		if (givenNumType) System.out.println(n+"  is a " + numType +" Number.");
		else System.out.println(n+"  is not a " + numType +" Number.");
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PlayingWithNumbers loop = new PlayingWithNumbers();
        System.out.print("Enter a Number to Play = ");
        Scanner scn = new Scanner(System.in);
        int num = scn.nextInt();
        scn.close();
		

		PlayingWithNumbers.print(num, "ArmStrong",loop.isArmstrongNo(num));
		PlayingWithNumbers.print(num, "Neon", loop.isNeonNo(num));
		PlayingWithNumbers.print(num, "Perfect",loop.isPerfectNo(num));
		PlayingWithNumbers.print(num, "Prime",loop.isPrimeNo(num));
		PlayingWithNumbers.print(num, "emirP",loop.isemirPNo(num));
		PlayingWithNumbers.print(num, "Strong",loop.isStrong(num));
		
	}
	
	private boolean isArmstrongNo(int n) {
		//Sum of (each digit to the power of No of digits) == n
		int num=n, powSum=0, nod = countDigits(num);
		while (num > 0) {
			powSum += findPower((num%10),nod);
			num/=10;
		}
		if (n == powSum) return true;
		return false;
	}
	
	private boolean isNeonNo(int n) {
		//Neon = (Sum of digits of a squared number == number)
		int sqNum = n*n;
		if (n == sumOfDigits(sqNum)) return true;
		return false;
	}
	
	private boolean isPerfectNo(int n) {
		//Sum of its divisors (excluding n) == n
		int i=2, sum=1;
		while (i<n) {
			if (n%i == 0) sum+=i; // Adding Divisors
			i++;
		}
		if (sum==n) return true;
		return false;
	}
	
	private boolean isPrimeNo(int n) {
		// N number that can be divided exactly only by itself, ie n's divisors are 1 and n
		if (n<=1 || n%2 == 0) return false;
		if (n<=3) return true;
		int i=3;
		while (i < n) {
			if (n%i == 0) {
				return false;
			}
			i+=2;
		}
		return true;
	}
	
	private boolean isemirPNo(int n) {
		// A number and revereDigits of that number are prime
		int rev = reverseDigits(n);
		return (isPrimeNo(n) && isPrimeNo(rev))? true:false;
	}
	
	private boolean isStrong(int n) {
		//sum of the factorial of its digit is equal to number itself
		// Ex. 145 = 1!+4!+5!
		int sum = 0, num=n;
		while (num>0) {
			sum += factorial(num%10);
			num/=10;
		}
		return (n == sum)? true:false; 
	}
	
	private int findPower(int base, int pr) {
		// finding base to the power pr
		int res=1;
		while (pr>=1) {
			res *= base;
			pr--;
		}
		return res;
	}
	
	private int reverseDigits(int num) {
		int rev = 0;
		while (num>0) {
			rev = rev*10 + num%10;
			num/=10;
		}
		return rev;
	}
	private int countDigits(int num) { //Counting No. of Digits
		int cnt=0;
		while (num>0) {
			num/=10;
			cnt++;
		}
		return cnt;
	}
	private int sumOfDigits(int n) {
		int sum=0;
		while (n>0) {
			sum += n%10;
			n/=10;
		}
		return sum;
	}
	
	private int factorial(int n) {
		int i=1,fact=1;
		while (i<=n) {
			fact *= i;
			i++;
		}
		return fact;
	}	

}


Output:

❌
❌