Some Java Problems to practice(part 15)

Codynn
7 Min Read

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

145. Write a program in java to generate the following series 11,3,14,17,31…………18th term.

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

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

   }}  

146. Write a program to input any number and check whether the given no. is divisible by 3 and 7 or not.

 import java.util.Scanner; 
    
   class Divisible { 
   public static void main(String[] args) { 
   Scanner sc = new Scanner(System.in); 
   System.out.println("Enter your number"); 
   int num = sc.nextInt(); 
   if (num % 3 == 0 && num % 7 == 0) { 
   System.out.println("The number is divisible by 3 and 7"); 
   } else { 
   System.out.println("The number is not divisible by 3 and 7"); 
   } 
   } 
   } 

147. Write a program in java to print sum of cube of any three ask numbers.

 import java.util.Scanner;  
     
   class SumOfCube{  
   public static void main(String[] args) {  
   int num1 = 0;  
   int num2 = 0;  
   int num3 = 0;  
   int sum = 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();  
   sum = num1 * num1 * num1 + num2 * num2 * num2 + num3 * num3 * num3;  
   System.out.println("Sum of cube of three numbers is " + sum);  
   }  
   }  

148. Write a program in java to enter any 10 numbers and sort in ascending order.

import java.util.Scanner;  
     
   class Sort {  
   public static void main(String[] args) {  
   int[] arr = new int[10];  
   System.out.println("Enter 10 numbers");  
   Scanner MyScanner = new Scanner(System.in);  
   for (int i = 0; i   < 10; i++) {  
   arr[i] = MyScanner.nextInt();  
   }  
   for (int i = 0; i   < 10; i++) {  
   for (int j = i + 1; j   < 10; j++) {  
   if (arr[i] > arr[j]) {  
   int temp = arr[i];  
   arr[i] = arr[j];  
   arr[j] = temp;  
   }  
   }  
   }  
   System.out.println("Sorted array is ");  
   for (int i = 0; i   < 10; i++) {  
   System.out.println(arr[i]);  
   }  
   }  
   }  

149.  Write a program in java to ask number and check whether the given nocomposite or not.

import java.util.Scanner;  
     
   class Composite {  
   public static void main(String[] args) {  
   int num = 0;  
   System.out.println("Enter number");  
   Scanner MyScanner = new Scanner(System.in);  
   num = MyScanner.nextInt();  
   if (num % 2 == 0) {  
   System.out.println("Number is composite");  
   } else {  
   System.out.println("Number is not composite");  
   }  
   }  
   }  

150. Write a program in java to enter any three strings and print the shortest one.

import java.util.Scanner;  
     
   class ShortestString {  
   public static void main(String[] args) {  
   String str1 = "";  
   String str2 = "";  
   String str3 = "";  
   System.out.println("Enter first string");  
   Scanner MyScanner = new Scanner(System.in);  
   str1 = MyScanner.nextLine();  
   System.out.println("Enter second string");  
   str2 = MyScanner.nextLine();  
   System.out.println("Enter third string");  
   str3 = MyScanner.nextLine();  
   if (str1.length()   < str2.length()   &;  &; str1.length()   < str3.length()) {  
   System.out.println("Shortest string is " + str1);  
   } else if (str2.length()   < str1.length()   &;  &; str2.length()   < str3.length()) {  
   System.out.println("Shortest string is " + str2);  
   } else {  
   System.out.println("Shortest string is " + str3);  
   }  
   }  
   }  

151. Write a program in java to ask number and find product of odd digits.

import java.util.Scanner;  
     
   class ProductOfOddDigit {  
   public static void main(String[] args) {  
   int num = 0;  
   int prod = 1;  
   System.out.println("Enter number");  
   Scanner MyScanner = new Scanner(System.in);  
   num = MyScanner.nextInt();  
   while (num > 0) {  
   int rem = num % 10;  
   if (rem % 2 != 0) {  
   prod = prod * rem;  
   }  
   num = num / 10;  
   }  
   System.out.println("Product of odd digits is " + prod);  
   }  
   }  

152. Write a program in java TO ENTER ANY DIGIT AND PRINT EVEN DIGITS.

 import java.util.Scanner;  
     
   class EvenDigit {  
   public static void main(String[] args) {  
   int num = 0;  
   System.out.println("Enter number");  
   Scanner MyScanner = new Scanner(System.in);  
   num = MyScanner.nextInt();  
   while (num > 0) {  
   int rem = num % 10;  
   if (rem % 2 == 0) {  
   System.out.println(rem);  
   }  
   num = num / 10;  
   }  
   }  
   }  
     

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

import java.util.Scanner;  
class SumofEven {  
   private static Scanner sc;  
   public static void main(String[] args)  
   {  
       int number, i, evenSum = 0;  
       sc = new Scanner(System.in);  
  
       System.out.print(" Please Enter any Number : ");  
       number = sc.nextInt();  
  
       for(i = 1; i   <= number; i++)  
       {  
           if(i % 2 == 0)  
           {  
               evenSum = evenSum + i;  
           }  
       }  
       System.out.println("   The Sum of Even Numbers upto " + number + "  =  " + evenSum);  
   }  
}  

154. Write a program in java to print the sum of the numbers between 3 to 30.

class Sumofthenumber {  
   public static void main(String[] args) {  
       int sum = 0;  
       for (int i = 3; i   < 30; i++) {  
sum = sum+i;  
  
       }  
       System.out.println(sum);  
   }  
}  

155. Write a program in java to generate 7,22,11,34……………19th terms.


class Series {  
    public static void main(String[] args) {  
        int a = 7;  
        for (int i = 1; i   < 19; i++) {  
            System.out.println(a);  
            if (a % 2 == 0) {  
                a = a / 2;  
            } else {  
                a = a * 3 + 1;  
            }  
        }  
    }  
}  
Share This Article
Leave a comment

Leave a Reply

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