Armstrong
Java Math.pow() Method
The Math.pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter.
If the second parameter is positive or negative zero then the result will be 1.0.
If the second parameter is 1.0 then the result will be same as that of the first parameter.
If the second parameter is NaN(Not a Number)then the result will also be NaN.
The function java.lang.Math.pow() always returns a double datatype.
Reference:https://www.geeksforgeeks.org/math-pow-method-in-java-with-example/
Syntax
**
public static double pow(double a, double b)
**
package Afterfeb4;
public class Armstrong {
public static void main(String[] args) {
int a = 1634;
int b = a;
int result = findnum(a);
System.out.println(result);
if (result == b) {
System.out.println("ARMSTRONG");
} else {
System.out.println("Not ARMSTRONG");
}
}
public static int findnum(int a) {
int sum = 0;
int rev = 0;
int count = findcount(a);
while (a > 0) {
rev = a % 10;
sum = sum + (int) Math.pow(rev, count);// math.pov is used to raise the power(count) of reversed number
a = a / 10;
}
return sum;
}
private static int findcount(int a) {
int count = 0;
while (a > 0) {
a = a / 10;
count++;
}
return count;
}
}
Output:
1634
ARMSTRONG
Strongnumber(Special Number)
Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number.
Example 1:
Input:
N = 145
Output:
1
Explanation:
1! + 4! + 5! = 145 So, 145 is a Strong
Number and therefore the Output 1.
Example 2:
Input:
N = 14
Output:
0
Explanation:
1! + 4! = 25 So, 14 isn't a Strong
Number and therefore the Output "NO".
Reference:https://www.geeksforgeeks.org/problems/strong-numbers3315/1
package Afterfeb4;
public class Strongnumber {
public static void main(String[] args) {
int no = 145;
int no2 = no;
int result = reverse(no);
System.out.println(result);
if (result == no2) {
System.out.println("strong number");
} else {
System.out.println("not strong number");
}
}
public static int reverse(int no) {
int reverse = 0;
int sum = 0;
while (no > 0) {
reverse = no % 10;
sum = sum + factoriyal(reverse);// calling another method which performing factorial( factoriyal(reverse);)
no = no / 10;
}
return sum;
}
public static int factoriyal(int reverse) {
int i = 1;
int total = 1;
while (reverse >= i) {
total = total * reverse;
reverse--;
}
return total;
}
}
Output:
145
strong number
perfectnumber
What is a perfect number in Java?
A number whose sum of factors (excluding the number itself) is equal to the number is called a perfect number. In other words, if the sum of positive divisors (excluding the number itself) of a number equals the number itself is called a perfect number. Let's understood it through an example.
Example of a Perfect Number
Let's take the number 496 and heck it is a perfect number or not.
First, we find the factors of 496 i.e. 1, 2, 4, 8, 16, 31, 62, 124, and 248. Let's find the sum of factors (1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 +248 = 496). We observe that the sum of factors is equal to the number itself. Hence, the number 496 is a perfect number. Similarly, we can check other numbers also.
Reference:https://www.javatpoint.com/perfect-number-program-in-java
package Afterfeb4;
public class perfectnum {
public static void main(String[] args) {
int a = 8128;
int b = a;
int result = perfectnumber(a);
System.out.println(result);
if (b == result) {
System.out.println("PERFECT NUMBER");
} else {
System.out.println("NOT PERFECT NUMBER");
}
}
private static int perfectnumber(int a) {
int sum = 0;
int div = 1;
while (div <= a / 2) {
if (a % div == 0) {
sum = sum + div;
}
div++;
}
return sum;
}
}
output:
8128
PERFECT NUMBER
Top comments (0)