Final Exam
A Prime-Adam integer is a positive integer (without leading zeroes) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself. Example: 2, 3, 5, 7, etc.
Adam number: The square of a number and the square of its reverse are reverse to each other. Example: If n = 13 and reverse of ‘n’ is 31, then, 132 = 169, and 312 = 961 which is reverse of 169. Thus, 13 is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1:
INPUT:
m = 5
n = 100
OUTPUT:
The Prime-Adam integers are:
11, 13, 31
Frequency of Prime-Adam integers is: 3
Example 2:
INPUT:
m = 100
n = 200
OUTPUT:
The Prime-Adam integers are:
101, 103, 113
Frequency of Prime-Adam integers is: 3
Example 3:
INPUT:
m = 50
n = 70
OUTPUT:
The Prime-Adam integers are:
NIL
Frequency of Prime-Adam integers is: 0
Example 4:
INPUT:
m = 700
n = 450
OUTPUT:
Invalid Input.
import java.util.Scanner;
class PrimeAdam
{
boolean isPrime(int n)
{
int c=0;
for(int i=1; i<=n; i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
boolean isAdam(int n)
{
int rev = reverse(n);
int sq_of_n = n*n;
int sq_of_rev = rev*rev;
if(sq_of_n == reverse(sq_of_rev))
return true;
else
return false;
}
int reverse(int n)
{
int rev=0;
while(n!=0)
{
rev = rev*10+n%10;
n/=10;
}
return rev;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter value of m and n");
System.out.print("m = ");
int m = in.nextInt();
System.out.print("n = ");
int n = in.nextInt();
if(m>=0 && n>0 && m<n)
{
PrimeAdam obj = new PrimeAdam();
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
int freq=0;
for(int i=m; i<=n; i++)
{
if(obj.isPrime(i) && obj.isAdam(i))
{
freq++;
System.out.print(i + " ");
}
}
if(freq==0)
System.out.print("NIL");
System.out.println("\nFREQUENCY OF PRIME-ADAM INTEGERS IS: " +freq);
}
else
{
System.out.println("INVALID INPUT");
}
}
}
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:
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.
Test your program with the sample data and some random data:
Example 1
import java.io.*;
class ISCprac2016Q03
{
public static void main(String arg[])throws IOException
{
int l,i,n, p, nowwv;
boolean flag = true;
char ch, fc, lc, lastChar;
String str, prefix = "", suffix = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence : ");
str = br.readLine();
l = str.length();
lastChar = str.charAt(l-1); // Extract the last character
if(lastChar != '.' && lastChar != '?' && lastChar != '!')
System.out.println("INVALID INPUT");
else{
//Pointer to the first character of each word
p = 0;
// store no of words starting and ending with a vowel
nowwv = 0;
for(i = 0; i < l; i++){
ch = str.charAt(i);
if(ch == ' ' || ch == '?' || ch == '!' || ch == '.'){
//Check if first and last character of the word is a vowel
if(isVowel(str.charAt(p)) && isVowel(str.charAt(i-1))){
//Add words starting and ending with a vowel
prefix += str.substring(p,i) + " ";
nowwv++;
}else{
//Add words that doesn't start and end with a vowel
suffix += str.substring(p,i) + " ";
}
p = i + 1;
}//if
}//for loop
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + nowwv);
System.out.println(prefix + suffix);
}//else
}//end of main
//Return true if 'ch' is a vowel else returns false
private static boolean isVowel(char ch){
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
return true;
else
return false;
}//end of isVowel
}//end of class
Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words are to be separated by a single blank space and are in UPPER CASE.
Perform the following tasks:
- Check for the validity of the accepted sentence only for the terminating character.
- Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically.
- Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
Example 2:
INPUT:
SELF HELP IS THE BEST HELP.
OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF
Example 3:
INPUT:
BE KIND TO OTHERS.
OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS
Example 4:
INPUT:
NOTHING IS IMPOSSIBLE#
OUTPUT:
INVALID INPUT
import java.util.*;
public class StringCheck
{
public static String sortString(String ipStr) {
StringTokenizer st = new StringTokenizer(ipStr);
int wordCount = st.countTokens();
String strArr[] = new String[wordCount];
for (int i = 0; i < wordCount; i++) {
strArr[i] = st.nextToken();
}
for (int i = 0; i < wordCount - 1; i++) {
for (int j = 0; j < wordCount - i - 1; j++) {
if (strArr[j].length() > strArr[j + 1].length()) {
String t = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = t;
}
if (strArr[j].length() == strArr[j + 1].length()
&&(strArr[j].compareTo(strArr[j+1]) > 0))
{
String t = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = t;
}
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < wordCount; i++) {
sb.append(strArr[i]);
sb.append(" ");
}
/*
* trim will remove the extra space added
* to the end of the string in the loop above
*/
return sb.toString().trim();
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int len = str.length();
System.out.println();
if (str.charAt(len - 1) != '.'
&& str.charAt(len - 1) != '?'
&& str.charAt(len - 1) != '!') {
System.out.println("INVALID INPUT");
return;
}
/*
* str.substring(0, len - 1) removes the
* '.', '?', '!' at the end of the string
*/
String sortedStr = sortString(str.substring(0, len - 1));
System.out.println(str);
System.out.println(sortedStr);
}
}
Write a program to declare a matrix a[][] of order (M × N) where ‘M’ is the number of rows and ‘N’ is the number of columns such that the value of ‘M’ must be greater than 0 and less than 10 and the value of ‘N’ must be greater than 2 and less than 6. Allow the user to input digits (0 – 7) only at each location, such that each row represents an octal number.
Example:
2 3 1 (decimal equivalent of 1st row = 153 i.e. 2 × 82 + 3 × 81 + 1 × 80)
4 0 5 (decimal equivalent of 2nd row = 261 i.e. 4 × 82 + 0 × 81 + 5 × 80)
1 5 6 (decimal equivalent of 3rd row = 110 i.e. 1 × 82 + 5 × 81 + 6 × 80)
Perform the following tasks on the matrix:
a) Display the original matrix.
b) Calculate the decimal equivalent for each row and display as per the format given below.
Test your program for the following data and some random data:
Example 1:
INPUT:
M = 1
N = 3
Enter elements for row 1: 1 4 4
OUTPUT:
Filled Matrix:
1 4 4
Decimal Equivalent:
100
Example 2:
INPUT:
M = 3
N = 4
Enter elements for row 1: 1 1 3 7
Enter elements for row 2: 2 1 0 6
Enter elements for row 3: 0 2 4 5
OUTPUT:
Filled Matrix:
1 1 3 7
2 1 0 6
0 2 4 5
Decimal Equivalent:
607
1094
165
Example 3:
INPUT:
M = 3
N = 3
Enter elements for row 1: 2 4 8
OUTPUT:
Invalid Input.
Example 4:
INPUT:
M = 4
N = 6
OUTPUT:
Out of range.
import java.io.*;
class ToDecimal{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][];
int m;
int n;
System.out.print("M = ");
m = Integer.parseInt(br.readLine());
System.out.print("N = ");
n = Integer.parseInt(br.readLine());
if(m < 1 || m > 9 || n < 3 || n > 5){
System.out.println("Out of range.");
return;
}
a = new int[m][n];
for(int i = 0; i < m; i++){
System.out.println("Enter elements for row " + (i + 1) + ":");
for(int j = 0; j < n; j++){
a[i][j] = Integer.parseInt(br.readLine());
if(a[i][j] < 0 || a[i][j] > 7){
System.out.println("Invalid Input.");
return;
}
}
}
System.out.println("Filled Matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Decimal Equivalent:");
for(int i = 0; i < m; i++){
int d = 0;
int p = n - 1;
for(int j = 0; j < n; j++){
d += a[i][j] * (int)Math.pow(8, p);
p--;
}
System.out.println(d);
}
}
}
Encryption is a technique of coding messages to maintain their secrecy. A String array of size 'n' where 'n' is greater than 1 and less than 10, stores single sentences (each sentence ends with a full stop) in each row of the array.
Write a program to accept the size of the array.
Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise. Change the sentence of the odd rows with an encryption of two characters ahead of the original character.
Also change the sentence of the even rows by storing the sentence in reverse order. Display the encrypted sentences as per the sample data given below.
Test your program on the sample data and some random data.
import java.io.*;
class ISCprac2011q02{
public static void main(String args[])
throws IOException{
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
int nos;
System.out.print("Enter number of sentences : ");
nos=Integer.parseInt(br.readLine());
if(nos<1 || nos>=10)
System.out.println("\nInvalid Entry");
else{
int i,j,p,l;
String s[]=new String[nos];
System.out.print("\nEnter "+nos+" sentences : ");
for(i=0;i< nos;i++)
s[i]=(br.readLine()).toUpperCase();
for(i=0;i< nos;i++)
{
String t;
s[i]=" "+s[i];// add a blank space before each sentence
l=s[i].length();
if(i%2==0){
t="";
for(j=0;j< l;j++){
// store the ASCII code of the character
int ch=s[i].charAt(j);
if(ch!=32 && ch!='.'){
ch=ch+2;//shift the letter two spaces
if(ch>90)//to maintain cyclic order
ch=ch-26;// subtract 26
}
//convert to character and add to a temporary string
t=t+(char)ch;
}
s[i]=t.trim();// remove leading or trailing spaces
}else{
t="";
p=l-1;
for(j=l-1;j>=0;j--){
//reverse loop to start extraction of words
//from last to first
char ch=s[i].charAt(j);
if(ch==' '){
t=t+s[i].substring(j+1,p)+" ";
// add the extracted word and a space
p=j;
}
}
t=t+".";
s[i]=t;
}
}
System.out.println("\nOUTPUT:");
for(i=0;i< nos;i++)
System.out.print(s[i]);
}
}
}
Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is valid or not.
If it is valid, display "VALID DATE", also compute and display the day number of the year for the date of birth. If it is invalid, display "INVALID DATE" and then terminate the program.
Testing of the program
Example 1
import java.util.*;
class ISCprac2011q03{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your date od birth in
dd mm yyyy format : ");
int d=scan.nextInt();
int m=scan.nextInt();
int y=scan.nextInt();
int dom[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%400==0 || (y%100!=0 && y%4==0))
dom[1]=29;
if(d<=dom[m-1])
{
System.out.print("VALID DATE ");
int i,s=0;
for(i=0;i< m-1;i++)
s=s+dom[i];
s+=d;
System.out.print(s);
}else{
System.out.print("INVALID DATE");
}
}
}