Prime Numbers
Prime Numbers
import java.util.Scanner;
// Display results
System.out.println("Number of characters (excluding spaces): " + charCount);
System.out.println("Number of words: " + wordCount);
System.out.println("Number of lines: " + lineCount);
scanner.close();
}
}
Output:
Enter your text (press Enter twice to finish):
hi iam hari
how are you?
iam fine!!
import java.util.Scanner;
switch (choice)
{
case "1": // Find String Length
System.out.print("Enter the string: ");
char[] str1 = sc.nextLine().toCharArray();
System.out.println("Length: " + str1.length);
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (!choice.equals("4"));
sc.close();
}
}
Output:
import java.util.Scanner;
switch (choice) {
case "1": // String Concatenation
System.out.print("Enter first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter second string: ");
String str2 = scanner.nextLine();
String concatenated = str1.concat(str2);
System.out.println("Concatenated String: " + concatenated);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (!choice.equals("7"));
scanner.close();
}
switch (choice)
{
case "1": // String Concatenation
System.out.print("Enter first string: ");
String str1 = sc.nextLine();
System.out.print("Enter second string: ");
String str2 = sc.nextLine();
StringBuffer sbConcat = new StringBuffer(str1);
sbConcat.append(str2); // Append second string
System.out.println("Concatenated String: " + sbConcat.toString());
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (!choice.equals("7"));
sc.close();
}
}
Matrix Multiplication
import java.util.Scanner;
// Initialize matrices
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
scanner.close();
}
}
Output: