In this post, we will learn Search a Course Program in java Programming language.
Question:
IIHT institution is offering a variety of courses to students. Students have a facility to check whether a particular course is available in the institution. Write a program to help the institution accomplish this task. If the number is less than or equal to zero display “Invalid Range”.
Assume maximum number of courses is 20.
Sample Input 1:
Enter no of course:
5
Enter course names:
Java
Oracle
C++
Mysql
Dotnet
Enter the course to be searched:
C++
Sample Output 1:
C++ course is available
Sample Input 2:
Enter no of course:
3
Enter course names:
Java
Oracle
Dotnet
Enter the course to be searched:
C++
Sample Output 2:
C++ course is not available
Sample Input 3:
Enter no of course:
0
Sample Output 3:
Invalid Range
CODE:–
import java.util.*; public class Course { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter no of course:"); int no_crs=sc.nextInt(); if(no_crs>0) { System.out.println("Enter course names:"); String crs[]=new String[no_crs]; for(int i=0;i<no_crs;i++) { crs[i]=sc.next(); } System.out.println("Enter the course to be searched:"); String srch=sc.next(); int flag=0; for(int i=0;i<no_crs;i++) { if(srch.equals(crs[i])) { flag++; } } if(flag!=0) { System.out.println(srch+" course is available"); } else { System.out.println(srch+" course is not available"); } } else { System.out.println("Invalid Range"); } } }