Some Java Programs to practice.(Part 11)

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

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

import java.util.*; 
    
   class SumOfEvenNumber{ 
    
   public static void main(String[] args) { 
   int sum = 0; 
   for (int i = 2; i   <= 50; i++) { 
   if (i % 2 == 0) { 
   sum = sum + i; 
   } 
   } 
   System.out.println("Sum is " + sum); 
   } 
    
   } 
    

107. Write a program in java using to print product of all numbers from 4 to 8.

import java.util.*; 
    
   class Product{ 
    
   public static void main(String[] args) { 
   int product = 1; 
   for (int i = 4; i   <= 8; i++) { 
   product = product * i; 
   } 
   System.out.println("Product is " + product); 
   } 
    
   } 

108. Write a program in java to print the series 2,8,18,32…..upto 10th term.

  import java.util.Scanner; 
    
   class Series{ 
   public static void main(String[] args) { 
   for (int i = 1; i   <= 10; i++) { 
   //PRINT i ^ 2 * 2; 
   int x= (int) Math.round(Math.pow(i,2)*2); 
   System.out.print(x + " "); 
   } 
   } 
   } 
    
    

109. Write a program in java to generate the following series 5,54,543,5432,54321.

import java.util.Scanner; 
    
   class Series { 
    
    
   public static void main(String[] args) { 
    
   for (int i = 5; i >= 1; i--) { 
   for (int j = 1; j   < i; j++) 
   System.out.print(" "); 
   for (int k = 5; k >= i; k--) 
   System.out.print(k); 
   System.out.print(" "); 
   } 
   } 
    
   } 
    

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

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

111. Write a program in java to ask any string and count total no. of words.

import java.util.*; 
    
   class CountTotalNumberOfWords{ 
    
   public static void main(String[] args) { 
   String str = ""; 
   int count = 0; 
   System.out.println("Enter string"); 
   Scanner MyScanner = new Scanner(System.in); 
   str = MyScanner.nextLine(); 
   String[] strArr = str.split(" "); 
   count = strArr.length; 
   System.out.println("Total number of words is " + count); 
   } 
    
   } 
    
    

112. Write a program in java to ask any string and count total no. of vowels.

import java.util.*; 
    
   class CountTotalNumberOfVowels{ 
    
   public static void main(String[] args) { 
   String str = ""; 
   int count = 0; 
   System.out.println("Enter string"); 
   Scanner MyScanner = new Scanner(System.in); 
   str = MyScanner.nextLine(); 
   String[] strArr = str.split(""); 
   for (int i = 0; i   < strArr.length; i++) { 
   if (strArr[i].equals("a") || strArr[i].equals("e") || strArr[i].equals("i") || strArr[i].equals("o") || strArr[i].equals("u")) { 
   count++; 
   } 
   } 
   System.out.println("Total number of vowels is " + count); 
   } 
    
   } 
    

113. Write a program in java TO PRINT H.C.F AND L.C.M OF ANY TWO Given NUMBERS by user.

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

114. Write a program in java to generate the series 3,10,5,16………………11th term.

import java.util.Scanner; 
    
   class Series { 
   public static void main(String[] args) { 
   int n = 6; 
   while (n != 1) { 
   System.out.print(n + " "); 
   if ((n  & 1) == 1) 
   n = 3 * n + 1; 
   else 
   n = n / 2; 
   } 
   System.out.print(n); 
   } 
    
   } 
Share This Article
Leave a comment

Leave a Reply

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