Advertisement
thotfrnk

percentage

Apr 28th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1.  
  2. import java.util.Scanner; //scanner import
  3. public class Percentages { //program begins
  4.     public static void main(String[] args) { //main method begins
  5.  
  6.         Scanner input = new Scanner (System.in); //scanner declaration
  7.  
  8.         //variable declaration
  9.         double num1, num2;
  10.  
  11.         //input statements to prompt the user to enter the values
  12.         System.out.println("Please enter the first number: ");
  13.         num1 = input.nextDouble();
  14.  
  15.         System.out.println("Please enter the second number: ");
  16.         num2 = input.nextDouble();
  17.  
  18.         //method call
  19.         computePercent(num1, num2);
  20.         computePercent(num2, num1);
  21.  
  22.     } //main method ends
  23.  
  24.     //method computePercent takes on two double arguments and calculates and displays its percentage
  25.     public static void computePercent (double no1, double no2) {
  26.         double percent; //variable declaration
  27.  
  28.         //to calculate the percentage
  29.         percent = (no1 / no2) * 100;
  30.  
  31.         //output statements for the percentage
  32.         System.out.println(no1 + " is " +percent+ "%");
  33.         System.out.println(" of " +no2);
  34.     } //method computePercent ends
  35. } //program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement