Some Java Problems to practice(part 14)

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

135.  Write a program in java to print all prime numbers from 1 to 100.

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

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

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

137. Write a program in java to ask number and find sum of square of odd digits.

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

138. Write a program in java to ask number and find product of even digits.

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

139. Write a program in java TO CHECK WHETHER THE ASK NUMBER IS PERFECT SQUARE NUMBER OR NOT.

import java.util.Scanner;  
     
   class PerfectSquareNumber{  
   public static void main(String[] args) {  
   int num = 0;  
   System.out.println("Enter number");  
   Scanner MyScanner = new Scanner(System.in);  
   num = MyScanner.nextInt();  
   int sq = 0;  
   for (int i = 1; i   <= num; i++) {  
   if (num % i == 0) {  
   sq = sq + i;  
   }  
   }  
   if (sq == num) {  
   System.out.println("Number is perfect square number");  
   } else {  
   System.out.println("Number is not perfect square number");  
   }  
   }  
   }  
     

140. Write a program to input sales amount and rate of commission then calculate commission and return net sales. [ns=sa-c).

import java.util.Scanner;  
     
   class Sale{  
   public static void main(String[] args) {  
   double sales = 0;  
   double rate = 0;  
   double commission = 0;  
   double netSales = 0;  
   System.out.println("Enter sales amount");  
   Scanner MyScanner = new Scanner(System.in);  
   sales = MyScanner.nextDouble();  
   System.out.println("Enter rate of commission");  
   rate = MyScanner.nextDouble();  
   commission = sales * rate / 100;  
   netSales = sales - commission;  
   System.out.println("Commission is " + commission);  
   System.out.println("Net sales is " + netSales);  
   }  
   }  
     

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

class SumOfOddNumber{  
   public static void main(String[] args) {  
   int sum = 0;  
   for (int i = 1; i   <= 100; i++) {  
   if (i % 2 != 0) {  
   System.out.println(i);  
   sum = sum + i;  
   }  
   }  
   System.out.println("Sum of odd numbers is " + sum);  
   }  
   }  
     

142. Write a program in java to enter any three strings and print the longest one.

import java.util.Scanner;  
     
   class LongestString{  
   public static void main(String[] args) {  
   String str1 = "";  
   String str2 = "";  
   String str3 = "";  
   System.out.println("Enter string 1");  
   Scanner MyScanner = new Scanner(System.in);  
   str1 = MyScanner.nextLine();  
   System.out.println("Enter string 2");  
   str2 = MyScanner.nextLine();  
   System.out.println("Enter string 3");  
   str3 = MyScanner.nextLine();  
   if (str1.length() > str2.length()   &;  &; str1.length() > str3.length()) {  
   System.out.println("Longest string is " + str1);  
   } else if (str2.length() > str1.length()   &;  &; str2.length() > str3.length()) {  
   System.out.println("Longest string is " + str2);  
   } else {  
   System.out.println("Longest string is " + str3);  
   }  
   }  
   }  

143. Write a program in java to print all even numbers from 2 to 100.

class EvenNumbers {  
       public static void main(String args[]) {  
           for (int i = 2; i   <101 ; i++) {  
               System.out.print(i+ " ");  
               i++;  
  
           }  
       }  
       }  
  

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

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);  
   }  
   }  
Share This Article
Leave a comment

Leave a Reply

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