import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String names[] = new String[15];
double salaries[] = new double[15];
double salariesTotal = 0;
for (int i = 0; i < names.length; i++) {
System.out.print("Enter employee name " + (i + 1) + ": ");
names[i] = in.nextLine();
System.out.print("Enter employee salary " + (i + 1) + ": ");
salaries[i] = in.nextDouble();
salariesTotal += salaries[i];
in.nextLine();
}
System.out.println();
for (int i = 0; i < names.length; i++) {
System.out.println("Enter employee name: " + names[i]);
System.out.println("Enter employee salary: " + salaries[i]);
}
double averageSalary = salariesTotal / 15.0;
System.out.printf("The average salary employees: %.2f\n", averageSalary);
in.close();
}
}
Do this coding without using array and printf
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ouput = "";
double salariesTotal = 0;
for (int i = 0; i < 15; i++) {
System.out.print("Enter employee name " + (i + 1) + ": ");
String name = in.nextLine();
System.out.print("Enter employee salary " + (i + 1) + ": ");
double salary = in.nextDouble();
ouput += "Employee name " + (i + 1) + ": " + name + "\n";
ouput += "Employee salary " + (i + 1) + ": " + salary + "\n";
salariesTotal += salary;
in.nextLine();
}
System.out.println(ouput);
double averageSalary = salariesTotal / 15.0;
System.out.println("The average salary employees: " + averageSalary);
in.close();
}
}
Comments
Leave a comment