Java Program to Calculate the Area of Rectangle
Java Programming

Java Program to Calculate the Area of Rectangle

 

Java program to calculate rectangle area using Scanner classes:

import java.util.Scanner;

public class Rarea

{

public static void main(String[] args)

{

Scanner inn = new Scanner(System.in);

double height, width, area;

System.out.println(“Area of Rectangle”);

System.out.print(“Please enter the height: “);

height = inn.nextDouble();

System.out.print(“Please enter the width: “);

width = inn.nextDouble();

area = height * width;

System.out.println(“Area: ” + area);

}

}

In above program, the user enters the width and height of the rectangle. The rectangle area can be found by multiplying the width by the height.

The formula for the area is:

Area = width * height

the output of the above program is:

Calculate the Area of the Rectangle using the Scanner class

Area:600.0  result is shown in floating points because in the program we declare the variable width, height, and area as double. we use double data types. for example, if we have a double value then

Java program to calculate rectangle area without using Scanner classes:

class Rarea1 {

public static void main (String[] args)

{

double length = 22.3;

double width = 24.6;

double area = length*width;

System.out.println(“Area of Rectangle is:”+area);

}

}

the output of the above program is:

Calculate the area of the rectangle without using Scanner class

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment