Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
package Grade;
import java.util.*;

public class Grade {

   
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
          
        System.out.println("Enter the first number: ");
  int M1 = input.nextInt();

        System.out.println("Enter the second number :");
           int M2 = input.nextInt();
               
  System.out.println("Enter the third number: ");
           int M3 = input.nextInt();
             
           int total = M1+M2+M3;
           
             System.out.println("Average of three numbers is: " + 
                       total / 60)*100) ;


What I have tried:

Enter the first number:
15
Enter the second number :
20
Enter the third number:
10
Average of three numbers is: 0
BUILD SUCCESSFUL (total time: 19 seconds)
Posted
Updated 13-Sep-22 20:12pm
v2

You are using the correct computation with a wrong datatypes. The result must be a double and you have to force the use of floating point division in the expression (in your expression, the integer division is used). Replace
Quote:
System.out.println("Average of three numbers is: " +
total / 60)*100) ;
with
Java
double result = total*100.0/60; // same of 'result = total*5.0/3;'

 System.out.println("Average of three numbers is: " +
             result) ;
 
Share this answer
 
Comments
Patrice T 14-Sep-22 2:52am    
+5
Member 15766438 14-Sep-22 16:03pm    
Thank you
Your lack of a question or problem description leaves one to assume you're trying to calculate an average and not coming up with the correct answer.

You've got this in your code:
Java
int total = M1+M2+M3;
           
             System.out.println("Average of three numbers is: " + 
                       total / 60)*100) ;

So how do you calculate an average? You add up the values you have, in your case 15, 20, and 10, then divide that total by the number of values you added together, 3.

Where did you get the "divided by 60, times 100" garbage from?
 
Share this answer
 
Comments
CPallini 14-Sep-22 2:15am    
The OP terminology is not exact. But the computation is logically correct (technically flawed), if you consider the requirements.
Dave Kreskowiak 14-Sep-22 8:04am    
Wow. That's what 3 hours of sleep gets me. I should have gone back to read the description for the eleventh time, and maybe I would have gotten it.
:doh:
Member 15766438 14-Sep-22 16:42pm    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900