Monday, July 12, 2021

5 easy java exersizes with solutions.

 11. Write a Java program to print the area and perimeter of a circle.

Test Data:
Radius = 7.5

-->

public class Main {

      public static void main(String[] args) {

          float radius = 7.5f;

          int pie = 22/7;

          float area = pie * radius * radius;

          float perimeter = 2 * pie * radius;

          System.out.println("The area of the circle is = " + area);

          System.out.println("The perimeter of the circle is = " + perimeter);

     }

}


12. Write a Java program that takes three numbers as input to calculate and print the average of the numbers.

-->

import java.util.*;


public class Main {

      public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter the first number");

          float firstNumber = sc.nextFloat();

          System.out.println("Enter the second number");

          float secondNumber = sc.nextFloat();

          System.out.println("Enter the third number");

          float thirdNumber = sc.nextFloat();

          float average = (firstNumber + secondNumber + thirdNumber) / 3;

          System.out.println("The average is : " + average);

     }

}


13. Write a Java program to print the area and perimeter of a rectangle.

Test Data:
Width = 5.5 Height = 8.5

-->

import java.util.*;


public class Main {

      public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter the length of the rectangle");

          float length = sc.nextFloat();

          System.out.println("Enter the breadth of the rectangle");

          float breadth = sc.nextFloat();

          float area = length * breadth;

          float perimeter = 2 * ( length + breadth);

          System.out.println("The area of the rectrangle is : " + area);

          System.out.println("The perimeter of the rectrangle is : " + perimeter);

     }

}


14. Write a Java program to print an American flag on the screen. 

-->

public class Main {

      public static void main(String[] args) {

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println(" * * * * *  ==================================");

          System.out.println("* * * * * * ==================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

          System.out.println("=========================================");

     }

}


15. Write a Java program to swap two variables. 

-->

public class Main {

      public static void main(String[] args) {

          int a , b , c;

          a = 12;

          b = 21;

          System.out.println("variables before swapping are a = "+ a + " and b = " + b);

          c = a;

          a = b;

          b = c;

          System.out.println("variables after swapping are a = "+ a + " and b = " + b);

     }

}


NOTE: 

  • I use float because the user can input decimal value instead of integer value.
  • This is a beginner friendly so i didn't use loops.
  • We can calculate area , perimeter , etc using java.

ANNOUNCEMENT:I'll be posting more five questions and solutions tomorrow so stay tuned. If you have any suggestions tell me it'll help me improve.

1 comment: