|
|
back to boardWA #1 - Correct output for sample input - Need help with output formating. My code is giving correct output but only till one decimal place when the percentage is in whole number (50.0% instead of 50.00%). I'm using DecimalFormat. Below is my code:- import java.util.*; import java.lang.*; import java.text.DecimalFormat; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //candidates int m = sc.nextInt(); //electors double div = 0.0; double per = 0.0; int t = m; int v[] = new int[n]; while (m != 0) { int cv = sc.nextInt(); cv = cv - 1; v[cv] = v[cv] + 1; m--; } for (int i = 0; i <=n-1; i++) { int a = v[i]; div = t/a; per = (1/div * 100.0); per = Double.parseDouble(new DecimalFormat("##.##").format(per)); System.out.println(per + "%"); } } } O/P (For sample Input) 50.0% 33.33% 16.67% |
|
|