Array of Sum
I have been working of this program for quite a while now and can't seem to find out how to calculate the percentage of a number.
The output should read2 33.3333% of the sum but it comes out like this:2 3.0% of the sum.
Can you help me figure out where I went wrong.
This is the HW instructions: I am to write a program that reads in a list of int values, one perline and outputs their sum as well as all the numbers read in, with each number annotated to say what percentage it contributes to the sum. the program will ask the user how many integers there will be and I am to create an array of that length, and then fill the array with the integers input.
I got this far but can't seem to get my sum in percentage form.
import java.util.*;
publicclass ArrayOfSum
{
/** Reads a list of int values, one perline, and outputs their sum as well as all the numbers read in.
*/
publicstaticvoid main(String[] args)
{
int sum;
double percent;
Scanner keyboard =new Scanner(System.in);
System.out.println("How many numbers will you enter here:");
int index = keyboard.nextInt();
int[] value =newint [index];
System.out.println("Enter your numbers, one at a time:");
sum = 0;
for (index=0; index < value.length; index++)
{
value[index] = keyboard.nextInt();
sum = sum + value[index];//This is where all numbers are added up
}
System.out.println("The total sum is " + sum);
System.out.println("The numbers are: ");
for (index = 0; index < value.length; index++)
{
percent = 100 *(sum/value[index]);// this is where I am having my problem. I need to calculate percentage here.
//percent = Math.round(value[index]);// Creating a rounding of the total sum
System.out.println(value[index] +" " + percent+"% of the sum.");
}
System.out.println("Guess what? I am done with this weeks homework.");
}
}

