Help me with this code. Not all 4 test cases are getting passed

[Ram wants to know the maximum marks scored by him in each semester.
The mark should be between 0 to 100 ,if goes beyond the range display “You have entered invalid mark.”
Sample Input 1:
Enter no of semester: 3
Enter no of subjects in 1 semester: 3
Enter no of subjects in 2 semester: 4
Enter no of subjects in 3 semester: 2
Marks obtained in semester 1: 50 60 70
Marks obtained in semester 2: 90 98 76 67
Marks obtained in semester 3: 89 76
Sample Output 1:
Maximum mark in 1 semester:70
Maximum mark in 2 semester:98
Maximum mark in 3 semester:89
Sample Input 2:
Enter no of semester: 3
Enter no of subjects in 1 semester: 3
Enter no of subjects in 2 semester: 4
Enter no of subjects in 3 semester: 2
Marks obtained in semester 1: 55 67 98
Marks obtained in semester 2: 67 -98
Sample Output 2: You have entered invalid mark.]
(http://Ram wants to know the maximum marks scored by him in each semester. The mark should be between 0 to 100 ,if goes beyond the range display “You have entered invalid mark.” Sample Input 1: Enter no of semester: 3 Enter no of subjects in 1 semester: 3 Enter no of subjects in 2 semester: 4 Enter no of subjects in 3 semester: 2 Marks obtained in semester 1: 50 60 70 Marks obtained in semester 2: 90 98 76 67 Marks obtained in semester 3: 89 76 Sample Output 1: Maximum mark in 1 semester:70 Maximum mark in 2 semester:98 Maximum mark in 3 semester:89 Sample Input 2: Enter no of semester: 3 Enter no of subjects in 1 semester: 3 Enter no of subjects in 2 semester: 4 Enter no of subjects in 3 semester: 2 Marks obtained in semester 1: 55 67 98 Marks obtained in semester 2: 67 -98 Sample Output 2: You have entered invalid mark.)

Here is the code:

import java.util.*;
class HighestMarkPerSem{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println(“Enter no of semester:”);
int n=sc.nextInt();
int s[]=new int[n];
int temp=0,max=0,ntemp=0,stemp=0;
int mark[]=new int[100];
int fmark[][]=new int[10][10];
for(int i=0;i<n;i++){
System.out.println(“Enter no of subjects in “+(i+1)+” semester:”);
s[i]=sc.nextInt();
}
for(int j=0;j<n;j++){
temp=s[j];
System.out.println(“Marks obtained in semester “+(j+1)+”:”);
for(int k=0;k<temp;k++){
mark[k]=sc.nextInt();
if(mark[k]<0 || mark[k]>100){
System.out.println(“You have entered invalid mark.”);
}
else{
fmark[j][k]=mark[k];
}
}

    }
    
    for(int p=0;p<n;p++){
        ntemp=s[p];
        max=0;
        System.out.print("Maximum mark in "+(p+1)+" semester:");
        for(int q=0; q<ntemp; q++){
            if(fmark[p][q]<0){
                System.out.println("You have entered invalid mark.");
            }
            else if(fmark[p][q]>100){
                System.out.println("You have entered invalid mark.");
            }
                else if(fmark[p][q]>max){
                    max=fmark[p][q];
                }
            }
            System.out.print(max);
            System.out.println();
        }
        
}

}

Share the link of your code !

Hey,
Please share the problem link and the link of the code which you submitted

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwigvdz33rPqAhVUT30KHXOxBAoQFjABegQIARAB&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F62716788%2Fhow-to-apply-nested-for-loop-in-this-java-code&usg=AOvVaw1B02tnY7E3fPw-UMhxS6oZ

Hey,
A word of advice please share a link to .java or .txt file reading from photos is extremely difficult, Is this a school/college submission

I’m having some trouble with creating link for .java file and it’s a college assignment.

This is my solution.

https://ide.geeksforgeeks.org/TNqmX6EyDR

Thanks a lot your code works fine.

Cool

1 Like