0% found this document useful (0 votes)
96 views

How Many Prime-Palindrome Integers Are There in The Range Between M and N (Both Inclusive) and

1. The document describes a program to find prime palindrome integers between two given numbers. 2. It takes the lower and upper limits as input, checks if they are valid, then determines how many numbers in that range are both prime and palindromes. 3. The prime palindrome numbers found are printed along with the count of such numbers.

Uploaded by

Ishant chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

How Many Prime-Palindrome Integers Are There in The Range Between M and N (Both Inclusive) and

1. The document describes a program to find prime palindrome integers between two given numbers. 2. It takes the lower and upper limits as input, checks if they are valid, then determines how many numbers in that range are both prime and palindromes. 3. The prime palindrome numbers found are printed along with the count of such numbers.

Uploaded by

Ishant chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Q1) A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a

palindrome. Given two positive integers m and n, where m<= n, write a program to determine
how many prime-palindrome integers are there in the range between m and n (both inclusive) and
output them.
import java.io.*;

class PrimePal
{
public void showPrimePal() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int m,n, c=0;
System.out.println(“Enter the Lower Limit:”);
m=Integer.parseInt(br.readLine());
System.out.println(“Enter the Upper Limit:”);
n=Integer.parseInt(br.readLine());
if(m>n || n>3000)
System.out.println(“Out of Range.”);
else
{
System.out.println(“The Prime Palindrome integers are:”);
while(m<=n)
{
if(checkPrimePal(m))
{
if(c==0)
System.out.print(m);
else
System.out.print(“, “+m);
c++;
}
m++;
}
System.out.println(“\nFrequency of Prime Palindrome integers: “+c);
}
}
boolean checkPrimePal(int x)
{
boolean flag=false;
int c=0, temp=x;
int rev=0,rem=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;
}
if(c<=2)
{
while(x!=0)
{
rem=x%10;
rev=rev*10+rem;
x/=10;
}
if(rev==temp)
flag=true;
}
return flag;
}
public static void main(String args[]) throws IOException
{
PrimePal ob=new PrimePal ();
ob.showPrimePal();
}
}

ALGORITHM-:
Start
Checking for errors
initialisation
determining the no. of palendromes in between m&n (inclusive)
main function
creating object of class
calling functions
Stop

Q2) Write a program to accept a sentence as input. The words in the string are to be separated by a
blank. Each word must be in upper case. The sentence is terminated by either “.”,”!” or “?”. Perform
the following tasks:
(i) Obtain the length of the sentence. (measured in words)
(ii) Arrange the sentence in alphabetical order of the words.

import java.io.*;
class WordsInSentence
{
public void take() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str, words[], stk=””;
int i,j,c=0,flag, len;
char ch;
while(true)
{
flag=0;
System.out.println(“Enter the sentence:”);
str=br.readLine();
len= str.length();
words=new String[len];
for(i=0;i
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
else
System.out.println(“Enter the sentence again with all uppercase letters”);
}
i=0;
while(i<len)
{
ch=str.charAt(i);
if(ch==’ ‘|| ch==’.’ || ch==’?’ || ch==’!’)
{
words[c]=stk;
c++;
i++;
stk=””;
}
else
{
stk+=ch;
i++;
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c-1;j++)
{
if((words[j].compareTo(words[j+1]))>0)
{
stk=words[j];
words[j]=words[j+1];
words[j+1]=stk;
}
}
}
System.out.println(“Length= “+c);
System.out.println(“\nRearranged Sentence:\n”);
for(i=0;i<c;i++)
System.out.print(words[i]+” “);
}
public static void main(String args[]) throws IOException
{
WordsInSentence ob=new WordsInSentence();
ob.take();
}
}
ALGORITHM-:
Start
Checking for errors
initialisation
Obtaining the length of the sentence
Arranging the sentence in alphabetical order of the words
main function
creating object of class
calling functions
Stop

Q3) Write a program to declare a matrix A [][] of order (MXN) where ‘M’ is the number of rows and ‘N’
is the number of columns such that both M and N must be greater than 2 and less than 20. Allow the
user to input integers into this matrix. Perform the following tasks on the matrix:

