Skip to content

Commit d58edcb

Browse files
Male and Female Percentage
1 parent 1b97a73 commit d58edcb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Write a program that asks the user for the number of males and the number of females registered in a class.
3+
The program should display the percentage of males and females in the class.
4+
Hint: Suppose there are 8 males and 12 females in a class.
5+
There are 20 students in the class.
6+
The percentage of males can be calculated as 8 / 20 = 0.4, or 40%.
7+
The percentage of females can be calculated as 12 / 20 5 = 0.6, or 60%.
8+
*/
9+
10+
package com.challenges;
11+
12+
import java.util.Scanner;
13+
14+
public class MaleAndFemalePercentages {
15+
public static void main(String [] args) {
16+
17+
// Declare Variables
18+
int maleStudents;
19+
int femaleStudents;
20+
int totalStudents;
21+
double malePercentage;
22+
double femalePercentage;
23+
24+
Scanner scanner = new Scanner(System.in);
25+
26+
System.out.println("Enter the number of male students in the class: ");
27+
maleStudents = scanner.nextInt();
28+
System.out.println("Enter the number of female students in the class: ");
29+
femaleStudents = scanner.nextInt();
30+
31+
totalStudents = maleStudents + femaleStudents;
32+
33+
malePercentage = ((float)maleStudents / totalStudents) * 100;
34+
femalePercentage = ((float)femaleStudents / totalStudents) * 100;
35+
36+
System.out.println("The percentage of male students in the class: " + malePercentage + "%");
37+
System.out.println("The percentage of female students in the class: " + femalePercentage + "%");
38+
}
39+
}

0 commit comments

Comments
 (0)