❌

Normal view

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

Task – For Loop – Playing with Numbers

By: Sugirtha
17 October 2024 at 14:46
Armstrong, Neon, Perfect, Prime, emirP, Strong, Triangle Series
import java.util.Scanner;

public class forLoopPlayWithNos {
// USING FOR LOOP
//Code for Armstrong, Neon, Perfect, Prime, emirP, Strong Numbers
// Factorial, SumOfDigits, ReverseDigits, CountOfDigits, Power also used.
//Triangular Series : 1,3,6,10,15,21,28,....
	    // FOR PRINTING 
		private static void print(int n, String numType, boolean matched) {
			if (matched) 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
			forLoopPlayWithNos loop = new forLoopPlayWithNos();
	        System.out.print("Enter a Number to Play = ");
	        Scanner scn = new Scanner(System.in);
	        int num = scn.nextInt();
	        scn.close();
			
	        forLoopPlayWithNos.print(num, "ArmStrong",loop.isArmstrongNo(num));
	        forLoopPlayWithNos.print(num, "Neon", loop.isNeonNo(num));
	        forLoopPlayWithNos.print(num, "Perfect",loop.isPerfectNo(num));
	        forLoopPlayWithNos.print(num, "Prime",loop.isPrimeNo(num));
	        forLoopPlayWithNos.print(num, "emirP",loop.isemirPNo(num));
	        forLoopPlayWithNos.print(num, "Strong",loop.isStrong(num));
			loop.tringangularSeries(num);
		}
		
		private boolean isArmstrongNo(int n) {
			//Sum of (each digit to the power of (No of digits)) == n
			// Ex. 371, 153 == 1^3 + 5^3 + 3^3 = 1+125+27
			int powSum=0, nod = countDigits(n);
			for (int num=n; num>0; num/=10) {
				powSum += findPower((num%10),nod);
			}
			if (n == powSum) return true;
			return false;
		}
		
		private boolean isNeonNo(int n) {
			//Neon = (Sum of digits of a squared number == number)
			// Ex 1,9,45,55
			int sqNum = n*n;
			if (n == sumOfDigits(sqNum)) return true;
			return false;
		}
		
		private boolean isPerfectNo(int n) {
			//Sum of its divisors (excluding n) == n
			//Ex. 6,28,496
			int sum=1;
			for(int i=2; i<n; i++) {
				if (n%i == 0) sum+=i; // Adding Divisors
			}
			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
			// Ex 2,3,5,7,11,13
			switch(n) {
			case 0,1:
				return false;
			case 2,3:
				return true;
			default:
				if (n%2 == 0) return false;
				else {
					for (int i=3; i*i<n; i+=2) {
						if (n%i == 0) {
							return false;
						}
					}
				}
			}
			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;
			for (num=n; num>0; num/=10) {
				sum += factorial(num%10);
			}
			return (n == sum)? true:false; 
		}
		
		private int findPower(int base, int pr) {
			// finding base to the power pr
			int res=1;
			for (; pr>=1; pr--) {
				res *= base;
			}
			return res;
		}
		
		private int reverseDigits(int num) {
			int rev = 0;
			for (; num>0; num/=10) {
				rev = rev*10 + num%10;
			}
			return rev;
		}
		private int countDigits(int num) { //Counting No. of Digits
			int cnt=0;
			for (; num>0; num/=10,cnt++); 
			return cnt;
		}
		private int sumOfDigits(int n) {
			int sum=0;
			for (; n>0; n/=10) {
				sum += n%10;
			}
			return sum;
		}
		
		private int factorial(int n) {
			int fact=1;
			for (int i=1; i<=n; i++) {
				fact *= i;
			}
			return fact;
		}	
		
		private void tringangularSeries(int n) {
			System.out.print("Tringangular Series : ");
			for (int tri=1, i=2; tri<=n; i++) {
				System.out.print(tri+" ");
				tri+=i;
			}
		}
}

OUTPUT:

❌
❌