Java Number Programs Collection
Prime Numbers Program
public class PrimeNumbers {
public static void main(String[] args) {
for(int i = 2; i <= 100; i++) {
int count = 0;
for(int j = 1; j <= i; j++) {
if(i % j == 0) {
count++;
}
}
if(count == 2) {
[Link](i);
}
}
}
}
Fibonacci Program
public class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1, c;
[Link](a);
[Link](b);
for(int i = 1; i <= 10; i++) {
c = a + b;
[Link](c);
a = b;
b = c;
}
}
}
Armstrong Number Program
public class Armstrong {
public static void main(String[] args) {
int num = 153;
int temp = num;
int sum = 0;
int r;
while(num > 0) {
r = num % 10;
sum = sum + (r*r*r);
num = num / 10;
}
if(sum == temp)
[Link]("Armstrong Number");
else
[Link]("Not Armstrong");
}
}
Palindrome Program
public class Palindrome {
public static void main(String[] args) {
int num = 121;
int temp = num;
int rev = 0;
int r;
while(num > 0) {
r = num % 10;
rev = rev * 10 + r;
num = num / 10;
}
if(rev == temp)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
(Other programs included similarly: Strong, Spy, Neon, Automorphic, Menu driven, Peterson, Duck, Sunny,