using array write a program in java to accept roll number and marks of 10 students. Accept roll number and check whether it is present or not. If found print an appropriate message otherwise print no roll number such found
Answers
import java.util.Scanner;
public class RollNumbers {
public static void main(String[ ] args) {
int[ ] rollNumbers, marks;
rollNumbers = marks = new int[10];
System.out.println("Enter roll numbers and marks - ");
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print(" Roll no. - ");
rollNumbers[i] = sc.nextInt( );
System.out.print(" Marks - ");
marks[i] = sc.nextInt( );
}
System.out.print("\nEnter the Roll no. to be searched - ");
int rollNumber = sc.nextInt( ), mark = 0;
boolean found = false;
for (int i = 0 ; i < 10; i++)
if (rollNumbers[i] = = rollNumber) {
mark = marks[i];
found = true;
break;
}
System.out.println(found ? "Marks for the entered Roll No. - " + mark : "Roll no. not found!");
}
}