Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Project ISC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

HIRANANDANI FOUNDATION SCHOOL, THANE

Mens Sana in Corpore Sano

COMPUTER science Project

Name: Sreeniket Rajesh


Class/Div: 11-B
Roll No: 38

1
INDEX
Sr. No Topic Page No
1 Number Programs 3-12

2 Strings 13-28

3 2d Arrays 29-48

2
NUMBER PROGRAMS
Program 1:
/**
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Adam number: The square of a number and the square of its reverse are reverse to each other.
*/
import java.util.Scanner;
public class PrimeAdam
{
public static int reverse(int num)
{
int rev = 0;
while (num != 0)
{
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
}
return rev;
}

public static boolean isAdam(int num)


{
int sqNum = num * num;
int revNum = reverse(num);
int sqRevNum = revNum * revNum;
int rev = reverse(sqNum);

3
return rev == sqRevNum;
}

public static boolean isPrime(int num)


{
int k= 0;

for (int i = 1; i <= num; i++) {


if (num % i == 0)
{
k++;
}
}
return k==2;
}

public static void main(String args[])


{
Scanner in=new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m=in.nextInt();
System.out.print("Enter the value of n: ");
int n=in.nextInt();
int k=0;
if (m>=n)
{
System.out.println("INVALID INPUT");
return;

4
}

System.out.println("THE PRIME-ADAM INTEGERS ARE:");


for (int i=m;i<=n;i++)
{
boolean adam = isAdam(i);
if(adam)
{
boolean prime = isPrime(i);
if (prime)
{
System.out.print(i + " ");
k++;
}
}
}

if (k==0)
{
System.out.print("NIL");
}

System.out.println();
System.out.println("Number of Prime-Adam integers are: " +k);
}
}

5
Program 2:
/**
* A number is said to be a Goldbach number, if the number can be expressed as the addition of
two odd prime number pairs.
*/
import java.util.Scanner;
public class GoldbachNumber
{
public static boolean isPrime(int n) {
int k=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
{
k++;
}
}
return k==2;
}

public static void main(String args[])


{
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n1=in.nextInt();
if (n1<=9||n1>=50)
{
System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
return;

6
}
if (n1%2!=0)
{
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
}
System.out.println("PRIME PAIRS ARE:");
int a=3;
int b=0;
while (a<=n1/2)
{
b=n1-a;
if (isPrime(a) && isPrime(b))
{
System.out.println(a + ", " + b);
}

a+=2;
}
}
}

7
Program 3:
import java.util.Scanner;
/**
* A prime number is said to be a circular prime if after any cyclic permutations of the digits, it
remains a prime.
*/
public class CircularPrime
{
public static boolean isPrime(int num)
{
int c=0;
for (int i=1;i<=num;i++)
{
if(num%i==0)
{
c++;
}
}
return c==2;
}
public static int getDigitCount(int num)
{
int c = 0;
while (num != 0)
{
c++;
num /= 10;
}
return c;

8
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("ENTER INTEGER TO CHECK (N): ");
int n = in.nextInt();
if (n<=0)
{
System.out.println("INVALID INPUT");
return;
}

boolean isCircularPrime = true;


if (isPrime(n))
{
System.out.println(n);
int digitCount = getDigitCount(n);
int divisor = (int)(Math.pow(10, digitCount - 1));
int n2 = n;
for (int i = 1; i < digitCount; i++)
{
int t1 = n2 / divisor;
int t2 = n2 % divisor;
n2 = t2 * 10 + t1;
System.out.println(n2);
if (!isPrime(n2))
{
isCircularPrime = false;

9
break;
}
}
}
else
{
isCircularPrime = false;
}

if (isCircularPrime)
{
System.out.println(n + " IS A CIRCULAR PRIME.");
}
else
{
System.out.println(n + " IS NOT A CIRCULAR PRIME.");
}
}
}

10
Program 4:
/**
* A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes,
12 boxes, 24 boxes and 48 boxes.
* Design a program to accept the number of boxes to be packed (N) by the user (maximum up to
1000 boxes) and display the break-up of the cartons used in
descending order of capacity (i.e. preference should be given to the highest capacity available,
and if boxes left are less than 6, an extra carton of
capacity 6 should be used.)
*/
import java.util.Scanner;
public class CartonBoxes
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of boxes (N): ");
int n = in.nextInt();
if (n < 1 || n > 1000)
{
System.out.println("INVALID INPUT");
return;
}
int cartonSizes[] = {48, 24, 12, 6};
int total = 0;
int t = n;
for (int i = 0; i < cartonSizes.length; i++)
{
int cartonCount = t / cartonSizes[i];

11
t = t % cartonSizes[i];
total += cartonCount;
if (cartonCount != 0)
{
System.out.println(cartonSizes[i] + " * " + cartonCount +" = " + (cartonSizes[i] *
cartonCount));
}
}

if (t!=0)
{
System.out.println("Remaining boxes = " + t + " * 1 = " + t);
total++;
}
else
{
System.out.println("Remaining boxes = 0");
}

System.out.println("Total number of boxes = " + n);


System.out.println("Total number of cartons = " + total);
}
}

12
STRING PROGRAMS
Program 1:
/**
* The names of the teams participating in a competition should be displayed on a banner
vertically, to accommodate as many teams as possible in a single
banner.
* Design a program to accept the names of N teams, where N is greater than 2 and less than 9,
and display them in vertical order, side by side with a
horizontal tab (i.e. eight spaces).
*/
import java.util.Scanner;
public class Banner
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n=in.nextInt();
if (n<=2||n>=9)
{
System.out.println("INVALID INPUT");
return;
}
String teams[]=new String[n];
int highLen = 0;
for (int i=0;i<n;i++)
{
System.out.print("Team " + (i+1) + ": ");
teams[i] = in.nextLine();

13
if (teams[i].length() > highLen) {
highLen = teams[i].length();
}
}

for (int i=0;i<highLen;i++)


{
for (int j=0;j<n;j++)
{
int len=teams[j].length();
if (i>=len)
{
System.out.print(" \t");
}
else
{
System.out.print(teams[j].charAt(i) + "\t");
}
}
System.out.println();
}
}
}

14
Program 2:
/**
* Program to display the number of words in a sentence input by the user, that begin and end
with a vowel. And also display the rearranged sentence with
the words begenning with the words that begin with vowels.
*/
import java.util.Scanner;
public class VowelCheck
{
public static boolean WordCheck(String w)
{
int len1=w.length();
char first=w.charAt(0);
char last=w.charAt(len1 - 1);
if(isVowel(w.charAt(0))==true && isVowel(w.charAt(len1-1))==true)
return true;
else
return false;
}
public static boolean isVowel(char ch)
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
else
return false;
}
public static void main(String args[])
{
int count=0,i=0,len=0;

15
char last=' ',ch=' ';
String s="",wd="",vowels="",nonVowels="";
Scanner in=new Scanner(System.in);
System.out.print("Sentence: ");
s=in.nextLine();
s=s.trim();
s=s.toUpperCase();
len=s.length();
last=s.charAt(len - 1);
if(last != '.' && last != '?' && last != '!')
{
System.out.println("INVALID INPUT");
return;
}

for(i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch==','||ch=='!')
{

if(wd.length() > 0 && WordCheck(wd)==true)


{
count++;
vowels+=wd+" ";
}
else
{

16
nonVowels+=wd+" ";
}
wd ="";
}
else
{
wd+=ch;
}
}

System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A


VOWEL = " + count);
System.out.println(vowels + nonVowels);
}

17
Program 3:

/**
Program to sort the words in ascending order of their length
*/
import java.util.Scanner;
import java.util.StringTokenizer;
class Arrange{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = in.nextLine().trim();
if(s.length() == 0){
System.out.println("INVALID INPUT");
return;
}
char last = s.charAt(s.length() - 1);
if(last != '!' && last != '?' && last != '.'){
System.out.println("INVALID INPUT");
return;
}
s = s.toUpperCase();
StringTokenizer st = new StringTokenizer(s, " .?!");
int size = st.countTokens();
String a[] = new String[size];
for(int i = 0; i < size; i++)
a[i] = st.nextToken();
for(int i = 0; i < a.length; i++){

18
for(int j = 0; j < a.length - 1 - i; j++){
if(a[j].length() > a[j + 1].length()){
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
else if(a[j].length() == a[j + 1].length()){
int d = a[j].compareTo(a[j + 1]);
if(d > 0){
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
String c = "";
for(int i = 0; i < a.length; i++)
c += a[i] + " ";
System.out.println(s + "\n" + c);
}
}

19
Program 4:
/**
* Checking if the words in a sentence(input by the user) are palindrome or not and converting
the non-palindrome words
*/
import java.util.*;
public class CheckPalin
{
static String palincheck(String word)
{
int flag=0;
String s1="",s2="",s3="";
String result="";
for(int i=word.length()-1;i>=0;i--)
{
char c=word.charAt(i);
s1=s1+c;
}
if(s1.equalsIgnoreCase(word))
{
flag=1;
}
if(flag!=1)
{
int l=word.length();
char last_char=word.charAt(l-1);
int dupli_length = 0;
int count = 0;
for(int i=l-1;i>=0;i--)

20
{
char c1 = word.charAt(i);
if(last_char==c1)
{
count++;
dupli_length = i;
}

else
break;
}
if(count<=1)
{
dupli_length = l;
}

if(count>1)
{
dupli_length++;
}

for(int i=0;i<dupli_length-1;i++)
{
char c = word.charAt(i);
s2=c+s2;
}
s3 = word+s2;
result = s3;

21
}
return result;
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String s = sc.nextLine();
int l = s.length();
String words[] = new String[100];
String k = "";
int ins = 0;
if(s.charAt(l-1)=='?'||s.charAt(l-1)=='.'||s.charAt(l-1)=='!')
{
for(int i=0;i<l;i++)
{
char c=s.charAt(i);
if(c==' '||c=='?'||c=='.'||c=='!')
{
words[ins++] = k;
k = "";
}
else
{
k = k+c;
}
}

22
for(int i=0;i<ins;i++)
{
String w=palincheck(words[i]);
words[i]=w;
}
System.out.println(s);
for(int i=0;i<ins;i++)
System.out.print(words[i]+" ");
}
else
System.out.println("INVALID INPUT");
}
}

23
Program 5:
/**
* Program to check if the date input by the user is valid or not.
*/
import java.util.Scanner;
public class ValidDateCheck
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a date (dd-mm-yyyy): ");
String inputDate = scanner.nextLine();
int day = Integer.parseInt(inputDate.substring(0, 2));
int month = Integer.parseInt(inputDate.substring(3, 5));
int year = Integer.parseInt(inputDate.substring(6));
boolean isValidDate = false;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day >= 1 && day <= 31)
isValidDate = true;
break;
case 4:

24
case 6:
case 9:
case 11:
if (day >= 1 && day <= 30)
isValidDate = true;
break;
case 2: // February

if (isLeapYear(year))
{

if (day >= 1 && day <= 29) {

isValidDate = true;

} else
{

if (day >= 1 && day <= 28) {

isValidDate = true;

}
break;

25
default:
isValidDate = false;
break;
}
if (isValidDate)
System.out.println("Valid date.");
else
System.out.println("Invalid date.");
scanner.close();
}

public static boolean isLeapYear(int year)


{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}

26
Program 6:
/**
* Program to find the total number of anagrams for a given word
* Example:
Enter a word : TOP
The Anagrams are :
TOP
TPO
OTP
OPT
PTO
POT
Total NO. of Anagrams = 6
*/

import java.util.*;
class anagramsWord
{
int count = 0;
void input()

{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word : ");
String s = sc.next();
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total NO. of Anagrams = "+count);

27
}
void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++;
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c);
}
}
}
public static void main(String args[])throws Exception
{
anagramsWord ob=new anagramsWord();
ob.input();
}
}

28
ARRAY PROGRAMS
Program 1:
/**
* Sorting each row of an array(input by the user) using bubble sort and displying the original as
well as sorted array
*/
import java.util.Scanner;
public class ArraySort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF M: ");
int m = in.nextInt();
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
if (m<=2||m>=10||n<=2||n>=10)
{
System.out.println("MATRIX SIZE OUT OF RANGE.");
return;
}
int a[][] = new int[m][n];
System.out.println("ENTER ELEMENTS OF MATRIX:");
for (int i=0;i<m;i++)
{
System.out.println("ENTER ELEMENTS OF ROW " + (i+1) + ":");
for (int j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}

29
}
System.out.println("ORIGINAL MATRIX");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n-1;j++)
{
for (int k=0;k<n-j-1;k++)
{
if (a[i][k]>a[i][k + 1])
{
int t=a[i][k];
a[i][k]=a[i][k+1];
a[i][k+1]=t;
}
}
}
}
System.out.println("MATRIX AFTER SORTING ROWS");
for (int i=0;i<m;i++)
{

30
for (int j=0;j<n;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

31
Program 2:
/**
* sorting the non-boundry elements of the matrix(input by the user) in ascending order and
displaying the original matrix, rearranged matrix, only
* only diagonal elements(of the rearranged matrix) with the sum of the diagonal elements
*/
import java.util.Scanner;
public class MatrixSort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("ENTER MATRIX SIZE (M): ");
int m = in.nextInt();
if (m <= 3 || m >= 10)
{
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return;
}
int a[][] = new int[m][m];
System.out.println("ENTER ELEMENTS OF MATRIX");
for (int i=0;i<m;i++)
{
System.out.println("ENTER ROW " + (i+1) + ":");
for (int j = 0; j < m; j++)
{
a[i][j]=in.nextInt();
if (a[i][j]<0)
{

32
System.out.println("INVALID INPUT");
return;
}
}
}
System.out.println("Original matrix");
for (int i=0;i<m;i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
int b[]=new int[(m - 2) * (m - 2)];
int k=0;
for (int i=1;i<m-1;i++)
{
for (int j=1;j<m-1;j++)
{
b[k++]=a[i][j];
}
}

for (int i=0;i<k-1;i++)


{
for (int j=0;j<k-i-1;j++)
{

33
if (b[j]>b[j+1])
{
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
k=0;
for (int i=1; i<m-1;i++)
{
for (int j=1;j<m-1;j++)
{
a[i][j]=b[k++];
}
}
System.out.println("Rearranged Matrix");
for (int i=0;i<m;i++)
{
for (int j=0; j<m;j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}

int sum=0;
System.out.println("DIAGONAL ELEMENTS");

34
for (int i=0;i<m;i++)
{
for (int j=0;j<m;j++)
{
if (i==j||i+j==m-1)
{
sum+=a[i][j];
System.out.print(a[i][j] + "\t");
}
else {
System.out.print("\t");
}
}
System.out.println();
}
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);

}
}

35
Program 3:
/**
* Program to display the mirror image of the matrix input by the user along with the original
matrix
*/

import java.util.*;
class MatrixMirror
{
public static int[][] mirrorImage(int A[][])
{
int t, half=A[0].length/2;
for(int i=0;i<A.length;i++)
{
for(int j=0;j<=half;j++)
{
t=A[i][A[0].length-1-j];
A[i][A[0].length-1-j]=A[i][j];
A[i][j]=t;
}
}
return A;
}
public static void display(int A[][])
{
for(int i=0;i<A.length;i++)
{
System.out.print("\t\t\t");
for(int j=0;j< A.length;j++)

36
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
public static void main( String args[] )
{
Scanner in=new Scanner(System.in);
System.out.print("INPUT:\t\tM=");
int M=in.nextInt();
if(M<=2||M>=20)
{
System.out.println("OUTPUT:\t\tSIZE OUT OF RANGE");
}else{
int A[][]=new int[M][M];
for(int i=0;i < M;i++)
{
for(int j=0; j<M; j++)
{
A[i][j]=in.nextInt();
}
}
System.out.println("OUTPUT:\t\t");
System.out.println("ORIGINAL MATRIX");
display(A);
A=mirrorImage(A);
System.out.println("MIRROR IMAGE MATRIX");

37
display(A);
}
}
}

38
Program 4:
/**
* Program to check if the matrix input by the user is symmetric or not.A square matrix is said to
be Symmetric, if the element of the ith row and the jth
column is equal to the element of jth row and the ith column. Display the original matrix and the
symmetric matrix. Also find and display the sum of the
left and right diagonals.
*/
import java.util.*;
public class SymmetricMatrix
{
public static void main()
{
Scanner in=new Scanner(System.in);
int m,i,j,flag=0,ld=0,rd=0;
System.out.println("Enter size of matrix : ");
m=in.nextInt();
if((m>2)&&(m<10))
{
int a[][]=new int[m][m];
System.out.println("Enter Elements in the matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");

39
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=a[j][i])
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
for(i=0;i<m;i++)
{
ld=ld+a[i][i];
rd=rd+a[i][m-1-i];
}

40
System.out.println("The sum of the left diagonal : "+ld);
System.out.println("The sum of the right diagonal : "+rd);
}
else
System.out.println("THE MATRIX SIZE IS OUT OF RANGE");
}
}

41
Program 5:
/**
* A program to:
* find the duration for which each user logged and Output all records along with the duration in
hours (format hours: minutes).
* Output the record of the user who logged for the longest duration. It is assumed that no user
will login for more than 48 hours.
*/
import java.util.Scanner;
public class MachineUtilization
{
public static void main(String args[])
{
int MINS_IN_DAY = 1440;
int MINS_IN_HOUR = 60;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Users: ");
int n = in.nextInt();
in.nextLine();
if (n>100||n<1)
{
System.out.println("Invalid Input!");
System.out.println("No of users must be between 1 and 100");
return;
}
String records[][] = new String[n][6];
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i=0;i<n;i++)
{

42
System.out.println("Enter record of user " + (i+1) + ": ");
System.out.print("Enter User Identification: ");
records[i][0] = in.nextLine();
System.out.print("Enter Login Time(hh:mm): ");
records[i][1] = in.nextLine();
System.out.print("Enter Login Date(dd-mm): ");
records[i][2] = in.nextLine();
System.out.print("Enter Logout Time(hh:mm): ");
records[i][3] = in.nextLine();
System.out.print("Enter Logout Date(dd-mm): ");
records[i][4] = in.nextLine();
}
System.out.println();
int longIdx = 0;
int longDuration = 0;
for (int i=0;i<n;i++)
{
int duration = 0;
int tempIdx = records[i][1].indexOf(':');
int loginHr = Integer.parseInt(records[i][1].substring(0, tempIdx));
int loginMin = Integer.parseInt(records[i][1].substring(tempIdx + 1));
tempIdx = records[i][3].indexOf(':');
int logoutHr = Integer.parseInt(records[i][3].substring(0, tempIdx));
int logoutMin = Integer.parseInt(records[i][3].substring(tempIdx + 1));
int m1 = loginHr * MINS_IN_HOUR + loginMin;
int m2 = logoutHr * MINS_IN_HOUR + logoutMin;
if (records[i][2].equals(records[i][4]))
duration = m2 - m1;

43
else
{
int daysDiff = 0;
tempIdx = records[i][2].indexOf('-');
int loginDay = Integer.parseInt(records[i][2].substring(0, tempIdx));
int loginMonth = Integer.parseInt(records[i][2].substring(tempIdx + 1));
tempIdx = records[i][4].indexOf('-');
int logoutDay = Integer.parseInt(records[i][4].substring(0, tempIdx));
int logoutMonth = Integer.parseInt(records[i][4].substring(tempIdx + 1));
if (loginMonth == logoutMonth)
daysDiff = logoutDay - loginDay - 1;
else
{
daysDiff = monthDays[loginMonth - 1] - loginDay + logoutDay - 1;
}
duration = (MINS_IN_DAY - m1) + m2 + daysDiff * MINS_IN_DAY;
}
if (duration > longDuration)
{
longDuration = duration;
longIdx = i;
}
int durHr = duration / 60;
int durMin = duration % 60;
records[i][5] = (durHr == 0 ? "00" : durHr)
+ ":"
+ (durMin == 0 ? "00" : durMin);

44
}
System.out.println("User\t\tLogin\t\tLogout\t\tDuration");
System.out.println("Identification\tTime & Date\tTime & Date\tHours:Minutes");
for (int i=0;i<n;i++)
{
for (int j=0;j<6;j++)
{
System.out.print(records[i][j] + "\t");
}
System.out.println();
}
System.out.println();
System.out.println("The user who logged in for longest duration:");
for (int j=0;j<6;j++)
{
System.out.print(records[longIdx][j] + "\t");
}
}
}

45
Program 6:
/**
* The result of a quiz competition is to be prepared as follows:
* The quiz has five questions with four multiple choices (A, B, C, D), with each question
carrying 1 mark for the correct answer.
* Design a program to accept the number of participants N such that N must be greater than 3
and less than 11.
* Create a double-dimensional array of size (Nx5) to store the answers of each participant row-
wise.
* Calculate the marks for each participant by matching the correct answer stored in a single-
dimensional array of size 5.
* Display the scores for each participant and also the participant(s) having the highest score.
*/

import java.util.Scanner;
public class QuizCompetition
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Number of Participants (N): ");
int n=in.nextInt();
if (n<=3||n>=11)
{
System.out.println("INPUT SIZE OUT OF RANGE.");
return;
}
char answers[][] = new char[n][5];
char key[] = new char[5];
System.out.println("Enter answers of participants");

46
for (int i=0;i<n;i++)
{
System.out.println("Participant " + (i+1));
for (int j=0;j<5;j++)
{
answers[i][j] = in.next().charAt(0);
}
}
System.out.println("Enter Answer Key:");
for (int i=0;i<5;i++)
{
key[i]=in.next().charAt(0);
}
int hScore=0;
int score[]=new int[n];
System.out.println("Scores:");
for (int i=0;i<n;i++)
{
for (int j=0;j<5;j++)
{
if (answers[i][j]==key[j])
{
score[i]++;
}
}
if (score[i]>hScore)
{
hScore=score[i];

47
}
System.out.println("Participant " + (i+1) + " = " + score[i]);
}
System.out.println("Highest Score:");
for (int i=0;i<n;i++)
{
if (score[i]==hScore)
{
System.out.println("Participant " + (i+1));
}
}
}
}

48

You might also like