Some Java Problems to practice(part 16)

Some Java Programs to practice

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

156. Write a program in java to generate the series 10,12,15,19…………..14th terms.

class Series {  
    public static void main(String[] args) {  
        int a = 10;  
        int b=2;  
        for (int i = 1; i   < 14; i++) {  
            System.out.println(a);  
            a=a+b;  
           b=b+1;  
            }  
        }  
    } 

156. Write a program in java to ask any string and check whether the first character of a ask string is alphabet number or symbol.

import java.util.Scanner;  
     
   class Alphabet {  
   public static void main(String[] args) {  
   String str = "";  
   System.out.println("Enter string");  
   Scanner MyScanner = new Scanner(System.in);  
   str = MyScanner.nextLine();  
   if (str.charAt(0) >= \'a\'   &;  &; str.charAt(0)   <= \'z\' || str.charAt(0) >= \'A\'   &;  &; str.charAt(0)   <= \'Z\') {  
   System.out.println("First character is alphabet");  
   } else {  
   System.out.println("First character is not alphabet");  
   }  
   }  
   }  

157. Write a program in java to ask any string and reverse it.

import java.util.Scanner;  
     
   class ReverseString {  
   public static void main(String[] args) {  
   String str = "";  
   System.out.println("Enter string");  
   Scanner MyScanner = new Scanner(System.in);  
   str = MyScanner.nextLine();  
   String rev = "";  
   for (int i = str.length() - 1; i >= 0; i--) {  
   rev = rev + str.charAt(i);  
   }  
   System.out.println("Reverse string is " + rev);  
   }  
   }  

158. Write a program in java to enter any 10 numbers and sort in descending order.

class DescendingOrder {  
    public static void main(String[] args) {  
  
        int [] arr = new int [] {7, 3, 5, 6, 2};  
        int temp = 0;  
        System.out.println("Elements of original array: ");  
        for (int i = 0; i   < arr.length; i++) {  
            System.out.print(arr[i] + " ");  
        }  
        for (int i = 0; i   < arr.length; i++) {  
            for (int j = i+1; j   < arr.length; j++) {  
                if(arr[i]   < arr[j]) {  
                    temp = arr[i];  
                    arr[i] = arr[j];  
                    arr[j] = temp;  
                }  
            }  
        }  
  
        System.out.println();  
        System.out.println("Elements of array sorted in descending order: ");  
        for (int i = 0; i   < arr.length; i++) {  
            System.out.print(arr[i] + " ");  
        }  
    }  
}  

159. Write a program in java to ask number and find sum of cube of even digits.

import java.util.Scanner;  
     
   class SumOfCubeOfEvenNumbers {  
   public static void main(String[] args) {  
   int sum = 0;  
   for (int i = 2; i   <= 50; i++) {  
   if (i % 2 == 0) {  
   sum = sum + i * i * i;  
   }  
   }  
   System.out.println("Sum of cube of even numbers is " + sum);  
   }  
     
     
   }  

160. Write a program in java TO ENTER ANY DIGIT AND PRINT ODD DIGITS.

import java.util.Scanner;  
  
class PrintOddDigits {  
   public static void main(String[] args) {  
  
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter Any Number");  
       int n = sc.nextInt();  
       int r;  
       while (n != 0) {  
           r = n % 10;  
           if (r % 2 == 1) {  
               System.out.println(r);  
           }  
           n = n / 10;  
       }  
   }  
}  
  

161. Write a program to enter any three numbers and display the greatest one.

import java.util.Scanner;  
     
   class GreatestNumber {  
   public static void main(String[] args) {  
   int num1 = 0;  
   int num2 = 0;  
   int num3 = 0;  
   System.out.println("Enter first number");  
   Scanner MyScanner = new Scanner(System.in);  
   num1 = MyScanner.nextInt();  
   System.out.println("Enter second number");  
   num2 = MyScanner.nextInt();  
   System.out.println("Enter third number");  
   num3 = MyScanner.nextInt();  
   if (num1 > num2   &;  &; num1 > num3) {  
   System.out.println("Greatest number is " + num1);  
   } else if (num2 > num1   &;  &; num2 > num3) {  
   System.out.println("Greatest number is " + num2);  
   } else {  
   System.out.println("Greatest number is " + num3);  
   }  
   }  
   }  

162. Write a program in java to ask any string and count total no. of vowels consonants and words.

class Word {  
   public static void main(String[] args) {  
   String str = "";  
   int vowels = 0;  
   int consonants = 0;  
   int words = 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\' || str.charAt(i) == \'A\' || str.charAt(i) == \'E\' || str.charAt(i) == \'I\' || str.charAt(i) == \'O\' || str.charAt(i) == \'U\') {  
   vowels++;  
   } else if (str.charAt(i) == \' \') {  
   words++;  
   } else {  
   consonants++;  
   }  
   }  
   System.out.println("No. of vowels is " + vowels);  
   System.out.println("No. of consonants is " + consonants);  
   System.out.println("No. of words is " + words);  
   }  
   }  
     

163. Write a Java program to sum values of an array.

 class SumArray {  
   public static void main(String[] args) {  
       int arr[] = {3,5 , 2, 4, 1, 6, 0, 7,};  
       int sum = 0;  
  
       for (int i : arr)  
           sum += i;  
       System.out.println("The sum is " + sum);  
   }  
}  

164. Write a program in java to generate the series 2,2,4,6,10…………13th term.

class FibonacciSeries{  
   public static void main(String args[])  
   {  
       int n1=2,n2=2,n3,i,count=14;  
       System.out.print(n1+" "+n2);  

       for(i=2;i   <count; ++i)  
       {  
           n3=n1+n2;  
           System.out.print(" "+n3);  
           n1=n2;  
           n2=n3;  
       }  
         

   }}  

165. Write a program in java to generate the following series 13579,1357,135,13,1.

 class SumArray {  
    public static void main(String[] args) {  
        int x = 13579;  
        System.out.print(x+" ");  
while(x>1){  
    x = x / 10;  
   System.out.print(x+" ");  
  
  
}  
    }  
}  
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
Some Java Programs to practice

Some Java Problems to practice(part 15)

Next Post
Some Java Programs to practice

Some Java Problems to practice(part 17)

Related Posts