Write a program that takes two integers as input and finds their greatest common divisor
import java.util.Scanner;
public class javatechypid {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int n1 = input.nextInt();
System.out.print("Enter the second integer: ");
int n2 = input.nextInt();
int gcd = 1;
for (int i = 1; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
System.out.println("The greatest common divisor of " + n1 + " and " + n2 + " is " + gcd + ".");
}
}
Output:
Enter the first integer: 12
Enter the second integer: 18
The greatest common divisor of 12 and 18 is 6.
Explanation of above java code:
This Java program prompts the user to enter two integers and calculates their greatest common divisor.
The main method prompts the user to enter the first integer and stores it in the variable n1.
The main method then prompts the user to enter the second integer and stores it in the variable n2.
The program initializes the variable gcd to 1, which will be the default value if no common divisor is found.
The program then enters a for loop that starts at 1 and continues until the smaller of the two numbers (n1 and n2) is reached. For each iteration of the loop, the program checks if both n1 and n2 are divisible by i without remainder. If this is true, then i is a common divisor of n1 and n2 and is stored in the variable gcd. The loop continues until it has checked all possible divisors.
The program then outputs the value of gcd along with a message that displays the values of n1 and n2 and the result.
Write a program that takes a string as input and prints it in reverse order
import java.util.Scanner;
public class javatechypid {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
String reverse = "";
for (int i = str.length()-1; i >= 0; i--) {
reverse += str.charAt(i);
}
System.out.println("The reverse of the string is: " + reverse);
}
}
Output:
Enter a string: techypid
The reverse of the string is: dipyhcet
Explanation of above java code:
This Java program takes a string input from the user and then reverses the order of its characters.
A new String variable named “reverse” is declared and initialized to an empty string. This variable will hold the reversed string.
The program starts a for loop that iterates over the characters of the input string “str” in reverse order, using the length of the string minus 1 as the starting index and decrementing by 1 each time until the index reaches 0. Inside the loop, the program appends each character to the “reverse” string using the String concatenation operator “+”.
After the loop completes, the program prints the reversed string using the System.out.println() method.
Write a program that takes an integer array as input and finds the largest and smallest elements in the array
import java.util.Scanner;
public class javatechypid {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = input.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
int largest = arr[0], smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println("The largest element in the array is: " + largest);
System.out.println("The smallest element in the array is: " + smallest);
}
}
Output:
Enter the number of elements in the array: 5
Enter the elements of the array: 7
4
3
7
9
The largest element in the array is: 9
The smallest element in the array is: 3
Explanation of above java code:
This Java program finds the largest and smallest element in an array of integers.
The program starts by importing the Scanner class for taking user input.
The main method starts by creating a Scanner object for taking user input.
The program prompts the user to enter the number of elements in the array.
The user enters the number of elements in the array, which is stored in the variable n.
An array of integers, arr, is created with n elements.
The program prompts the user to enter the elements of the array.
The user enters the elements of the array, which are stored in the array arr.
Two variables, largest and smallest, are initialized with the first element of the array.
A for loop is used to iterate over the remaining elements of the array.
If the current element is greater than the largest element found so far, it is assigned to the largest variable.
If the current element is smaller than the smallest element found so far, it is assigned to the smallest variable.After the loop, the program prints out the largest and smallest elements found in the array.
Write a Java program that calculates the factorial of a given number
import java.util.Scanner;
public class javatechypid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int factorial = 1;
for (int i = 2; i <= num; i++) {
factorial *= i;
}
System.out.println(num + "! = " + factorial);
scanner.close();
}
}
Output:
Enter a number: 5
5! = 120
Explanation of above java code:
This Java program reads an integer input from the user and calculates its factorial using a for loop.
A variable named “factorial” is declared and initialized to 1. This variable will store the factorial of the input number.
The program enters a for loop that will iterate from 2 to the input number “num”.
Inside the loop, the program multiplies the “factorial” variable by the loop index variable “i”. This calculates the factorial by multiplying all the integers between 1 and the input number.
After the loop completes, the program prints the calculated factorial using the System.out.println() method. The output message includes the input number and its factorial.
The program closes the Scanner object using the close() method.
Above Output user inputs 5, the program will calculate 5! (5 factorial) as 5 x 4 x 3 x 2 x 1 = 120 and so output “5! = 120”.
Write a Java program that sorts an array of integers in ascending order using the bubble sort algorithm
import java.util.Arrays;
public class javatechypid {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 12, 1, 6};
System.out.println("Original array: " + Arrays.toString(arr));
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
Output:
Original array: [5, 2, 8, 12, 1, 6]
Sorted array: [1, 2, 5, 6, 8, 12]
Explanation of above java code:
The above Java code sorts an array of integers in ascending order using the bubble sort algorithm.
The program first initializes an integer array arr
with six unsorted integer values. The Arrays.toString(arr)
method is used to print the original array.
The program first initializes an integer array arr
with six unsorted integer values. The Arrays.toString(arr)
method is used to print the original array.
Write a Java program to generates a random password
import java.security.SecureRandom;
public class javatechypid {
private static final String CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;':\",./<>?";
private static final int LENGTH = 12;
public static void main(String[] args) {
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder();
for (int i = 0; i < LENGTH; i++) {
int index = random.nextInt(CHARSET.length());
password.append(CHARSET.charAt(index));
}
System.out.println("Generated password: " + password.toString());
}
}
Output:
Generated password: ;:eK[bO,c@CC
Generated password: sW}$S,AI3#6[
Generated password: BJj2U+R/k><s
Explanation of above java code:
The above Java program generates a random password of length 12 that consists of characters from a predefined set of characters.
The program imports the SecureRandom class from the java.security package.
The program declares a private static final String variable named “CHARSET” that contains a set of characters from which the password will be generated. These characters include lowercase and uppercase letters, numbers, and a set of special characters.
The program declares a private static final int variable named “LENGTH” that specifies the length of the password.
The program creates a SecureRandom object named “random” that will be used to generate random numbers.
The program creates a StringBuilder object named “password” that will be used to build the password one character at a time.
The program uses a for loop to generate each character of the password. The loop runs for a total of “LENGTH” iterations.
Inside the loop, the program generates a random integer between 0 (inclusive) and the length of the “CHARSET” string (exclusive) using the SecureRandom object’s nextInt() method. This integer is used to select a random character from the “CHARSET” string using the charAt() method. The selected character is then appended to the “password” StringBuilder object.
Once the loop has completed, the program prints the generated password using the System.out.println() method.
Therefore, the output of the program will be a randomly generated password of length 12 that consists of characters from the “CHARSET” string. The output will be different every time the program is run due to the use of the SecureRandom class to generate random numbers.