1.
Sphenic number:
public class SphenicNumber {
public static void main(String[] args) {
int n = 30; // change this number to test
int count = 0;
int temp = n;
boolean isSphenic = true;
for (int i = 2; i <= temp && count <= 3; i++) {
if (temp % i == 0) {
int exponent = 0;
while (temp % i == 0) {
exponent++;
temp /= i;
}
if (exponent > 1) {
isSphenic = false;
break;
}
count++;
}
}
if (temp > 1) {
count++;
}
if (count == 3 && isSphenic) {
System.out.println(n + " is a Sphenic Number.");
} else {
System.out.println(n + " is not a Sphenic Number.");
}
}
}
2. Neon number:
public class NeonNumber{
public static void main(String[] args) {
int n = 9;
int square = n * n;
int sum = 0;
while (square > 0) {
int digit = square % 10;
sum = sum + digit;
square = square / 10;
}
// Check and print result
if (sum == n) {
System.out.println(n + " is a Neon Number.");
} else {
System.out.println(n + " is not a Neon Number.");
}
}
}
3.Perfect number:
public class PerfectNumber {
public static void main(String[] args) {
int n = 28; // Change this number to test
int sum = 0;
// Find divisors from 1 to n-1
for (int i = 1; i <= n / 2; i++) { // proper divisors are <= n/2
if (n % i == 0) {
sum += i;
}
}
// Check if sum of divisors equals the number
if (sum == n && n > 0) {
System.out.println(n + " is a Perfect Number.");
} else {
System.out.println(n + " is not a Perfect Number.");
}
}
}
4.Patterns :
i)1234
1234
1234
1234
public class Patterns {
public static void main(String[] args) {
for (int i = 1; i<= 3; i++) {
for (int j = 1; j <= 4; j++) {
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int j = 1; j<= 4; j++) {
System.out.print(j);
}
}
}
*
**
***
****
*****
public class Patterns {
public static void main(String[] args) {
int n=5;
for (int i = 1; i<=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
A
BB
CCC
DDDD
EEEEE
public class Patterns {
public static void main(String[] args) {
int n=5;
char abc ='A';
for (int i = 1; i<=n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(abc);
}
System.out.println();
abc++;
}
}
}
A
AB
ABC
ABCD
ABCDE
public class Patterns {
public static void main(String[] args) {
int n = 5; // number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char) ('A' + j - 1));
}
System.out.println();
}
}
}
*
***
*****
*******
public class Patterns {
public static void main(String[] args) {
int n = 5;
int stars = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= stars; j++) {
System.out.print("*");
}
System.out.println();
stars += 2;
}
}
}
*
***
*****
*******
public class Patterns {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i <n; i++) {
for (int j = 0; j < n - i-1 ; j++) {
System.out.print(" ");
}
for (int k = 0; k < 2 * i+1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
1
222
33333
4444444
555555555
public class Patterns {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= n - i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i-1; k++) {
System.out.print(i);
}
System.out.println();
}
}
}
1
123
12345
1234567
public class Patterns {
public static void main(String[] args) {
int n = 4;
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= n - i ; j++) {
System.out.print(" ");
}
for (int num = 1; num <= 2 * i-1; num++) {
System.out.print(num);
}
System.out.println();
}
}
}
1
2 3 4
5 6 7 8 9
public class Patterns {
public static void main(String[] args) {
int n = 3;
int m=1;
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= n - i ; j++) {
System.out.print(" ");
}
for (int num = 1; num <= 2 * i-1; num++) {
System.out.print(" ");
System.out.print(m);
m++;
}
System.out.println();
}
}
}
A
A B C
A B C D E
public class Patterns {
public static void main(String[] args) {
int n = 3;
char m='A';
for (int i = 1; i <=n; i++) {
for (int j = 1; j <= n - i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i-1; k++) {
System.out.print(" ");
System.out.print(m);
m++;
}
System.out.println();
}
}
}
4 4 4 4
4 3 3 3
4 3 2 2
4 3 2 1
4 3 2 2
4 3 3 3
public class Patterns {
public static void main(String[] args) {
int size = 4;
)
for (int i = size; i >= 1; i--) {
for (int j = size; j >= 1; j--) {
System.out.print(Math.max(i, j) + " ");
}
System.out.println();
}
)
for (int i = 2; i <= size; i++) {
for (int j = size; j >= 1; j--) {
System.out.print(Math.max(i, j) + " ");
}
System.out.println();
}
}
}
Revresed String
import java.util.Scanner;
public class Patterns {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = sc.nextLine();
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed String: " + reversed);
}
}
Second largest elements in an array of integers :
import java.util.Scanner;
public class Patterns {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > firstLargest) {
secondLargest = firstLargest;
firstLargest = num;
} else if (num > secondLargest && num != firstLargest) {
secondLargest = num;
}
}
if (secondLargest == Integer.MIN_VALUE) {
System.out.println("No second largest element (all numbers may be
same)");
} else {
System.out.println("Second largest element: " + secondLargest);
}
sc.close();
}
}
Removing Duplicates :
import java.util.Scanner;
public class Patterns {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Take string input
System.out.print("Enter a string: ");
String str = sc.nextLine();
String result = ""; // to store string without duplicates
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Add character only if it is not already in result
if (result.indexOf(ch) == -1) {
result += ch;
}
}
System.out.println("String after removing duplicates: " + result);
sc.close();
}
}