Skip to content

Commit dba6868

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#157 from aditya-gite-04/master
Happy Number
2 parents 7978220 + 9766bb8 commit dba6868

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

AbundantNumber.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.math.*;
3+
4+
// Function to calculate sum of divisors
5+
class AbundantNumber{
6+
static int getSum(int n)
7+
{
8+
int sum = 0;
9+
10+
// Note that this loop runs till square
11+
// root of n
12+
for (int i=1; i<=(Math.sqrt(n)); i++)
13+
{
14+
if (n%i==0)
15+
{
16+
// If divisors are equal,take only
17+
// one of them
18+
if (n/i == i)
19+
sum = sum + i;
20+
21+
else // Otherwise take both
22+
{
23+
sum = sum + i;
24+
sum = sum + (n / i);
25+
}
26+
}
27+
}
28+
29+
// calculate sum of all proper divisors
30+
// only
31+
sum = sum - n;
32+
return sum;
33+
}
34+
35+
// Function to check Abundant Number
36+
static boolean checkAbundant(int n)
37+
{
38+
// Return true if sum of divisors is
39+
// greater than n.
40+
return (getSum(n) > n);
41+
}
42+
43+
/* Driver program to test above function */
44+
public static void main(String args[])throws
45+
IOException
46+
{
47+
if(checkAbundant(12))
48+
System.out.println("YES");
49+
else
50+
System.out.println("NO");
51+
if(checkAbundant(15))
52+
System.out.println("YES");
53+
else
54+
System.out.println("NO");
55+
}
56+
}
File renamed without changes.

HappyNumber.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class HappyNumber
2+
{
3+
public static int isHappyNumber(int num){
4+
int rem = 0, sum = 0;
5+
6+
while(num > 0){
7+
rem = num%10;
8+
sum = sum + (rem*rem);
9+
num = num/10;
10+
}
11+
return sum;
12+
}
13+
14+
public static void main(String[] args) {
15+
int num = 82;
16+
int result = num;
17+
18+
while(result != 1 && result != 4){
19+
result = isHappyNumber(result);
20+
}
21+
22+
if(result == 1)
23+
System.out.println(num + " is a happy number");
24+
else if(result == 4)
25+
System.out.println(num + " is not a happy number");
26+
}
27+
}

0 commit comments

Comments
 (0)