Advertisement
thotfrnk

area/perimeter while loop.java

Nov 20th, 2022 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import java.util.Scanner; //importing scanner
  2. public class number2 { //program begins
  3.     public static void main(String[] args) {
  4.  
  5.         /*
  6.         *Design a program for finding the perimeter and area of each of a set of rectangles
  7.         *Output the sides, area and perimeter of each rectangle
  8.         *Assume that data consists of two sides, side1 and side2.
  9.         *The last data item contains a side1 of zero.
  10.          */
  11.  
  12.         Scanner input = new Scanner (System.in); //scanner declaration
  13.        
  14.         //variable declaration
  15.  
  16.         int side1, side2, perimeter, area;
  17.        
  18.         //input statements for the length and width of the rectangle
  19.  
  20.         System.out.println("Enter length of rectangle: ");
  21.         side1 = input.nextInt();
  22.  
  23.         System.out.println("Enter width of rectangle: ");
  24.         side2 = input.nextInt();
  25.        
  26.         //while loop being used as a continuous loop to find the area and perimeter of different rectangle and will end when side1 = 0
  27.  
  28.         while (side1 != 0) {
  29.            
  30.             //calculating area and perimeter
  31.  
  32.             perimeter = (side1 + side2) * 2;
  33.  
  34.             area = side1 * side2;
  35.            
  36.             //output statements for length, width, area, and perimeter
  37.  
  38.             System.out.println("Length: " +side1);
  39.             System.out.println("Width: " +side2);
  40.             System.out.println("Perimeter: " +perimeter);
  41.             System.out.println("Area: " +area);
  42.            
  43.             //variables being updated within the loop
  44.  
  45.             System.out.println("Enter length of rectangle: ");
  46.             side1 = input.nextInt();
  47.  
  48.             System.out.println("Enter width of rectangle: ");
  49.             side2 = input.nextInt();
  50.         } //while loop ends
  51.     }
  52. } //program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement