Java Program
Java Program
Write a Java program that prompts the user for an integer and prints all the prime numbers
upto that integer
import java.util.Scanner;
scanner.close();
scanner.close();
4. Generate random numbers between two given limits using Random class and print messages
according to the range of value generated
import java.util.Random;
import java.util.Scanner;
scanner.close();
// Generate a random number between lowerLimit (inclusive) and upperLimit (inclusive)
Random random = new Random();
int randomNumber = random.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
5. Write a program to do String manipulation using character array and perform the following
operations i) string length ii) Finding a character at a particular position iii) concatenating two
strings
import java.util.Scanner;
scanner.close();
}
return result;
}
}
6. Write a program to perform the following operations using string class. i)String
concatenation ii) Search a substring iii)To extract a substring from given string
import java.util.Scanner;
// i) String concatenation
if (str1.contains(substring)) {
} else {
System.out.println("The substring \"" + substring + "\" is not found in the first string.");
if (startIndex >= 0 && endIndex <= str1.length() && startIndex < endIndex) {
} else {
scanner.close();
7. Write a program to perform the following string operations using StringBuffer Class i)Length
of string ii) Reverse a string iii)Delete the substring from the given string
import java.util.Scanner;
// Input string
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
StringBuffer stringBuffer = new StringBuffer(inputString);
// i) Length of string
int length = stringBuffer.length();
System.out.println("Length of the string: " + length);
scanner.close();
}
}
8. Write a program that implements multi thread application that has three threads. First
thread generates random integer every 1 second and if the value is even second thread
computes the square of the number and prints. If the value is odd third thread computes the
cube of the number and prints.
import java.util.Random;
generator.start();
evenProcessor.start();
oddProcessor.start();
}
}
9.