# Average returns 0

Printable View

• 12-03-2010, 06:29 PM
jehduncs
Average returns 0[solved]
Im trying to get the average of number of marks divded by the total marks such as 3 marks of 10, 10 and 10 and have the total marks 50 so 30/50

Code:

``` import java.util.Scanner; public class MarksAverage {         public static void main(String[] args)         {                 Scanner input = new Scanner(System.in);                                 System.out.print("How many scores will be entered? ");                 int numTests = input.nextInt();                                                 int [] marks = new int [numTests];                                 for(int counter =0; counter < marks.length; counter++)                 {                         System.out.print("Enter mark # "+ (counter+1) + ":");                         marks[counter] = input.nextInt();                 }                 int runningTotal = 0;                                 for(int counter = 0; counter < marks.length; counter++)                 {                                                runningTotal += marks[counter];                         //calculate average after the loop                 }//end for                                 //calculate average                                 System.out.print("What is the worth of all marks");                 int marksWorth = input.nextInt();                                 int average = (int) (runningTotal / marksWorth) * 100;                                                 System.out.println(average);         } }```
• 12-03-2010, 06:35 PM
Fubarable
You seem to be doing int division -- an int divided by an int -- which will always result in an int. So 3/6 will give 0, and 5/3 will give one. The solution is to use double by casting one of the ints to double:

Code:

`double average = (double) myIntA / myIntB;`
• 12-03-2010, 07:29 PM
jehduncs
thank you, works perfectly:)