Contents: Click for quick access
206. Write a program in java to ask any string and print only consonants.207. Write a program in java to print first 15 prime numbers.208. Write a program in java to generate the following series ½, 2/3, ¾………15th term.209. Write a program in java to print first 20 palindrome numbers.210. Write a program in java to ask any number and print the factors.211. Write a program in java to ask any word and count total no of vowels and consonants.212. Write a program in java to ask three angles of a triangle and determine whether a triangle is right angled triangle or not.213. Write a program in java to print 3,6,12,24………15th terms.214. Write a program in java to generate the series 1,5,25,125….10th terms.215. Write a program to enter any three numbers and display the middle number.
I’ve included a list of some of the Java programs below. You may also view all of the Java programs on our mobile app. I strongly advise you to use our mobile app for a better experience.
Please click the link to get our app–> https://play.google.com/store/apps/details?id=com.allbachelor.javaapp&hl=en&gl=US
206. Write a program in java to ask any string and print only consonants.
import java.util.Scanner;
class ConsonantString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any string");
String str = sc.nextLine();
String str1 = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == \'a\' || str.charAt(i) == \'e\' || str.charAt(i) == \'i\' || str.charAt(i) == \'o\' || str.charAt(i) == \'u\') {
str1 = str1 + str.charAt(i);
}
}
System.out.println("The consonant string is " + str1);
}
}
207. Write a program in java to print first 15 prime numbers.
class Prime
{
public static void main(String[] args)
{
int ct=0,n=0,i=1,j=1;
while(n <15)
{
j=1;
ct=0;
while(j <=i)
{
if(i%j==0)
ct++;
j++;
}
if(ct==2)
{
System.out.printf("%d ",i);
n++;
}
i++;
}
}
}
208. Write a program in java to generate the following series ½, 2/3, ¾………15th term.
class Series
{
public static void main(String[] args)
{
int a=1,b=2;
for (int i = 0; i < 15; i++) {
System.out.println(a+ "/"+ b);
a=a+1;
b=b+1;
}
}
}
209. Write a program in java to print first 20 palindrome numbers.
class Palindrome {
public static void main(String[] args) {
int counter = 11;
int palindromesFound = 0;
while (palindromesFound < 10) {
if (isPalindrome(Integer.toString(counter))) {
System.out.print(counter);
palindromesFound++;
if (palindromesFound % 10 != 0) {
System.out.print(",");
} else {
System.out.println("");
}
}
counter++;
}
}
public static boolean isPalindrome(String str) {
int n = str.length();
for( int i = 0; i < n/2; i++ )
if (str.charAt(i) != str.charAt(n-i-1)) return false;
return true;
}
}
210. Write a program in java to ask any number and print the factors.
class Factor {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("Enter a number");
int n= scanner.nextInt();
System.out.print("Factors of " + n + " are: ");
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
System.out.print(i + " ");
}
}
}
}
211. Write a program in java to ask any word and count total no of vowels and consonants.
import java.util.Scanner;
class Count{
public static void main(String[] args) {
String str = "";
int vowels = 0;
int consonants = 0;
int words = 0;
int sentences = 0;
System.out.println("Enter string");
Scanner MyScanner = new Scanner(System.in);
str = MyScanner.nextLine();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == \'a\' || str.charAt(i) == \'e\' || str.charAt(i) == \'i\' || str.charAt(i) ==\'o\' || str.charAt(i) == \'u\') {
vowels = vowels + 1;
} else if (str.charAt(i) == \'A\' || str.charAt(i) == \'E\' || str.charAt(i) == \'I\' || str.charAt(i) == \'O\' || str.charAt(i) == \'U\') {
vowels = vowels + 1;
} else if (str.charAt(i) == \' \') {
words = words + 1;
} else if (str.charAt(i) == \'.\' || str.charAt(i) == \'!\' || str.charAt(i) == \'?\') {
sentences = sentences + 1;
} else {
consonants = consonants + 1;
}
}
System.out.println("No. of vowels is " + vowels);
System.out.println("No. of consonants is " + consonants);
System.out.println("No. of words is " + words);
System.out.println("No. of sentences is " + sentences);
}
}
212. Write a program in java to ask three angles of a triangle and determine whether a triangle is right angled triangle or not.
import java.util.Scanner;
class Triangle {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("Enter first angle of triangle");
int n= scanner.nextInt();
System.out.println("Enter second angle of triangle");
int n1 = scanner.nextInt();
System.out.println("Enter third angle of triangle");
int n2 = scanner.nextInt();
if (n==90|n1 ==90|n2==90 ) {
System.out.print("IT IS A RIGHT ANGLED TRIANGLE");
}
else {
System.out.println("It is not a right angled triangle");
}
}
}
213. Write a program in java to print 3,6,12,24………15th terms.
class Series {
public static void main(String[] args) {
int a=3;
for (int i = 1; i <15 ; i++) {
System.out.println(a);
a=a*2;
}
}
}
214. Write a program in java to generate the series 1,5,25,125….10th terms.
import java.util.Scanner;
class Series {
public static void main(String[] args) {
int a=1;
for (int i = 1; i <10 ; i++) {
System.out.println(a);
a=a*5;
}
}
}
215. Write a program to enter any three numbers and display the middle number.
import java.util.Scanner;
class MiddleNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
if (num1 > num2 &; &; num1 > num3) {
if (num2 > num3) {
System.out.println("The middle number is " + num2);
} else {
System.out.println("The middle number is " + num3);
}
} else if (num2 > num1 &; &; num2 > num3) {
if (num1 > num3) {
System.out.println("The middle number is " + num1);
} else {
System.out.println("The middle number is " + num3);
}
} else if (num3 > num1 &; &; num3 > num2) {
if (num1 > num2) {
System.out.println("The middle number is " + num1);
} else {
System.out.println("The middle number is " + num2);
}
}
}