1.
2. Display the input matrix
3. Find the maximum and minimum value in the matrix and display them along with their position.
4. Sort the elements of the matrix in ascending order using any standard sorting technique and
rearrange them in the matrix.
5. Output the rearranged matrix.

import java.io.*;

class Matrix
{
public void take()throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[][],m,n;
int g,h,i,j,max,min,maxr,maxc,minr,minc;
while(true)
{
System.out.println(“\nEnter the number of rows :”);
m=Integer.parseInt(br.readLine());
System.out.println(“\nEnter the number of columns:”);
n=Integer.parseInt(br.readLine());
if(m<2 || n<2 || m>20 || n>20)
System.out.println(“\nEnter the number of rows and columns”);
else
break;
}
arr=new int[m][n];
for(i=0;i<m; i++)
{
for(j=0;j<n;j++)
{
System.out.println(“\nEnter Value:”);
arr[i][j]=Integer.parseInt(br.readLine());
}
}
max=arr[0][0];
min=arr[0][0];
maxr=0;
minr=0;
maxc=0;
minc=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
maxr=i;
maxc=j;
}
else if(arr[i][j]< min)
{
minr=i;
minc=j;
min=arr[i][j];
}
}
}
System.out.println(“\nOriginal Matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+” “);
}
System.out.println();
}
System.out.println(“\nMaximum Value=”+max);
System.out.println(“\nRow=”+maxr);
System.out.println(“\nColumn=”+maxc);
System.out.println(“\nMinimum Value=”+min);
System.out.println(“\nRow=”+minr);
System.out.println(“\nColumn=”+minc);
for(g=0;g<m;g++)
{
for(h=0;h<n;h++)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(arr[g][h]< arr[i][j])
{
min=arr[g][h];
arr[g][h]=arr[i][j];
arr[i][j]=min;
}
}
}
}
}
System.out.println(“\nSorted Array\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+” “);
}
System.out.println();
}
}
public static void main(String args[]) throws Exception
{
Matrix ob=new Matrix();
ob.take();
}
}
ALGORITHM-:
Start
Checking for errors
initialisation
Displaying the input matrix
Finding the maximum and minimum value in the matrix and display them along with their
position.
Sorting the elements of the matrix in ascending order using any standard sorting technique
and rearranging them in the matrix.
creating object of class
calling functions
Stop

Q4) Write a program to accept a sentence as input. The words in the string are to be separated by a
blank. Each word must be in upper case. The sentence is terminated by either “.”,”!” or “?”. Perform
the following tasks:
(i) Obtain the length of the sentence. (measured in words)
(ii) Arrange the sentence in alphabetical order of the words.

import java.io.*;
class WordsInSentence
{
public void take() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str, words[], stk=””;
int i,j,c=0,flag, len;
char ch;
while(true)
{
flag=0;
System.out.println(“Enter the sentence:”);
str=br.readLine();
len= str.length();
words=new String[len];
for(i=0;i
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
else
System.out.println(“Enter the sentence again with all uppercase letters”);
}
i=0;
while(i<len)
{
ch=str.charAt(i);
if(ch==’ ‘|| ch==’.’ || ch==’?’ || ch==’!’)
{
words[c]=stk;
c++;
i++;
stk=””;
}
else
{
stk+=ch;
i++;
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c-1;j++)
{
if((words[j].compareTo(words[j+1]))>0)
{
stk=words[j];
words[j]=words[j+1];
words[j+1]=stk;
}
}
}
System.out.println(“Length= “+c);
System.out.println(“\nRearranged Sentence:\n”);
for(i=0;i<c;i++)
System.out.print(words[i]+” “);
}
public static void main(String args[]) throws IOException
{
WordsInSentence ob=new WordsInSentence();
ob.take();
}
}
ALGORITHM-:
Start
Checking for errors
initialisation
Obtaining the length of the sentence
Arranging the sentence in alphabetical order of the words
main function
creating object of class
calling functions
Stop

Q5) A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a
palindrome. Given two positive integers m and n, where m<= n, write a program to determine
how many prime-palindrome integers are there in the range between m and n (both inclusive) and
output them.
The input contains two positive integers m and n where m>=100 and n<= 3000. Display number of
prime palindrome integers in the specified range along with their values in the format specified below:

class PrimePal
{
public void showPrimePal() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int m,n, c=0;
System.out.println(“Enter the Lower Limit:”);
m=Integer.parseInt(br.readLine());
System.out.println(“Enter the Upper Limit:”);
n=Integer.parseInt(br.readLine());
if(m>n || n>3000)
System.out.println(“Out of Range.”);
else
{
System.out.println(“The Prime Palindrome integers are:”);
while(m<=n)
{
if(checkPrimePal(m))
{
if(c==0)
System.out.print(m);
else
System.out.print(“, “+m);
c++;
}
m++;
}
System.out.println(“\nFrequency of Prime Palindrome integers: “+c);
}
}
boolean checkPrimePal(int x)
{
boolean flag=false;
int c=0, temp=x;
int rev=0,rem=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;
}
if(c<=2)
{
while(x!=0)
{
rem=x%10;
rev=rev*10+rem;
x/=10;
}
if(rev==temp)
flag=true;
}
return flag;
}
public static void main(String args[]) throws IOException
{
PrimePal ob=new PrimePal ();
ob.showPrimePal();
}
}
ALGORITHM-:
Start
Checking for errors
initialisation
determine how many prime-palindrome integers are there in the range between m and n
(both inclusive)
main function
creating object of class
calling functions
Stop

Q6)Write a programme to check whether a number is a MAGIC number or not?

import java.util.*;

public class MagicNumberCheck

public static void main(String args[])

Scanner ob=new Scanner(System.in);

System.out.println("Enter the number to be checked.");

int n=ob.nextInt();

int sum=0,num=n;

while(num>9)

sum=num;int s=0;

while(sum!=0)

s=s+(sum%10);

sum=sum/10;

num=s;

}
if(num==1)

System.out.println(n+" is a Magic Number.");

else

System.out.println(n+" is not a Magic Number.");

}}}
ALGORITHM-:
Start
Initialisation
Cheacking wether magic no. or not
Printing wether magic no. or not
Stop

Q7) A bank intends to design a program to display the denomination of an input amount, up to 5
digits. The available denomination with the bank are of rupees 1000 , 500 , 100 , 50 , 20 , 10 , 5 , 2 ,
and 1.
Design a program to accept the amount from the user and display the break-up in descending order of
denomination. (i.e. preference should be given to the highest denomination available) along with the
total number of notes. [Note: Only the denomination used should be displayed]. Also print the amount
in words according to the digits.

import java.io.*;

class QBank
{
public void intake() throws IOException
{
int rev=0,amount,dummy,rem;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the Amount:”);
amount=Integer.parseInt(br.readLine());
if(amount >99999)
{
System.out.println(“Invalid Amount…”);
return;
}
dummy=amount;
while(dummy !=0)
{
rev=rev*10+dummy%10;
dummy=dummy/10;
}
System.out.print(“Amount in words :”);
while(rev!=0)
{
rem=rev%10;
switch(rem)
{
case 0:
System.out.print(” ZERO”);
break;
case 1:
System.out.print(” ONE”);
break;
case 2:
System.out.print(” TWO”);
break;
case 3:
System.out.print(” THREE”);
break;
case 4:
System.out.print(” FOUR”);
break;
case 5:
System.out.print(” FIVE”);
break;
case 6:
System.out.print(” SIX”);
break;
case 7:
System.out.print(” SEVEN”);
break;
case 8:
System.out.print(” EIGHT”);
break;
case 9:
System.out.print(” NINE”);
}
rev=rev/10;
}
int den[]={1000,500,100,50,20,10,5,2,1};
int i=0, tot=0;
System.out.println(“\nDENOMINATORS:\n”);
while (amount!=0)
{
rev=amount/den[i];
if(rev!=0)
{
System.out.println(den[i]+” X ” + rev + ” = ” + rev*den[i]);
tot+=rev;
}
amount=amount%den[i];
i++;
}
System.out.println(“TOTAL NUMBER OF NOTES: “+ tot);
}
}
ALGORITHM-:
Start
accepting the amount from the user
displaying the break-up in descending order of denomination
displaying the total number of notes
printing the amount in words according to the digits.
Stop

Q8) A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a
right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits. If the
sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar
numbers are: 9, 45, 297 …….

import java.io.*;

class FindKarpekar
{
int p,q;
int count=0;
void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
p=Integer.parseInt(br.readLine());
q=Integer.parseInt(br.readLine());
if(p>=5000 || q>=5000 || p>=q)
System.out.println(“Out of range”);
else
{
System.out.println(“THE KAPREKAR NUMBERS ARE:- “);
for(int i=p;i<=q;i++)
{
if(check(i)==1)
{
if(count==0)
System.out.print(i);
else
System.out.print(” ,”+i);
count++;
}
}
System.out.println();
System.out.println(“FREQUENCY OF KAPREKAR NUMBERS IS: “+count);
}
}
long check(int n)
{
int len=(“”+n).length();
long sq=n*n;
long k=sq%(long)(Math.pow(10,len));
long s=sq/(long)(Math.pow(10,len));
long l=(k+s);
if(l==n)
return 1;
else
return 0;
}
public static void main(String args[])throws IOException
{
FindKarpekar ob=new FindKarpekar();
ob.input();
}
}
ALGORITHM-:
Start
Initialisation
Cheacking wether karpekar no. or not
Printing wether karpekar no. or not
Main function
Making object
Calling functions
Stop

Q9) Input a paragraph containing ‘n’ number of sentences where (1 = < n < 4). The words are to be
separated with a single blank space and are in UPPERCASE. A sentence may be terminated either with
a full stop ‘.’ Or a question mark ‘?’ only. Any other character may be ignored. Perform the following
operations:

(i) Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate error
message must be displayed.
(ii) Find the number of words in the whole paragraph

(iii) Display the words in ascending order of their frequency. Words with same frequency may appear
in any order.
import java.util.*;
class SentencesOrder
{
String s,str,sarr[],strarr[];
StringTokenizer st;
int i,j,n,c,index=0,fre[],index1=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
System.out.println(“Enter the Number of sentences:”);
n=Integer.parseInt(br.readLine());
if(n< 1 || n >4)
{ System.out.println(“Wrong Input…”);
return;
}
System.out.println(“Enter the Paragraph:”);
str=br.readLine();
st=new StringTokenizer(str,”,.? “);
n=st.countTokens();
System.out.println(“Number of Words in the paragraph=”+n);
sarr=new String[n];
strarr=new String[n];
fre=new int[n];
while(st.hasMoreTokens())
{
sarr[index++]=st.nextToken();
}
for(i=0;i< index-1;i++)
{
for(j=i+1;j< index;j++)
{
if(sarr[i].compareTo(sarr[j]) > 0)
{
s=sarr[i];
sarr[i]=sarr[j];
sarr[j]=s;
}
}
}
c=1;
s=sarr[0];
for(i=1;i< index;i++)
{
if(!s.equals(sarr[i]))
{
strarr[index1]=s;
fre[index1++]=c;
c=1;
s=sarr[i];
}
else
c++;
}
strarr[index1]=s;
fre[index1++]=c;
for(i=0;i< index1-1;i++)
{
for(j=i+1;j< index1;j++)
{
if(fre[i] > fre[j])
{
n=fre[i];
fre[i]=fre[j];
fre[j]=n;
s= strarr[i];
strarr[i]=strarr[j];
strarr[j]=s;
}
}
}
System.out.println(“WORD FREQUENCY”);
for(i=0;i< index1;i++)
System.out.println(strarr[i]+” “+fre[i]);
}
}
ALGORITHM-:
Start
Initialisation
Accepting the number of sentences. If the number of sentences exceeds the limit, an
appropriate error message must be displayed.
Finding the number of words in the whole paragraph
Displaying the words in ascending order of their frequency. Words with same frequency
may appear in any order.
Displaying output arrary
Stop
Q10) Design a class Perfect to check if a given number is a perfect number or not. A number is said to be perfect if
the sum of the factors of the number excluding itself is equal to the original number.
Class name: Perfect
Data members/instance variables:
num: to store the number
Methods/Member functions:
Perfect(int n): parameterized constructor to initialize the data member num = n.
int sumOfFactors(int i): returns the sum of the factors of the number (num), excluding itself, using recursive
technique.
void check(): checks whether the given number is perfect by invoking the function sumOfFactors(int) and displays the
result with an appropriate message.
Specify the class Perfect, giving details of the constructor, int sumOfFactors(int) and void check(). Define
the main() function to create an object and call the functions accordingly to enable the task.

import java.util. *;

class Perfect

int num;

Perfect (int nn)

num = nn;

int sum_of_factors (int i)

if (num == i)
return (sum_of_factors (i/2));

else if (i == 1)

return (1);

else if (num % i == 0)

return i +(sum_of_factors(i-1));

else

return (sum_of_factors (i-1));]

void check ( )

if (num == sum_of_factors (num))

System. out. println (" Perfect Number");

else System. out. println ("Not Perfect Number");

public static void main (String args [ ] )

System.out.println ("Enter one number");

Scanner sc=new Scanner(System.in);

int n = sc.nextInt();

Perfect ob = new Perfect (n);

ob. check ( );

}}
ALGORITHM-:
Start
Designing a class Perfect to check if a given number is a perfect number or not
initializing num: to store the number
Creating functions Perfect(int n): parameterized constructor to initialize the data member
creating functions int sumOfFactors(int i): returns the sum of the factors of the number
(num), excluding itself, using recursive technique.
Creating functionsvoid check(): checks whether the given number is perfect by invoking the
function sumOfFactors(int) and displays the result with an appropriate message.
Specifying the class Perfect, giving details of the constructor, int sumOfFactors(int) and
void check().
Creating main function
Creating object of class
Stop

Q11) Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find the
smallest integer that is greater than M and whose digits add up to N. For example, if M = 100 and N = 11, then the
smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required number whose sum of
all its digits is equal to N. Also, print the total number of digits present in the required number. The program should
check for the validity of the inputs and display an appropriate message for an invalid input

import java.util.*;

class Q1_ISC2015

{int sumDig(long n){

int sum = 0, d; while(n>0)

{ d = (int)(n%10);

sum = sum + d;

n = n/10; }

return sum; }int countDig(long n) {

String s = Long.toString(n);

int len = s.length();

return len;

}public static void main()throws Exception

{ Q1_ISC2015 ob = new Q1_ISC2015();

Scanner sc = new Scanner(System.in);

System.out.print("Enter a value of 'm' from 100 to 10000 : ");

int m = sc.nextInt();

System.out.print("Enter a value of n from 1 to 99 : ");

int n = sc.nextInt();

if(m<100 || m>10000 || n<1 || n>99)

System.out.println("Invalid Input");

else

long i = (long)m; while(ob.sumDig(i)!=n)


i=i+1;

System.out.println("The required number = "+i);

System.out.println("Total number of digits = "+ob.countDig(i));

}}}

ALGORITHM-:
Start
Checking for the validity of the inputs
Display an appropriate message for an invalid input
Accepting the numbers M and N from the user
Printing the smallest required number whose sum of all its digits is equal to N
Stop

Q12) 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.*;

class BoxPacking_ISC2017

