There is no error in code but its still giving 0 percentage in result

Joined
Jan 28, 2024
Messages
1
Reaction score
0
import java.util.Scanner;
public class main1 {
public static void main(String[] args)
{


Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of student : ");
String n = sc.nextLine();
System.out.println("Enter the total marks to be scored : ");
int total = sc.nextInt();
System.out.println("enter the marks in maths : ");
int m = sc.nextInt();
System.out.println("enter the marks in english : ");
int e = sc.nextInt();
System.out.println("enter the marks in chemistry : ");
int c = sc.nextInt();
System.out.println("enter the marks in computer science : ");
int s = sc.nextInt();
System.out.println("enter the marks in physics : ");
int p = sc.nextInt();
int sum = (m+e+c+s+p);
System.out.println("the sum of marks scored is : " + sum);
int percentage = ((sum/total) * 100);
System.out.println("the percentage is : " + percentage );






}


}
 
Joined
Jan 24, 2024
Messages
41
Reaction score
8
The issue in your code is related to integer division. When you perform sum/total, both sum and total are of type int, and Java will perform integer division, which means the result will also be an int. In your case, this leads to the percentage being rounded down to the nearest integer, resulting in 0 if sum is less than total.
To fix this issue, you can use double for the percentage variable or cast one of the operands to double before performing the division. Here's an updated version of your code:

Java:
import java.util.Scanner;

public class main1 {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of student : ");
String n = sc.nextLine();
System.out.println("Enter the total marks to be scored : ");
int total = sc.nextInt();
System.out.println("enter the marks in maths : ");
int m = sc.nextInt();
System.out.println("enter the marks in english : ");
int e = sc.nextInt();
System.out.println("enter the marks in chemistry : ");
int c = sc.nextInt();
System.out.println("enter the marks in computer science : ");
int s = sc.nextInt();
System.out.println("enter the marks in physics : ");
int p = sc.nextInt();
int sum = (m + e + c + s + p);
System.out.println("the sum of marks scored is : " + sum);

// Use double to store the percentage
double percentage = ((double) sum / total) * 100;
System.out.println("the percentage is : " + percentage);
}
}
By casting sum to double or using 100.0 instead of 100, you ensure that the division is performed in floating-point arithmetic, giving you a more accurate percentage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,184
Members
46,721
Latest member
DollieSawt

Latest Threads

Top