Write a program to input marks in Eng, Science and Maths by using three single dimensional arrays . Calculate and Print the following information s
1 - average marks secured by each student
2- class average in each subject (class average is the average marks obtained by the students in a particular subject
Answers
Explanation:
import java.util.Scanner;
public class KboatSDAMarks
{
public void computeStudentNClassAverage() {
final int TOTAL_STUDENTS = 40;
Scanner in = new Scanner(System.in);
int english[] = new int[TOTAL_STUDENTS];
int maths[] = new int[TOTAL_STUDENTS];
int science[] = new int[TOTAL_STUDENTS];
double avgMarks[] = new double[TOTAL_STUDENTS];
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Marks in English: ");
english[i] = in.nextInt();
System.out.print("Marks in Maths: ");
maths[i] = in.nextInt();
System.out.print("Marks in Science: ");
science[i] = in.nextInt();
avgMarks[i] = (english[i] + maths[i] + science[i]) / 3.0;
}
int engTotal = 0, mathsTotal = 0, sciTotal = 0;
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println("Average marks of student " + (i+1) + " = " + avgMarks[i]);
engTotal += english[i];
mathsTotal += maths[i];
sciTotal += science[i];
}
System.out.println("Class Average in English = " + ((double)engTotal / TOTAL_STUDENTS));
System.out.println("Class Average in Maths = " + ((double)mathsTotal / TOTAL_STUDENTS));
System.out.println("Class Average in Science = " + ((double)sciTotal / TOTAL_STUDENTS));
}
}
Output
BlueJ output of KboatSDAMarks.java
BlueJ output of KboatSDAMarks.java
Question 10
Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and store all the even numbers in an array even[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of both the arrays.
import java.util.Scanner;
public class KboatSDAEvenOdd
{
public void segregateEvenOdd() {
final int NUM_COUNT = 20;
Scanner in = new Scanner(System.in);
int i = 0;
int arr[] = new int[NUM_COUNT];
int even[] = new int[NUM_COUNT];
int odd[] = new int[NUM_COUNT];
System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}
int eIdx = 0, oIdx = 0;
for (i = 0; i < NUM_COUNT; i++) {
if (arr[i] % 2 == 0)
even[eIdx++] = arr[i];
else
odd[oIdx++] = arr[i];
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}
}
Output