1.
Factorial Number
Calculate a number's factorial by multiplying all integers from 1 up to that number using a loop.
public class Factorial
{
public static void main(String[] args)
{
int number = 5; // Change this to your desired number
long factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
[Link]("Factorial: " + factorial);
}
}
2. Fibonacci Series
Prints the sequence where each number is the sum of the two preceding ones.
public class Fibonacci
{
public static void main(String[] args)
{
int n = 10, a = 0, b = 1; // n is total terms to print
[Link](a + " " + b + " ");
for (int i = 2; i < n; i++)
{
int next = a + b;
[Link](next + " ");
a = b;
b = next;
}
}
}
3. Palindrome Number
Checks if a number reads the same backward as forward (e.g., 121).
public class Palindrome
{
public static void main(String[] args)
{
int num = 121, original = num, reverse = 0;
while (num > 0)
{
int rem = num % 10;
reverse = (reverse * 10) + rem;
num = num / 10;
}
if (original == reverse)
[Link](original + " is Palindrome");
else
[Link](original + " is Not Palindrome");
}
}
4. Prime Number
Checks if a number is divisible only by 1 and itself (e.g., 7).
public class Prime
{
public static void main(String[] args)
{
int num = 29;
boolean isPrime = num > 1; // Numbers less than or equal to 1 are not prime
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
[Link](num + (isPrime ? " is Prime" : " is Not Prime"));
}
}
5. Leap Year
Checks if a year has 366 days based on standard rules.
public class LeapYear
{
public static void main(String[] args)
{
int year = 2024;
// Logical condition: divisible by 400 OR (divisible by 4 AND not by 100)
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
[Link](year + " is a Leap Year");
} else
{
[Link](year + " is Not a Leap Year");
}
}
}
6. Armstrong Number
Checks if the sum of the cubes of its digits equals the number itself (for 3-digit numbers like 153
\(\rightarrow 1^3 + 5^3 + 3^3 = 153\)).
public class Armstrong
{
public static void main(String[] args)
{
int num = 153, original = num, sum = 0;
while (num > 0)
{
int rem = num % 10;
sum = sum + (rem * rem * rem); // Cube of the digit
num = num / 10;
}
if (original == sum)
[Link](original + " is Armstrong");
else
[Link](original + " is Not Armstrong");
}
}