Some Java Programs to practice.(Part 2)

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

16. Write a program to display total surface area of cylinder.

import java.util.Scanner;

class SurfaceAreaOfCylinder{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of cylinder");
       double radius = sc.nextDouble();
       System.out.println("Enter the height of cylinder");
       double height = sc.nextDouble();
       double surfaceArea = 2 * Math.PI * (radius * radius) + 2 * Math.PI * radius * height;
       System.out.println("Surface area of cylinder is " + surfaceArea);
   }


   }

17. Write a program to display total surface area of cuboid / box.

import java.util.Scanner;

class SurfaceAreaOfCuboid{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of cuboid");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of cuboid");
       double breadth = sc.nextDouble();
       System.out.println("Enter the height of cuboid");
       double height = sc.nextDouble();
       double surfaceArea = 2 * (length * breadth + breadth * height + length * height);
       System.out.println("Surface area of cuboid is " + surfaceArea);
   }

   }

18. Write a program to display area and perimeter of rectangle.

import java.util.Scanner;

class PerimeterAndAreaOfRectangle {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of rectangle");
       double length = sc.nextDouble();
       System.out.println("Enter the breadth of rectangle");
       double breadth = sc.nextDouble();
       double perimeter = 2 * (length + breadth);
       double area = length * breadth;
       System.out.println("Perimeter of rectangle is " + perimeter);
       System.out.println("Area of rectangle is " + area);
   }

   }

19. Write a program to display total surface area of sphere.

import java.util.Scanner;

class SurfaceAreaOfSphere {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of sphere");
       double radius = sc.nextDouble();
       double surfaceArea = 4 * Math.PI * (radius * radius);
       System.out.println("Surface area of sphere is " + surfaceArea);
   }


   }

20. Write a program to calculate distance.[S=UT+1/2(AT2)].

import java.util.Scanner;

class Distance {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the speed");
       double speed = sc.nextDouble();
       System.out.println("Enter the time");
       double time = sc.nextDouble();
       System.out.println("Enter the acceleration");
       double acceleration = sc.nextDouble();
       double distance = speed * time + 0.5 * acceleration * (time * time);
       System.out.println("Distance is " + distance);
   }


   }

21. Write a program to input principal, rate, time and display total amount.

import java.util.Scanner;

class TotalAmount {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the principal");
       double principal = sc.nextDouble();
       System.out.println("Enter the rate");
       double rate = sc.nextDouble();
       System.out.println("Enter the time");
       double time = sc.nextDouble();
       double amount = principal * Math.pow(1 + rate / 100, time);
       System.out.println("Amount is " + amount);
   }

   }

22. Write a program to display area of square.

import java.util.Scanner;

class AreaOfSquare {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the length of square");
       double length = sc.nextDouble();
       double area = length * length;
       System.out.println("Area of square is " + area);
   }

   }

23. Write a program to ask n number and print the sum of first n natural numbers.

import java.util.Scanner;

class Sum {
   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;
       for (int i = 1; i <= number; i++) {
           sum = sum + i;
       }
       System.out.println("Sum of first " + number + " natural numbers is " + sum);
   }

   }

24. Write a program to ask n number and print the sum square of first n natural numbers.

import java.util.Scanner;

class Sum {
   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;
       for (int i = 1; i <= number; i++) {
           sum = sum + i * i;
       }
       System.out.println("Sum of square of first " + number + " natural numbers is " + sum);
   }

   }

25. Write a program to display total surface area of hemisphere.

import java.util.Scanner;

class SurfaceAreaOfHemisphere {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the radius of hemisphere");
       double radius = sc.nextDouble();
       double surfaceArea = 4 * Math.PI * (radius * radius);
       System.out.println("Surface area of hemisphere is " + surfaceArea);
   }

   }

Share This Article
Leave a comment

Leave a Reply

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