{public static void main(String args[])

{Scanner sc = new Scanner(System.in);

System.out.print("Enter number of boxes to be packed : ");

int N = sc.nextInt();

if(N<1 || N > 1000)

{System.out.println("INVALID INPUT");

else

{int cart[] = {48, 24, 12, 6};

int copy = N;

int totalCart = 0,count = 0;

System.out.println("OUTPUT :");

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

{count = N / cart[i];
if(count!=0)

{System.out.println("\t"+cart[i]+"\tx\t"+count+"\t= "+cart[i]*count);

totalCart = totalCart + count;

N = N % cart[i];}

if(N>0)

{System.out.println("\tRemaining Boxes "+N+" x 1 = "+N);

totalCart = totalCart + 1; }

else

{ System.out.println("\tRemaining Boxes\t\t= 0");}

System.out.println("\tTotal number of boxes = "+copy);

System.out.println("\tTotal number of cartons = "+totalCart);

}
ALGORITHM-:
Start
Designing a program to accept the number of boxes to be packed (N) by the user
Displaying the break-up of the cartons used in descending order of capacity
Checking that preference should be given to the highest capacity available
Checking weather boxes left are less than 6
If true an extra carton of capacity 6 is added
Printing remaining boxes
Printing total number of boxes
Printing total number of cartons used
Stop

Q13The 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.*;

class QuizResult_ISC2017

{ char A[][],K[];
int S[],n;

void input()

{Scanner sc = new Scanner(System.in);

System.out.print("Enter number of participants : ");

n = sc.nextInt();

if(n<4 || n>10)

{System.out.println("INPUT SIZE OUT OF RANGE");

System.exit(0);

A = new char[n][5]; // Array to store the answers of every participants

K = new char[5]; // Array to store answer key

S = new int[n]; // Array to store score of every participant

System.out.println("\n* Enter answer of each participant row-wise in a single line


*\n");

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

{System.out.print("Participant "+(i+1)+" : ");

for(int j=0; j<5; j++)

{ A[i][j] = sc.next().charAt(0); }

System.out.print("\nEnter Answer Key : ");

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

{K[i] = sc.next().charAt(0);

void CalcScore() // Function to calculate score of every participant

{ for(int i = 0; i<n; i++)

{ S[i] = 0;
for(int j=0; j<5; j++)

{ if(A[i][j] == K[j]) // Checking if Answer of the participants match with the


key or not

{ S[i]++;

}}

void printScore()

{ int max = 0;

System.out.println("\nSCORES : ");

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

{ System.out.println("\tParticipant "+(i+1)+" = "+S[i]);

if(S[i]>max)

{ max = S[i]; // Storing the Highest Score

System.out.println();

System.out.println("\tHighest Score : "+max);

System.out.println("\tHighest Scorers : ");

for(int i = 0; i<n; i++) // Printing all those participant number who got highest
score

{ if(S[i] == max)

{ System.out.println("\t\t\tParticipant "+(i+1));

public static void main(String args[])

{ QuizResult_ISC2017 ob = new QuizResult_ISC2017();


ob.input(); ob.CalcScore();ob.printScore();

}}
ALGORITHM-:
Start
Designing a program to accept the number of participants N
Checking that N must be greater than 3 and less than 11.
Creating a double dimensional array of size (Nx5)
Storing the answers of each participant row-wise.
Calculating the marks for each participant by matching the correct answer stored in array
Creating main function
Creating object of class
Displaying the scores for each participant and also the participant(s) having the highest
score.
Stop

Q14) Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13 places’). It is a simple
letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other
characters remaining unchanged.

Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100.

Encrypt the text if valid as per the Caesar Cipher.

import java.util.*;
class CaesarCipher_ISC2017
{
void rot13(String w)
{
char ch;
int a = 0;
String ans = "";
for(int i = 0; i<w.length(); i++)
{
ch = w.charAt(i);
if(Character.isLetter(ch))
{
a = ch + 13;

if((Character.isUpperCase(ch) && a>90) || (Character.isLowerCase(ch)


&& a>122))
{
a = a - 26;
}
ch = (char)a;
}
ans = ans + ch;
}
System.out.println("OUTPUT : The cipher text is :\n"+ans);
}

public static void main(String args[])


{
CaesarCipher_ISC2017 ob = new CaesarCipher_ISC2017();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s = sc.nextLine();
int L = s.length();
if(L<4 || L>99)
{
System.out.println("INVALID LENGTH");
}
else
{
ob.rot13(s);}}}
ALGORITHM-:
Start
Designing a program to accept plain text of length ‘l’
Checking that ‘l’ must be greater than 3 and less than 100.
Creating a function to check validity of text and converting it into cipher code
Displaying the coded text
Displaying appropriate message If the text is invalid
Stop

Q15) Write a program to accept a sentence which may be terminated by either’.’, ‘?’or’!’ only. The words may be
separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

(a) Find the number of words beginning and ending with a vowel.

(b) Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they
occur in the sentence.

import java.util.*;
class ISC2016_Q3
{
boolean isVowel(String w) // Function to check if a word begins and ends with a
vowel or not
{
int l = w.length();
char ch1 = w.charAt(0); // Storing the first character
char ch2 = w.charAt(l-1); // Storing the last character
if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') &&
(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U'))
{
return true;
}
else
{
return false;
}
}

public static void main(String args[])


{
ISC2016_Q3 ob = new ISC2016_Q3();
Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence : ");


String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
char last = s.charAt(l-1); // Extracting the last character

/* Checking whether the sentence ends with '.' or '?' or not */


if(last != '.' && last != '?' && last != '!')
{
System.out.println("Invalid Input. End a sentence with either '.', '?' or
'!' only");
}
else
{
StringTokenizer str = new StringTokenizer(s," .?!");
int x = str.countTokens();
int c = 0;
String w = "", a = "", b = "";

for(int i=1; i<=x; i++)


{
w = str.nextToken(); // Extracting words and saving them in w

if(ob.isVowel(w))
{
c++; // Counting all words beginning and ending with a vowel
a = a + w + " "; // Saving all words beginning and ending with a
vowel in variable 'a'
}
else
b = b + w + " "; // Saving all other words in variable 'b'
}
System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND ENDING WITH A
VOWEL = " + c);
System.out.println(a+b);

}
}
}
ALGORITHM-:
Start
Designing a class
accepting the sentence from user
Find the number of words beginning and ending with a vowel.
Place the words which begin and end with a vowel at the beginning, followed by the
remaining words as they occur in the sentence.
Stop

Q16) Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less
than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:

(a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in
the matrix.
(b) Calculate the sum of both the diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their
sum.

import java.util.*;
class SortNonBoundary_ISC2016
{
int A[][],B[],m,n;

void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
m=sc.nextInt();
if(m<4 || m>10)
{
System.out.println("Invalid Range");
System.exit(0);
}
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n];

System.out.println("Enter the elements of the Matrix : ");


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a value : ");
A[i][j]=sc.nextInt();
}
}
}
}

void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}
}
}
}

void sortArray()
{
int c = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(B[i]>B[j])
{
c = B[i];
B[i] = B[j];
B[j] = c;
}
}
}
}

void printArray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}

void printDiagonal()
{
int sum = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j || (i+j)==m-1)
{
System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println("Sum of the Diagonal Elements : "+sum);
}

public static void main(String args[])


{
SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();
ob.input();
System.out.println("*********************");
System.out.println("The original matrix:");
System.out.println("*********************");
ob.printArray();
ob.convert(1);
ob.sortArray();
ob.convert(2);
System.out.println("*********************");
System.out.println("The Rearranged matrix:");
System.out.println("*********************");
ob.printArray();
System.out.println("*********************");
System.out.println("The Diagonal Elements:");
System.out.println("*********************");
ob.printDiagonal();
}

ALGORITHM-:
Start
The class SortNonBoundary_ISC2016 inputs a square matrix and
sorts the non-boundary elements in ascending order
Function for taking all the necessary inputs
Array to store Non-Boundary Elements
The below function stores Non-Boundary elements
from array A[][] to array B[] if s = 1
else stores the Non-Boundary elements in array A[][] from array B[]
Function for sorting Non-Boundary elements stored in array B[]
Function for printing the array A[][]
Function for printing the diagonal elements and their sum
Printing the original array
Storing Non-Boundary elements to a 1-D array
Sorting the 1-D array (i.e. Non-Diagonal Elements)
Storing the sorted Non-Boundary elements back to original 2-D array
Printing the rearranged array
Printing the diagonal elements and their sum
Stop
Q17) 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 2 < N < 9 and display them in vertical order, side by side with a horizontal tab (i.e. eight spaces).

import java.util.*;
public class ISC2018Q3
{
public static void main()
{
Scanner sc = new Scanner(System.in);
String ar[];
int n, i, j;
System.out.println("Enter the number of names: ");
n = sc.nextInt();
ar = new String[n];
System.out.println("Enter the names: ");
for(i=0;i<n;i++)
{
ar[i] = sc.nextLine();
}
int max = 0;

for(i=0;i<n;i++)
{
if(max<ar[i].length())
max = ar[i].length();
}
System.out.println("OUTPUT:" );
for(i=0;i<max;i++)
{
for(j=0;j<n;j++)
{
if(i<ar[j].length())
System.out.print(ar[j].charAt(i)+"\t");
else
System.out.print("\t");
}
System.out.println();
}
}
}
ALGORITHM-:
Start
Designing a class
Accepting the names of the teams participating in a competition
Displaying them on a banner vertically
accommodating as many teams as possible in a single banner.
Designing a program to accept the names of N teams,
Checking weather 2 < N < 9
Displaying them in vertical order, side by side with a horizontal tab (i.e. eight spaces).
Stop

Q18)A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.Note: All
even integer numbers greater than 4 are Goldbach numbers. Example: 6 = 3 + 3
10 = 3 + 7
10 = 5 + 5 Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7, 5 and 5.Write
a program to accept an even integer ‘N’ where N > 9 and N < 50. Find all the odd prime pairs whose sum is equal to
the number ‘N’

import java.util.*;
public class Goldbach
{
boolean isPrime(int n)
{
if(n<=1)
return false;

int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
return false;
}
return true;
}

void print(int n)
{
int i, j;
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{ if(isPrime(i)&&isPrime(j)&&i+j==n)
System.out.println(i+", "+j);
}
}
}
public static void main()
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the limit: ");
n = sc.nextInt();
if(n%2==1)
{System.out.println("INVALID INPUT. NUMBER IS ODD.");
System.exit(0);
}
if(n<=9||n>=50)
{System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
System.exit(0);
}
Goldbach ob = new Goldbach();
System.out.println("Prime Pairs are: ");
ob.print(n);
}
}
ALGORITHM-:
Start
Designing a class
Accepting the number from user
creating a program to accept an even integer ‘N’
cheaking weather N > 9 and N < 50.
Finding all the odd prime pairs whose sum is equal to the number ‘N’
Displaying them
Stop

