33.1k views
0 votes
Create a program that asks that asks the user for the number of males and females in a class. The program should display a percentage of males and females. For example, there are 5 males, and 15 females. That makes 20 people total. To find the percentage of males you can divide 5 by 20, which makes 0.25, or 25% HINT: USE A VARIABLE for every group of people that you will use in your calculation. Submit your .java source code and a screen capture of your program at runtime, including output.

1 Answer

4 votes

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

int male, female;

Scanner input = new Scanner(System.in);

System.out.print("Male: ");

male = input.nextInt();

System.out.print("Female: ");

female = input.nextInt();

System.out.println("Male%: "+(male*100)/(male+female)+"%");

System.out.println("Female%: "+(female*100)/(male+female)+"%"); }}

Step-by-step explanation:

This declares the number of boys and girls as integers

int male, female;

This prompts the user for number of male

System.out.print("Male: ");

This gets input for number of male

male = input.nextInt();

This prompts the user for number of female

System.out.print("Female: ");

This gets input for number of female

female = input.nextInt();

This calculates and prints the percentage of male students

System.out.println("Male%: "+(male*100)/(male+female)+"%");

This calculates and prints the percentage of female students

System.out.println("Female%: "+(female*100)/(male+female)+"%");

User NM Naufaldo
by
4.5k points