Some Java Problems to practice(part 19)

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

186.Write a program in java to print sum of square of any three ask numbers.

import java.util.Scanner;  
     
   class SquareOfNumbers {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your number");  
   int num1 = sc.nextInt();  
   System.out.println("Enter your number");  
   int num2 = sc.nextInt();  
   System.out.println("Enter your number");  
   int num3 = sc.nextInt();  
   int sum = (num1 * num1) + (num2 * num2) + (num3 * num3);  
   System.out.println("The sum of square of three numbers is " + sum);  
   }  
   }  

187. Write a program in java to ask any string and check whether the given string is palindrome or not.

import java.util.Scanner;  
     
   class Palindrome {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your string");  
   String str = sc.nextLine();  
   String rev = "";  
   for (int i = str.length() - 1; i >= 0; i--) {  
   rev = rev + str.charAt(i);  
   }  
   if (str.equals(rev)) {  
   System.out.println("The string is palindrome");  
   } else {  
   System.out.println("The string is not palindrome");  
   }  
   }  
   }  

188. Write a program to input a year and display whether that year is a leap year or not.

 import java.util.Scanner;  
     
   class LeapYear {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your year");  
   int year = sc.nextInt();  
   if (year % 4 == 0) {  
   if (year % 100 == 0) {  
   if (year % 400 == 0) {  
   System.out.println("The year is leap year");  
   } else {  
   System.out.println("The year is not leap year");  
   }  
   } else {  
   System.out.println("The year is leap year");  
   }  
   } else {  
   System.out.println("The year is not leap year");  
   }  
   }  
   }  

189. Write a program in java to print all natural numbers from 1 to 100 also print its sum.

import java.util.Scanner;  
     
   class Sum {  
   public static void main(String[] args) {  
   int sum = 0;  
   for (int i = 1; i   <= 100; i++) {  
   System.out.println(i);  
   sum = sum + i;  
   }  
   System.out.println("The sum of all natural numbers is " + sum);  
   }  
   }  

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

import java.util.Scanner;  
     
   class HexaDecimalToDecimal {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your hexadecimal number");  
   String hex = sc.nextLine();  
   int dec = 0;  
   for (int i = 0; i   < hex.length(); i++) {  
   char ch = hex.charAt(i);  
   if (ch == \'A\') {  
   dec = dec + 10 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else if (ch == \'B\') {  
   dec = dec + 11 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else if (ch == \'C\') {  
   dec = dec + 12 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else if (ch == \'D\') {  
   dec = dec + 13 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else if (ch == \'E\') {  
   dec = dec + 14 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else if (ch == \'F\') {  
   dec = dec + 15 * (int) Math.pow(16, hex.length() - 1 - i);  
   } else {  
   dec = dec + (int) ch * (int) Math.pow(16, hex.length() - 1 - i);  
   }  
   }  
   System.out.println("The decimal number is " + dec);  
   }  
   }  

191. Write a program to input the age of a person and find out whether the person is eligible to drive or not.

class Drive {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your age");  
   int age = sc.nextInt();  
   if (age >= 18) {  
   System.out.println("You are eligible to drive");  
   } else {  
   System.out.println("You are not eligible to drive");  
   }  
   }  
   }  

192. Write a program to enter any 20 numbers and display the greatest one using array.

import java.util.Scanner;  
     
   class GreatestNumber {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your 20 numbers");  
   int[] num = new int[20];  
   for (int i = 0; i   < 20; i++) {  
   num[i] = sc.nextInt();  
   }  
   int max = num[0];  
   for (int i = 0; i   < 20; i++) {  
   if (num[i] > max) {  
   max = num[i];  
   }  
   }  
   System.out.println("The greatest number is " + max);  
   }  
   }  

193. Write a program in java TO PRINT L.C.M OF A GIVEN ANY TWO NUMBERS.

import java.util.Scanner;  
     
   class LCM {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter two numbers");  
   int num1 = sc.nextInt();  
   int num2 = sc.nextInt();  
   int lcm = 0;  
   for (int i = 1; i   <= num1   &;  &; i   <= num2; i++) {  
   if (num1 % i == 0   &;  &; num2 % i == 0) {  
   lcm = i;  
   }  
   }  
   System.out.println("The least common multiple is " + lcm);  
   }  
   }  

194. Write a program in java to enter any 10 numbers and find the sum of even numbers.

class Series  
{  
   public static void main(String args[]){  
   int total = 0, sum=0,evenNumber=2,n=10;  
   while(total !=n) {  
       total++;  
       sum+=evenNumber;  
       evenNumber +=2;  
   }  
       System.out.println("sum of first 10 even number is : "+ sum);  
}  
}  

195. Write a program to divide a number by another number and find the quotient and remainder.

import java.util.Scanner;  
     
   class QuotientAndReminder {  
   public static void main(String[] args) {  
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter your two numbers");  
   int num1 = sc.nextInt();  
   int num2 = sc.nextInt();  
   int quotient = num1 / num2;  
   int remainder = num1 % num2;  
   System.out.println("The quotient is " + quotient);  
   System.out.println("The remainder is " + remainder);  
   }  
   }  
Share This Article
Leave a comment

Leave a Reply

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