Some Java Programs to practice.(Part 6)

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

56. Write a program in java to ask three sides of a triangle and determine whether a triangle is equilateral triangle or isoceles.

import java.util.*;

class Triangle{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of first side");
       int length1 = sc.nextInt();
       System.out.println("Enter the length of second side");
       int length2 = sc.nextInt();
       System.out.println("Enter the length of third side");
       int length3 = sc.nextInt();
       if (length1 == length2 && length2 == length3) {
           System.out.println("The triangle is equilateral");
       } else if (length1 == length2 || length2 == length3 || length1 == length3) {
           System.out.println("The triangle is isoceles");
       } else {
           System.out.println("The triangle is scalene");
       }
   }
}

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

import java.util.*;

class GreatestAndSmallestOneUsingArray{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of elements");
       int n = sc.nextInt();
       int[] array = new int[n];
       for (int i = 0; i < n; i++) {
           System.out.println("Enter the number");
           array[i] = sc.nextInt();
       }
       int min = array[0];
       int max = array[0];
       for (int i = 0; i < n; i++) {
           if (array[i] < min) {
               min = array[i];
           }
           if (array[i] > max) {
               max = array[i];
           }
       }
       System.out.println("The minimum number is " + min);
       System.out.println("The maximum number is " + max);
   }
}

58. Write a program in java to generate the following series 1,4,9…………9th term.

class Series {
  public static void main(String[] args) {
      double n;

      for (int i = 1; i <= 5; i++) {
          System.out.print(i*i + " ");
          }
      }
  }

59. Write a program in java to print all Armstrong numbers from 1 to 500.

import java.util.*;

class ArmstrongNumber{
       public static void main(String[] args) {
       for (int i = 1; i <= 500; i++) {
           int sum = 0;
           int n = i;
           while (n > 0) {
               int remainder = n % 10;
               sum = sum + remainder * remainder * remainder;
               n = n / 10;
           }
           if (sum == i) {
               System.out.println(i);
           }
       }
   }

}

60. Write a program in java to ask number and count total number of even digits.

import java.util.*;

class NumberAndCount{
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number");
       int number = sc.nextInt();
       int sum = 0;
       while (number > 0) {
           int remainder = number % 10;
           number = number / 10;
           if (remainder % 2 == 0) {
               sum = sum + 1;
           }
       }
       System.out.println("The total number of even digits is " + sum);
   }
 }

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

class Series {
  public static void main(String[] args) {
      int n=5;

      for (int i = 1; i <= 5; i++) {
          System.out.print(n+ " ");
          n=n*10+5;
          }
      }
  }

62. Write a program in java to find the sum of all odd numbers from 1 to 100.

import java.util.*;

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

}

63. Write a program in java to generate the series 2,22,222………13th term.

class Series {
  public static void main(String[] args) {
      int n=2;

      for (int i = 1; i <= 5; i++) {
          System.out.print(n+ " ");
          n=n*10+2;
          }
      }
  }

64. Write a program in java to print all natural numbers from 1 to 100 in descending order.


class Natural {
    public static void main(String[] args) {
   for (int i = 100; i >= 1; i--) {
       System.out.println(i);
   }
}

    }

65. Write a program in java to enter any ten strings and sort in ascending order.

import java.util.Scanner;

class Ascending {
        public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of elements");
       int n = sc.nextInt();
       String[] array = new String[n];
       for (int i = 0; i < n; i++) {
           System.out.println("Enter the string");
           array[i] = sc.next();
       }
       for (int i = 0; i < n; i++) {
           for (int j = i + 1; j < n; j++) {
               if (array[i].compareTo(array[j]) > 0) {
                   String temp = array[i];
                   array[i] = array[j];
                   array[j] = temp;
               }
           }
       }
       for (int i = 0; i < n; i++) {
           System.out.println(array[i]);
       }
   }
        }
Share This Article
Leave a comment

Leave a Reply

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