Tag: java basic programs

  • Java Program to Calculate the Area of a Square

    Here, you will learn how to calculate the Area of a Square using the Formula (Side* Side). The Program takes the user input for Side, calculates the result, and displays the result on the screen.

    If you do not know about the operators in java, click the link below to learn about operators as this program is using one.


    Java Program to Calculate the Area of a Square

     //Area of a square
      
     import java.util.Scanner;
     
     public class AreaofSquare 
     {
        public static void main (String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            
            System.out.print("Enter Side of Square: ");
            float side = scanner.nextInt();
            
            //Area = side*side
            float area = side*side; 
            System.out.println("Area of Square is: "+area);
        }
     }

    Output:

    Enter Side of Square: 4
    Area of Square is: 16.0


  • Java Program to Calculate the Area of a Rectangle

    Here, you will learn how to calculate the Area of a Rectangle using the Formula (Length * Breadth). The program takes the user input for length and breadth, calculates the result, and displays the result on the screen.

    If you do not know about the operators in java, click the link below to learn about operators as this program is using one.


    Java Program to Calculate the Area of a Rectangle

    import java.util.Scanner;
    
    public class AreaRectangle
    {
      public static void main(String[] args)
      {
        int len, br, result;
        Scanner sc = new Scanner(System.in);
    
        //Taking Inputs from Users
        System.out.print("Enter the length: ");
        len = sc.nextInt();
        System.out.print("Enter the breadth: ");
        br = sc.nextInt();
    
        sc.close();
        result = len * br;
        System.out.println("Area of a rectangle: " + result);
      }
    }

    Output:

    Enter the length: 2
    Enter the breadth: 5
    Area of a rectangle: 10


  • Java Program to Calculate the Area and Circumference of a Circle

    The java program below uses Math.PI property of java that returns approx 3.14159. The program takes the user input for the value of radius and calculates using the formula of Area and Circumference of a circle and displays the output accordingly.


    Java Program to Calculate the Area and Circumference of a Circle

     //Area and circumference of circle
    
     import java.util.Scanner;
    
     public class AreaCircumCircle
     {
        
       public static void main(String args[])
       {
         Scanner sc = new Scanner(System.in);
         System.out.print("Enter the radius: ");
        
         double radius = sc.nextDouble();
        
         //Area = PI*radius*radius
         double area = Math.PI * (radius * radius);
         System.out.println("The area of a circle is: " + area);
        
         //Circumference = 2*PI*radius
         double circumference= Math.PI * 2*radius;
         System.out.println( "The circumference of a circle is:"+circumference) ;
       }
     }

    Output:

    Enter the radius: 7
    The area of a circle is: 153.93804002589985
    The circumference of a circle is:43.982297150257104


  • Java Program to find the Product of Two Numbers

    The example below shows how to take two integers as an input from the user using Scanner and multiply them and display the final result on the screen in Java.

    Before that, if you do not about the operators in Java, click the link below and learn about it.


    Java program to find the Product of Two Numbers:

    //product of two numbers
    
     import java.util.Scanner;
    
     public class ProductOfTwoNumbers 
     {
        public static void main(String[] args) 
        {
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter first number: ");
    
            // reads the input number
            int num1 = scan.nextInt();
            
            System.out.print("Enter second number: ");
            int num2 = scan.nextInt();
    
            // Closing Scanner after the use
            scan.close();
            
            // product of two numbers
            int product = num1*num2;
            
            // Displaying the result
            System.out.println("Product of two numbers is: "+product);
        }
     }

    Output:

     Enter first number: 7
     Enter second number: 7
     Product of two numbers is: 49

  • Java Program to Add Two Numbers

    This Java program adds two integers given in the program and displays the result on the screen.

    To understand the topic, you need to have a basic understanding of two basic topics in Java.


    Java Program for the Sum of two numbers

    //Addition of two numbers
     
     public class AddTwoNumbers 
     {
    
       public static void main(String[] args) 
       {
            
         int num1 = 20, num2 = 30, sum;
         sum = num1 + num2; //adding two numbers
    
         //Display result
         System.out.println("Sum of num1 and num2: "+sum);
       }
    }

    Output:

    Sum of num1 and num2: 50


    Java Program for the Sum of two numbers using Scanner

    The Scanner class is used to get the inputs from the users in Java programming. We need to import a scanner class at the beginning.

    import java.util.Scanner;
    public class AddTwoNumbers
    {
      public static void main(String[] args)
      {
        int num1, num2, sum;
        Scanner sc = new Scanner(System.in);
    
        //Taking Inputs from Users
        System.out.println("Enter First Number: ");
        num1 = sc.nextInt();
        System.out.println("Enter Second Number: ");
        num2 = sc.nextInt();
    
        sc.close();
        sum = num1 + num2;
        System.out.println("Sum of these numbers: " + sum);
      }
    }

    Output:

    Enter First Number:
    10
    Enter Second Number:
    5
    Sum of these numbers: 15