Q19) Write a program to declare a matrix A[ ] [ ] of order (M xN) where ‘M’ is the number of rows and ‘N’ is the
number of columns such that the values of both ‘M’ and ‘N’ must be greater than 2 and less than 10. Allow the user to
input integers into this matrix. Perform the following tasks on the matrix:
(a) Display the original matrix.
(b) Sort each row of the matrix in ascending order using any standard sorting technique.
(c) Display the changed matrix after sorting each row.
import java.util.*;
public class TwoDArr
{
void sort(int a[])
{
int i, j, n = a.length, tmp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}}}}

void display(int a[][])


{
int i, j;
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}}
void sort2D(int a[][])
{ int i, j;
for(i=0;i<a.length;i++)
{
sort(a[i]); }
}
}
ALGORITHM-:
Start
Designing a class
Creating a program to declare a matrix A[ ] [ ] of order (M xN)
Checking that the values of both ‘M’ and ‘N’ must be greater than 2 and less than 10.
Allowing the user to input integers into this matrix.
Displaying the original matrix.
Sorting each row of the matrix in ascending order using any standard sorting technique.
Displaying the changed matrix after sorting each row
Stop.

Q20) A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is
removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is
repeated until the original number is reached again.
A number is said to be prime if it has only two factors 1 and itself
Create a program to find weather given number is circular prime or not

import java.util.*;
public class Circular_Prime
{
public static void main()
{
Scanner in=new Scanner(System.in);
int n,d,i,r,c,j,f=0,pr=0,t;
System.out.print("Enter your number : ");
n=in.nextInt();
t=n;
d=Integer.toString(n).length();
for(i=0;i<d;i++)
{
System.out.println(n);
r=n%(int)(Math.pow(10,d-1));
c=r*10+n/(int)(Math.pow(10,d-1));
for(j=1;j<=c;j++)
{
if(c%j==0)
f++;
}
if(f==2)
{
pr++;
f=0;
}
n=c;
}
if(pr==d)
System.out.println(t+" IS A CIRCULAR PRIME");
else
System.out.println(t+" IS NOT A CIRCULAR PRIME");
}
}
ALGORITHM-:
Start
Designing a class
Creating a program accept value from user
Checking that the number is still prime after first loop
Checking number of factors are 2
Displaying appropriate message
Stop

You might also like