計算百分比的 Java 程式
百分比是指百分之一(百),即 100 分之幾的比例。百分號的符號為 %。我們通常計算獲得的分數、投資回報率等的百分比。百分比也可以超過 100%。
例如,假設我們有一個總數和一部分。因此,我們可以說一部分是總數的多少百分比,並應按如下計算 −
percentage = ( part / total ) × 100
演算法
以下是計算百分比的演算法 Java
1. Collect values for part and total
2. Apply formula { percentage = ( part / total ) × 100 }
3. Display percentage示例
import java.util.Scanner;
public class Percentage {
public static void main(String args[]){
float percentage;
float total_marks;
float scored;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks ::");
scored = sc.nextFloat();
System.out.println("Enter total marks ::");
total_marks = sc.nextFloat();
percentage = (float)((scored / total_marks) * 100);
System.out.println("Percentage ::"+ percentage);
}
}輸出
Enter your marks :: 500 Enter total marks :: 600 Percentage ::83.33333
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP