Solution To Practice Problems Itec 1620
Solution To Practice Problems Itec 1620
Take three numbers from the user import java.util.Scanner; In this program, we have declared three variables `num1`, `num2`, and `num3` to store the three numbers
as input and print the greatest entered by the user. We have also declared a variable `greatest` to store the greatest number.
number. Write a complete Java public class GreatestNumber {
program public static void main(String[] args) { Then we have used the `Scanner` class to read the user input for the three numbers.
Scanner input = new Scanner(System.in);
Next, we have used conditional statements to compare the three numbers and store the greatest number in the
System.out.print("Enter the first number: "); `greatest` variable.
int num1 = input.nextInt();
Finally, we have printed the greatest number using the `System.out.println()` method.
System.out.print("Enter the second number: ");
int num2 = input.nextInt(); When we run this program, it prompts the user to enter three numbers, and then prints the greatest number
among them.
System.out.print("Enter the third number: ");
int num3 = input.nextInt();
if (areStringsSimilar(string1, string2)) {
System.out.println("similar");
} else {
System.out.println("dissimilar");
}
}
System.out.println("Enter 10 numbers:");
if (numbers[i] % 2 == 0) {
sum += numbers[i];
}
}
// check if each element in both arrays is equal and in the same order
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}
Write a complete Java program that import java.util.Scanner;
will take an N*N matrix as input and 1. Take the number N as input.
output its Transpose. If N = 3, then public class MatrixTranspose { 2. Then take the N*N matrix as input.
following contains the transpose public static void main(String[] args) { 3. In the transpose method, create a new matrix with the same number of rows and columns as the input
matrix for the 3*3 matrix. Scanner scanner = new Scanner(System.in); matrix.
4. Use a nested loop to iterate through the rows and columns of the input matrix.
// Taking input of the size of the matrix 5. Set the value of each element in the new matrix using the corresponding element in the input matrix but with
System.out.print("Enter the size of the square matrix: "); the row and column indices swapped.
int n = scanner.nextInt(); 6. In the main method, create an instance of the class and call the transpose method with the input matrix as
argument.
int[][] matrix = new int[n][n]; 7. Print the transpose matrix to the console.
}
Write a complete Java program that import java.util.Scanner;
will take an N*N matrix as input and
sort all the values in each column. public class MatrixSorter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Sort columns
for (int j = 0; j < n; j++) {
for (int i = 0; i < n - 1; i++) {
for (int k = i + 1; k < n; k++) {
if (matrix[i][j] > matrix[k][j]) {
int temp = matrix[i][j];
matrix[i][j] = matrix[k][j];
matrix[k][j] = temp;
}
}
}
}
}
}
Write a complete Java program that import java.util.Scanner;
takes a String as input and find if
the text “final exam” is in that string public class Main {
or not. If found, print “Found”; public static void main(String[] args) {
otherwise, print “Not Found”. Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input.contains("final exam")) {
System.out.println("Found");
} else {
System.out.println("Not Found");
}
}
}
Write a complete Java program that import java.util.Scanner; In this program, we first define the input string (in this case, "hello world"). We then convert the string to a
takes a String as input and sort the character array using the toCharArray() method.
string in ascending order. Then print public class AsciiSorter {
the ascii values of each character in public static void main(String[] args) { To sort the character array, we use the bubble sort algorithm. We loop through each element of the array and
the sorted string. Scanner scanner = new Scanner(System.in); compare it with the element next to it. If the current element is greater than the next element, we swap them.
System.out.print("Enter a string: "); We repeat this process until the array is sorted.
String input = scanner.nextLine();
char[] chars = input.toCharArray(); Finally, we loop through each character in the sorted array using a for-each loop, and print the ASCII value of
for (int i = 0; i < chars.length - 1; i++) { each character using the (int) c syntax to cast the character to an integer.
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] > chars[j]) { You can replace the input string with any string of your choice to test the program.
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
for (char c : chars) {
System.out.print((int) c);
}
}
}
Write a complete Java program that import java.util.Scanner; In this program, we first ask the user to input an integer using Scanner class. Then, we create a boolean array
takes an integer as input and print with 10 elements to keep track of which digits are present and which are absent.
which digits from 0 to 9 are absent public class AbsentDigits {
in that integer. public static void main(String[] args) { We loop through each digit in the integer using modulo operator and divide by 10. We mark the corresponding
Scanner scan = new Scanner(System.in); index as true in the boolean array to indicate the presence of that digit.
System.out.print("Enter an integer: ");
int num = scan.nextInt(); Finally, we loop through the boolean array and print the indices where the value is false, indicating the absence
of that digit in the integer.
boolean[] digits = new boolean[10]; // to keep track of the presence of digits
System.out.print("Enter a positive integer: "); This program takes into account that the input integer may not have a fixed number of digits, so it is able to
int num = input.nextInt(); calculate the product of digits for any positive integer.
int product = 1;
while(num > 0) {
int digit = num % 10;
product *= digit;
num /= 10;
}
if(isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
Write a complete Java program that import java.util.Scanner;
calculates the factorial of a given
integer. public class FactorialCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num = input.nextInt();
if (num < 0) {
System.out.println("Error: Only positive integers can be used to calculate factorial.
Exiting program...");
System.exit(0);
}
int factorial = 1;
String longestWord = ""; Finally, we print out the longest word we found using `System.out.println()`.
int comparisonResult;
if (number > 50) {
comparisonResult = 1;
} else {
comparisonResult = 0;
}
switch (comparisonResult) {
case 1:
System.out.println("Greater");
break;
Write a complete Java Program that case 0:
will ask for an integer-type input System.out.println("Smaller");
(you can assume that the break;
given input is either positive or default:
negative number) and check // This case is not expected to be reached
whether the given input is greater System.out.println("Unexpected error");
than break;
50 or not. If greater than 50, then }
print “Greater”. Otherwise, print
“Smaller”. You must solve this scanner.close();
problem using the Switch-Case }
statement. }
import java.util.Scanner;
switch (number % 2) {
case 0:
Write a complete Java Program that System.out.println("Even");
will ask for an integer-type input and break;
check whether the given case 1:
input is even or odd. If even, print System.out.println("Odd");
“Even”. Otherwise, print “Odd”. You break;
cannot use If-Else statements for // The default case is not necessary here as the remainder will always be 0 or 1
this problem. Only the switch }
statement is }
allowed. }