❌

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:

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:

❌
❌