Some Java Problems to practice(part 12)

Codynn
5 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

115. Write a program in java to print the following series 9,7,5,…1.

 import java.util.Scanner; 
    
   class Series { 
   public static void main(String[] args) { 
   int n = 9; 
   while(n!=1){ 
   System.out.print(n+ " "); 
   n = n - 2; 
    
   } 
   System.out.println(1); 
   } 
    
   } 
    
    
    

116. Write a program in java to convert decimal number to hexadecimal number.

import java.util.*; 
    
   class DecimalToHexadecimal{ 
    
   public static void main(String[] args) { 
   int num = 0; 
   System.out.println("Enter number"); 
   Scanner MyScanner = new Scanner(System.in); 
   num = MyScanner.nextInt(); 
   String hex = ""; 
   while (num != 0) { 
   int rem = num % 16; 
   if (rem   < 10) { 
   hex = rem + hex; 
   } else { 
   switch (rem) { 
   case 10: 
   hex = "A" + hex; 
   break; 
   case 11: 
   hex = "B" + hex; 
   break; 
   case 12: 
   hex = "C" + hex; 
   break; 
   case 13: 
   hex = "D" + hex; 
   break; 
   case 14: 
   hex = "E" + hex; 
   break; 
   case 15: 
   hex = "F" + hex; 
   break; 
   } 
   } 
   num = num / 16; 
   } 
   System.out.println("Hexadecimal number is " + hex); 
   } 
    
   } 
    

117. Write a program in java to ask any number and print the prime factors.

import java.util.*; 
    
   class PrimeFactors{ 
    
   public static void main(String[] args) { 
   int num = 0; 
   System.out.println("Enter number"); 
   Scanner MyScanner = new Scanner(System.in); 
   num = MyScanner.nextInt(); 
   for (int i = 2; i   <= num; i++) { 
   if (num % i == 0) { 
   System.out.println(i); 
   } 
   } 
   } 
   } 

118. Write a program in java TO PRINT H.C.F OF A GIVEN ANY TWO NUMBERS.

import java.util.*; 
    
   class HCF{ 
    
   public static void main(String[] args) { 
   int num1 = 0; 
   int num2 = 0; 
   int hcf = 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(); 
   if (num1 > num2) { 
   hcf = num1; 
   } else { 
   hcf = num2; 
   } 
   while (hcf != 0) { 
   if (num1 % hcf == 0  & & num2 % hcf == 0) { 
   break; 
   } 
   hcf--; 
   } 
   System.out.println("HCF is " + hcf); 
   } 
    
   } 

119. Write a program in java to print all odd numbers from 1 to 100.

import java.util.*; 
    
   class OddNumber{ 
    
   public static void main(String[] args) { 
   for (int i = 1; i   <= 100; i++) { 
   if (i % 2 != 0) { 
   System.out.println(i); 
   } 
   } 
   } 
    
   } 
    
    
    
    

120. Write a program in java to generate the series 55555,5555,555,55,5.

class Series 
{ 
   public static void main(String[] args) { 
       int a=5555; 
       for (int i = 0; i   < 4; i++) { 

           System.out.print(a+ " "); 
           a= a/10; 
 
       } 
 
 
   } 
} 

121. Write a program in java to generate the series 1,2,3,5…………9th terms.

122. Write a program in java to print numbers 70,61,52…..7.

class Series{  
   public static void main(String args[])  
   {  
int n=70;  
       System.out.print(n+ " ");  
      while(n!=7){  
          n=n-9;  
          System.out.print(n+" ");  

      }  
   }  }  

123. Write a program in java to generate the series 100,95,90…………………10th term.

class Series{  
   public static void main(String args[])  
   {  
int n=100;  
       System.out.print(n+ " ");  
       for (int i = 0; i  <8 ; i++) {  
          n=n-5;  
          System.out.print(n+" ");  
      }  
         
   }}  

124. Write a program in java TO ENTER ANY DIGIT AND PRINT ITS REVERSED FORM.

  import java.util.*;  
     
   class ReversedForm{  
     
   public static void main(String[] args) {  
   int num = 0;  
   System.out.println("Enter number");  
   Scanner MyScanner = new Scanner(System.in);  
   num = MyScanner.nextInt();  
   int rev = 0;  
   while (num != 0) {  
   int rem = num % 10;  
   rev = rev * 10 + rem;  
   num = num / 10;  
   }  
   System.out.println("Reversed number is " + rev);  
   }  
     
     
   }  
     
     
     
     
Share This Article
Leave a comment

Leave a Reply

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