Some Java Programs to practice.(Part 13)

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

125. Write a program to enter any three numbers and display the smallest one.

import java.util.*;  
     
   class SmallestOne{  
     
   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("Smallest number is " + num1);  
   } else if (num2  < num1 && num2 < num3) {  
   System.out.println("Smallest number is " + num2);  
   } else {  
   System.out.println("Smallest number is " + num3);  
   }  
   }  
     
     
     
   }    

126. Write a program in java to print the sum of square of odd numbers up to 100.

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

126. Write a program in java to print the sum of cube of even numbers between 2 to 50.

import java.util.*;  
     
   class CubeOfEvenNumbers{  
     
   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);  
   }  
     
     
     
   }  
     

127. Write a program to input a mark in a subject of a student and check if the student is pass or not.

import java.util.Scanner;  
     
   class Result {  
   public static void main(String[] args) {  
   int mark = 0;  
   System.out.println("Enter mark");  
   Scanner MyScanner = new Scanner(System.in);  
   mark = MyScanner.nextInt();  
   if (mark >= 40) {  
   System.out.println("Pass");  
   } else {  
   System.out.println("Fail");  
   }  
   }  
     
     
   }  

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

import java.util.Scanner;  
     
   class ProductOfDigit {  
   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;  
   prod = prod * rem;  
   num = num / 10;  
   }  
   System.out.println("Product of digits is " + prod);  
   }  
     
   }  

129. Write a program in java using to print first 13 odd numbers.

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

130. Write a program in java to ask any number and find sum of factors.

import java.util.Scanner;  
     
   class SumOfFactors{  
   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;  
   for (int i = 1; i <= num; i++) {  
   if (num % i == 0) {  
   sum = sum + i;  
   }  
   }  
   System.out.println("Sum of factors is " + sum);  
   }  
   }  

131. Write a program in java to enter any ten strings and sort in descending order.

import java.util.Scanner;  
     
   class Sort{  
   public static void main(String[] args) {  
   String[] str = new String[10];  
   System.out.println("Enter 10 strings");  
   Scanner MyScanner = new Scanner(System.in);  
   for (int i = 0; i < 10; i++) {  
   str[i] = MyScanner.nextLine();  
   }  
   for (int i = 0; i < 10; i++) {  
   for (int j = i + 1; j < 10; j++) {  
   if (str[i].compareTo(str[j]) < 0) {  
   String temp = str[i];  
   str[i] = str[j];  
   str[j] = temp;  
   }  
   }  
   }  
   for (int i = 0; i <10; i++) {  
   System.out.println(str[i]);  
   }  
   }  
   }  
     

132. Write a program in java to ask any string and print only vowels.

import java.util.HashSet;  
import java.util.Scanner;  
import java.util.Set;  
  
  
class Vowels {  
       public static void main(String args[]) {  
           Scanner scanner = new Scanner(System.in);  
  
           System.out.print("Enter an String : ");  
           String str = scanner.next();  
  
           Set<Character> set = new HashSet<Character>();  
           for (int i = 0; i < str.length(); i++) {  
               char char = str.charAt(i);  
               if (isVowel(char)) {  
                   set.add(char);  
               }  
           }  
  
           System.out.println("Vowels are:");  
           for (Character c : set) {  
               System.out.print(" " + c);  
           }  
  
           scanner.close();  
       }  
  
       public static boolean isVowel(char character) {  
  
           if (character == \'a\' || character == \'A\' || character == \'e\' || character == \'E\' ||  
                   character == \'i\' || character == \'I\' || character == \'o\' || character == \'O\' ||  
                   character == \'u\' || character == \'U\') {  
               return true;  
           } else {  
               return false;  
           }  
       }  
  
   }  
  
  
  

133. Write a program in java to ask any number and print the factorial of a given number.

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

134. Write a program in java to print square of all numbers from 1 to 50.

import java.util.Scanner;  
     
   class Square{  
   public static void main(String[] args) {  
   for (int i = 1; i <= 50; i++) {  
   System.out.println(i * i);  
   }  
   }  
     
   }  

     
     
     
Share This Article
Leave a comment

Leave a Reply

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