计算百分比的 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