0% found this document useful (0 votes)
27 views63 pages

Computer Project!!

Uploaded by

shloksengupta05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views63 pages

Computer Project!!

Uploaded by

shloksengupta05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Q1) 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:
1. Sort the non-boundary elements in ascending order using any standard
sorting technique and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal
elements of the rearranged matrix with their sum.
Test your program with the sample data and some random data:
INPUT :M = 4

ORIGINAL MATRIX

9 2 1 5

8 13 8 4

15 6 3 11

7 12 23 8

REARRANGED MATRIX

9 2 1 5

8 3 6 4

15 8 13 11

7 12 23 8

ORIGINAL MATRIX

9 2 1 5

8 13 8 4

15 6 3 11
7 12 23 8

DIAGONAL ELEMENTS:

9 5

3 6

8 13

7 8

SUM OF THE DIAGONAL ELEMENTS = 59

ALGORITHM:
Step 1: Start.

Step 2: Input M.

Step 3: Input the range of M from 3 to 10.

Step 4: Input the elements

Step 5: Print the original matrix.

Step 6: Store the boundary elements.

Step 7: Sort the elements in ascending order.

Step 8: Print the rearranged matrix.

Step 9: Print the diagonal of rearranged matrix.

Step10: Stop.
PROGRAM:
import java.util.*;

class Q1

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows for a square matrix


(between 3 and 10): ");

int M = sc.nextInt();

if(M <=3 || M >=10){

System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");

else

int a[][] = new int[M][M];

int b[] = new int[M*M];

int i, j, c, t;
System.out.println("Enter " + (M*M) + " elements");

for(i = 0; i < M; i++){

for(j = 0; j < M; j++){

a[i][j] = sc.nextInt();

}//loop j

}//loop i

System.out.println("\nORIGINAL MATRIX: ");

c = 0;

for(i = 0; i < M; i++){

for(j = 0; j < M; j++){

//print the elements of the original matrix

System.out.print(a[i][j]+ " ");

//store the non-boundary elements in an array

if(i != 0 && j != 0 && i != M-1 && j != M - 1)

b[c++] = a[i][j];

}//loop j

System.out.println();

}//loop i

//sort the non-boundary elements in ascending order

for(i = 0; i < c; i++){

for(j = i+1; j < c; j++){

if(b[i] > b[j]){

t = b[i];
b[i] = b[j];

b[j] = t;

}//loop j

}//loop i

c = 0;

for(i = 0; i < M; i++){

for(j = 0; j < M; j++){

//store the sorted elements at the non-boundary indeces

if(i != 0 && j != 0 && i != M-1 && j != M - 1)

a[i][j] = b[c++];

}//loop j

}//loop i

System.out.println("\nREARRANGED MATRIX: ");

//Display the rearranged matrix

for(i = 0; i < M; i++){

for(j = 0; j < M; j++){

System.out.print(a[i][j]+ " ");

}//loop j

System.out.println();

}//loop i

System.out.println("\nDIAGONAL ELEMENTS: ");

//Display the rearranged matrix's diagonal elements


for(i = 0; i < M; i++){

for(j = 0; j < M; j++){

if( i == j || i+j == M-1)

System.out.print(a[i][j]+ " ");

else

System.out.print(" ");

}//loop j

System.out.println();

}//loop i

}//end of main

}//end of class

VARIABLE DECLARATION:

SL.NO. NAME TYPE PURPOSE

1. M int to input the range.

2. a[][] int to input the array.

3. b[] int to temporarily store the


elements.

4. i,j int loop variables.

5. c int for counter variable.

6. t int for temporary storage of


variable for sorting.
Q2) 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.)

Test your program with the following data and some random data:

Example 1

INPUT:
N = 726

OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16

Example 2

INPUT:
N = 140

OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3: N= 4296

INVALID INPUT

ALGORITHM:
Step 1: Start.

Step 2: Enter the number of boxes.

Step 3: Enter the cartoon sizes 48,24,12,6.

Step 4: Set the loop

for (int i = 0; i < cartonSizes.length; i++)

Step 5: Arrange the cartoons in given sizes.

Step 6: Display the conditions of remaining boxes.

Step 7: Display the output.

Step 8:Stop.
PROGRAM:
import java.util.*;

public class Q2

{//class starts

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) // set the range

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

return;

int cartonSizes[] = {48, 24, 12, 6};//set the size of the cartoons

int total = 0;

int t = n;

for (int i = 0; i < cartonSizes.length; i++) {


int cartonCount = t / cartonSizes[i];//distribute the boxes according to
the sizes

t = t % cartonSizes[i];

total += cartonCount;

if (cartonCount != 0) {

System.out.println(cartonSizes[i] + " * " + cartonCount +

" = " + (cartonSizes[i] * cartonCount));

/*

* This if check is for the case when

* boxes left are less than 6. We need

* one more carton of capacity 6 in this

* case so total is incremented by 1.

*/

if (t != 0)

System.out.println("Remaining boxes=” + " * 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);

}//class end

OUTPUT:

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. n int to input the size.

2. cartonSizes[ int to input the sizes of cartoons.


]

3. total int to input the total.

4. i int loop variable.

5. t int to store the size temporarily.


6. cartonCount int to count the cartoons.

q3) 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 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:

1. Display the original matrix.


2. Sort each row of the matrix in ascending order using any standard
sorting technique.
3. Display the changed matrix after sorting each row.

Test your program for the following data and some random data:
Example 1
INPUT:
M=4
N=3
AFTER ENTERING MATRIX ELEMENTS
OUTPUT:
ORIGINAL MATRIX

11 -2 3

5 16 7

9 0 4

3 1 8 MATRIX AFTER SORTING ROWS:

-2 3 11

5 7 16

0 4 9

1 3 8
Example 2
INPUT:
M=3
N=3
OUTPUT:
ORIGINAL MATRIX:

22 5 19

5 7 16

0 4 9
MATRIX AFTER SORTING ROWS

5 9 22

7 12 36

6 9 13
Example 3
INPUT:
M = 11
N=5
OUTPUT:
MATRIX SIZE OUT OF RANGE.

ALGORITHM:
Step 1: Start.

Step 2: Enter the value of m and n;

Step 3: Set conditions for size of matrix.

Step 4: Input the elements of array.

Step 5: Display the original matrix.


Step 6: Sort the array row wise in ascending order.

Step 7: Display the sorted array.

Step 8: Stop.

PROGRAM:
import java.util.*;

public class Q3

{//class starts

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("ENTER THE VALUE OF M: ");

int m = in.nextInt();//Enter size of rows

System.out.print("ENTER THE VALUE OF N: ");

int n = in.nextInt();//Enter size of column

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:");//Input elements

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();

System.out.println("ORIGINAL MATRIX");//Print 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++) { //Sort in ascending order

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");//Print the sorted


matrix

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

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

System.out.print(a[i][j] + " ");

System.out.println();

}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. n int to input the size.

2. m int to input the size.

3. a[][] int to store the array elements.

4. i,j,k int loop variables.

5. t int to store the elements


temporarily.
Q4) Write a program to declare a single-dimensional array a[] and a square
matrix b[][] of size N, where N > 2 and N < 10. Allow the user to input positive
integers into the single dimensional array.
PeRFORM the following tasks on the matrix:

1. Sort the elements of the single-dimensional array in ascending order


using any standard sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:

1 2 5 8

1 2 5 1

1 2 1 2

1 1 2 5

3. Display the filled matrix in the above format.

Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7

1 3 1

1 1 3
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N=5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX:
2 5 6 10 23

2 5 6 10 2

2 5 2 5 6

2 2 5 6 10

ALGORITHM:
Step 1: Start.

Step 2: Enter the value of N.


Step 3: Set the range for N.
Step 4: Enter the elements of array.
Step 5: Sort the array.
Step 6: Print the sorted array
Step 7: Set the loop and give the condition
if(j<N-i)
b[i][j]=a[j];
else
b[i][j]=a[j-(N-i)];
Step 8: Display the new matrix.

Step 9: Stop.

PROGRAM:
import java.util.*;
class Q4
{//class starts
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter");
int N=sc.nextInt();
if(N<2||N>10)//set the range
{
System.out.println("MATRIX SIZE OUT OF RANGE");
}
else
{
int a[]=new int[1000];
int b[][]=new int[100][100];
System.out.println("ENTER ELEMENT OF SINGLE DIMENSIONAL ARRAY:");
for(int i=0;i<N;i++)
{
a[i]=sc.nextInt();//input elements
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i-1;j++)//sort the array
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
///for printing sorted array....
System.out.println("SORTED ARRAY :");
for(int i=0;i<N;i++)
{
System.out.println(a[i]);
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(j<N-i)//fill the array
b[i][j]=a[j];
else
b[i][j]=a[j-(N-i)];
}
}
System.out.println("FILLED MATRIX");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(b[i][j]);//display the array
}
System.out.println();
}
}
}
}//class ends

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. N int to input the size.

2. b[][] int to store the elements in double


dimensional array.

3. a[] int to store the array elements.

4. i,j int loop variables.

5. t int to store the elements


temporarily.

Q5) Write a program to declare a matrix A[][] of order (M x 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:
Perform the following tasks on the matrix:

1. Display the original matrix.


2. 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

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 DECIMAL EQUIVALENT

1 1 3 7 607

2 1 0 6 1094

0 2 4 5 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

ALGORITHM:
Step 1: Start.

Step 2: Enter the value of m to input the rows.


Step 3: Enter the value of n to input the columns.
Step 4: Set the range of m and n.
Step 5: Enter the elements of the matrix.
Step 6: Set the condition for elements for matrix.
(a[i][j] < 0 || a[i][j] > 7)
Step 7: Calculate the decimal equivalent of each elements and add the decimal
equivalent.
decNum += a[i][j] * Math.pow(8, n - j - 1 );

Step 8: Print the output

Step 9: Stop
PROGRAM:
import java.util.*;

public class Q5

{//class starts

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the number of rows (M): ");

int m = in.nextInt();//input rows

System.out.print("Enter the number of columns (N): ");

int n = in.nextInt();//input columns

if (m <= 0 || m >= 10 || n <= 2 || n >= 6)//Set the range for rows and
columns

System.out.println("OUT OF RANGE");

return;

int 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] = in.nextInt();//enter the elements

if (a[i][j] < 0 || a[i][j] > 7) {

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

return;
}

System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");

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

int decNum = 0;

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

decNum += a[i][j] * Math.pow(8, n - j - 1 );//find and add the decimal


equivalent

System.out.print(a[i][j] + " ");//print the matrix

System.out.print("\t\t" + decNum);

System.out.println();

}//class ends

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. m int to input the rows.

2. n int to input the columns.

3. a[][] int to store the array elements.


4. i,j int loop variables.

5. decNum int to store the decimal equivalent


of the elements of array.

Q6) Write a program to accept a sentence which may be terminated by either


‘.’ ‘?’ or ‘!’ only. Any other character may be ignored. The words may be
separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

(a) Accept the sentence and reduce all the extra blank space between two
words to
a single blank space.
(b) Accept a word from the user which is part of the sentence along with its
position number and delete the word and display the sentence.

Test your program with the sample data and some random data:

Example 1

INPUT:

A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.

WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6

OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

Example 2

INPUT: AS YOU SOW, SO YOU REAP.


WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4

OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3

INPUT:

STUDY WELL ##.

OUTPUT:

INVALID INPUT.

ALGORITHM:
Step 1: Start.

Step 2: Enter the string.


Step 3: Check the last character of the string.
Step 4: Enter the word to be deleted.
Step 5: Enter the position of the word in the sentence.
Step 6: Store of the character of the string in ch.
Step 7: Give the conditions.
if(ch==' '||ch=='.'||ch=='?'||ch=='!')

if(wd1.equals(wd)==true && (count)==pos)

Step 8: Store the output in another string.

Step 9: Stop.
PROGRAM:
import java.util.*;

public class Q6

{//class starts

public static void main(String args[])

int i=0,count=1,len=0,pos=0;

char ch=' ',last=' ';

String sen="",newsen="",wd="",wd1="";

Scanner sc=new Scanner(System.in);

System.out.print("ENTER A SENTENCE: ");

sen =sc.nextLine();//Enter the string

len = sen.length();//find the length

last = sen.charAt(len - 1);//find the last character

if(last != '.' && last != '?' && last != '!')

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

return;

sen = sen.toUpperCase();

System.out.print("WORD TO BE DELETED: ");

wd = sc.next();//inputthe word to be deleted


wd = wd.toUpperCase();

System.out.print("WORD POSITION IN THE SENTENCE: ");

pos = sc.nextInt();//enter the position

for(i=0;i< len;i++)

ch=sen.charAt(i);

if(ch==' '||ch=='.'||ch=='?'||ch=='!')

if(wd1.equals(wd)==true && (count)==pos)//give the conditions

/*do nothing*/

else

newsen=newsen+wd1+ch;//store in a new string

wd1="";

count++;

else

wd1=wd1+ch;

}
}

System.out.println("NEW SENTENCE:"+newsen);

}//class end

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. sen String to input the string.

2. last String to store the last character.

3. len int to store the length.

4. i, int loop variables.

5. newsen String to store the new string.

6. count int counter variable.

7. pos int to end the position.

8. wd String to enter the word to be


deleted.

9. ch char to store the characters of the


string.

10. Wd1 String to temporarily store the string.

Q7) 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. Print an
error message if the input does not terminate with ‘.’ Or ‘?’. You can assume
that no word in the sentence exceeds 15 characters, so that you get a proper
formatted output. Perform the following tasks: i) Convert the first letter of
each word to uppercase. ii) Find the number of vowels and consonants in each
word and display them with proper heading along with the words. Test your
program with the following inputs:

Example 1:

INPUT: Intelligence plus character is education.

OUTPUT:
Intelligence Plus Character Is Education

Word Vowels Consonants


Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4

Example 2

INPUT: God is great.

OUTPUT:
God is Great

Word Vowels Consonants


God 1 2
Is 1 1
Great 2 3

Example 3
INPUT: All the best!

ALGORITHM:
Step 1: Start.

Step 2: Enter the string.


Step 3: Check if the sentence ends with either '.' or '?'".
Step 4: Store the characters of the string in ch.
Step 5: Store each word in p.
Step 6: Check that the string does not contain ‘.’ as terminator in the middle of
the string.
Step 7: Count the vowels present in the string.
Step 8: Otherwise count the consonants.

Step 9: Print the number of vowels and number of consonants.

Step 10: Stop.

PROGRAM:

import java.util.*;
class Q7
{//class starts
public static void main(String args[])
{
String p="";
int v=0,c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String n=sc.nextLine();//Enter the string
n=n+" ";
System.out.println("Word \t vowel \tconsonant");
int l=n.length();
if(n.charAt(l-2) != '.' && n.charAt(l-2) != '?')
{
System.out.println("Invalid Input. End a sentence with either '.' or '?'");
}
else
{
for(int i=0;i<l;i++)
{
char ch=n.charAt(i);
if(ch!=' ')
p=p+ch;
else
{
int k=p.length();

for(int j=0;j<k;j++)
{
char ch1=p.charAt(j);
if(ch1=='.')
break;
if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u')//check the vowels
{
v++;//count the vowels
}
else
c++;//count the consonant
}
System.out.println(p+"\t"+v+"\t"+c);//print the output
p="";
v=0;
c=0;
}
}
}
}
}//class ends

OUTPUT:

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. n String to input the string.

2. p String to store each word of string.

3. l int to store the length.

4. i, int loop variables.

5. c int to count the consonants.

6. k int to store the length of each


word.

7. ch char to store the character of the


string.

8. ch1 char to store the characters of each


word.

9. v int to count the vowels.

Q8) 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:

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


2. 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
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE
TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT

ALGORITHM:
Step 1: Start.

Step 2: Enter the string.


Step 3: Check if the sentence ends with either '.', '?', ‘!’.
Step 4: Store the words using substring.
Step 5: Store the words in StringTokenizer.
Step 6: Store the first and last character of each.
Step 7: Set the condition
if(“AEIOU”.indexOf(last)>=0&&(“AEIOU”.indexOf(ch)>=0))

Step 8: Store the output in a new string.


Step 9: Increase the counter value.

Step 10: Print the output.

Step 11:Stop.

PROGRAM:
import java.util.*;

public class Q8

{//class starts

public static void main(String args[])

Scanner in = new Scanner(System.in);

int n,i,t;

String s1=” “,s2=” “,s3=” “,s4=” “;

Char ch;

System.out.println("ENTER THE SENTENCE:");

String str = in.nextLine().trim().toUpperCase();//Enter the string

int len = str.length();

char last = str.charAt(len - 1);


if (last != '.' && last != '?'&& last != '!') //give the conditions for termination

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

return;

else

str = str.substring(0, len - 1);

StringTokenizer st = new StringTokenizer(str);//pick the word in


StringTokenizer

n=s.countToken( );

t=0;

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

s1=s.nextToken();

last=s1.charAt(0);

ch=s1.charAt(s1.length( )-1);

if(“AEIOU”.indexOf(last)>=0&&(“AEIOU”.indexOf(ch)>=0))//give the
conditions for first and last character of each word

s2=s2+’ ‘+s1;

t++;//increase the counter

else
s3=s3+’ ‘+s1;

s4=s2+’ ‘+s3;

System.out.println("Number of word beginning and ending with a vowel


="+t);//print the output

System.out.println(“The new sentence:”);

System.out.println(s4);

}
}
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. str String to input the string.

2. last String to store the last character of


string.

3. len int to store the length.

4. i, int loop variables.

5. t int to count the consonants.

6. st int for StringTokenizer.

7. ch char to store the first and last


character of each word.

8. n int for countToken.

10. s1 String for nextToken.

11. s2,s3 String to store the new string.

12. s4 String to print the output.


Q9) 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.

A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m

↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕

N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/ X/x Y/y Z/z
w

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.

Test your program with the sample data and some random data:

Example 1:

INPUT: Hello! How are you?

OUTPUT: The cipher text is:

Uryyb? Ubj ner lbh?

Example 2:

INPUT : Encryption helps to secure data.

OUTPUT : The cipher text is:

Rapelcgvba urycf gb frpher qngn.

Example 3:
INPUT: You
OUTPUT : INVALID LENGTH

ALGORITHM:
Step 1: Start

Step 2: Enter the string.

Step 3: Enter the range.

Step 4: Set the i loop

for(int i = 0; i < s.length(); i++){

Step 5: Give the condition

if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))

Step 6: Or give the condition

if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))

Step 7: Increase or decrease the ASCII value by 13.

Step 8: Print the output.

Step 9: Stop.

PROGRAM:
import java.util.*;
class Q9

{// class starts

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter the string: ");

String s = in.nextLine();//enter the string

if((s.length() < 4 ||( s.length()) > 99)){//set the range

System.out.println("INVALID LENGTH");

return;

String c = "";

for(int i = 0; i < s.length(); i++){

char ch = s.charAt(i);

if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))//give the condition

c += (char)(ch + 13);

else if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))

c += (char)(ch - 13);

else

c += ch;

System.out.println("The cipher text is:");

System.out.println(c);//print the output


}

}//class ends

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. s String to input the string.

2. ch char to store the characters of


string.

3. c int to store the output

4. i int loop variables.

Q10) 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).
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
OUTPUT:
E R C
m o o
u a y
s d o
t
R e
o
l
s
Example 2
INPUT:
N=4
Team 1: Royal
Team 2: Mars
Team 3: De Rose
Team 4: Kings

OUTPUT:
R M D K
o a e i
y r n
a s R g
l o s
s
e
Example 3
INPUT:
N = 10
OUTPUT:
INVALID INPUT

ALGORITHM:
Step 1: Start.

Step 2: Enter the string.

Step 3: Input the range.

Step 4: Store the words in an array.

Step 5: Store the highest range length word un highLen.

Step 6: Set i loop

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

Step 7: Set j loop

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

Step 8: Print output.

Step 9: Stop.

PROGRAM:
import java.util.*;

public class Q10

{//class starts

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("ENTER THE VALUE OF N: ");

int n = in.nextInt();//enter the value of N


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();//enter the word

if (teams[i].length() > highLen)//give the condition

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");//print the output

else {

System.out.print(teams[j].charAt(i) + "\t");//print the output

}
}

System.out.println();

}//class ends

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. n int to input the value of N.

2. teams[] String to store the words in an array.

3. highLen int to store the maximum length.

4. i,j int loop variables.

5. len int to store the length of each


word.

Q11) 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:

Test your program with the sample data and some random data:

Example 1:

INPUT:

M=100

N=1000

OUTPUT: The prime palindrome integers are:

101,131,151,181,191,313,351,373,383,727,757,787,797,919,929

Frequency of prime palindrome integers: 15

Example 2:
INPUT:

M=100

N=5000

ALGORITHM:
Step 1: Start.

Step 2: Input the range.

Step 3: Set the loop.

for(i=m;i<=n;i++)

Step 4: Check the numbers are prime or not.

Step 5: Check the numbers are palindrome or not.

Step 7: If the number is both prime as well as palindrome print the number
print the number.

Step 8: Increase the counter variable.

Step 9: Print the frequency.

Step 10: Stop.

PROGRAM:
import java.util.*;

class Q11

public static void main (String args[])

Scanner sc = new Scanner(System.in);


int m,n,i,j,t,c,r,a,freq;

System.out.println("Enter two positive integers m and n, where m < 3000 and


n < 3000: ");

m = sc.nextInt();//input the m value

n = sc.nextInt();//input the n value

if(m < 3000 && n < 3000){

// To count the frequency of prime-palindrome numbers

freq = 0;

System.out.println("The prime palindrome integers are:");

for(i=m;i<=n;i++)

t = i;

//Check for prime

c = 0;

for(j =1; j<=t; j++)

if(t%j == 0)

c++;

if(c == 2)

//Check for palindrome

r = 0;

while(t>0)
{

a = t%10;//getting remainder

r = r*10 + a;

t = t/10;

if(r == i){

System.out.print(i+" ");

freq++;

System.out.println("\nFrequency of prime palindrome integers:"+freq)//print


the frequency

else

System.out.println("OUT OF RANGE");

}//end of main

}//end of class
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. n int to input the value of n.

2. m int to input the value of m .

3. r int to store the sum to check for


palindrome.

4. i,j int loop variables.

5. t int to temporarily store the


numbers .

6. c int counter variable for prime.

7. freq int to store the frequency and act


as a counter.

8. a int To store the remainder.

Q12) An ISBN (International Standard Book Number) is a ten digit code which
uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and
the last digit is used to check whether ISBN is correct or not.

Each of the first nine digits of the code can take a value between 0 and 9.
Sometimes it is necessary to make the last digit equal to ten; this is done by
writing the last digit of the code as X.

To verify an ISBN, calculate 10 times the first digit, plus 9 times the second
digit, plus 8 times the third and so on until we add 1 time the last digit. If the
final number leaves no remainder when divided by 11, the code is a valid ISBN.

For Example:

1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 =
55

Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.

2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10
= 176

Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.

3. 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*1 + 3*4 + 2*2 + 1*5 =
71

Since 71 leaves no remainder when divided by 11, hence it is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input,
display an appropriate message. Verify the code for its validity in the format
specified below:

Test your program with the sample data and some random data:

Example 1
INPUT CODE: 0201530821

OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE

Example 2

INPUT CODE: 035680324

OUTPUT : INVALID INPUT

Example 3

INPUT CODE: 0231428031

OUTPUT : SUM = 122


LEAVES REMAINDER – INVALID ISBN CODE

ALGORITHM:
Step 1: Start

Step 2: Enter the ISBN code.

Step 3: Find the length.

Step 4: Give the condition that l<=10.


Step 5: Set the i loop

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

Step 6: Give the condition

if(ch.equalsIgnoreCase("X"))

Step 7: Print the output sum.

Step 8: Print that the ISBN code is valid or invalid.

Step 9: Stop.

PROGRAM:
import java.util.*;

class Q12

{//class starts

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.print("Enter a 10 digit code : ");

String s=sc.nextLine();//enter the ISBN code

int l=s.length();

if(l!=10)//set the range

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

else

String ch;
int a=0, sum=0, k=10;

for(int i=0; i<l; i++)// loop i

ch=Character.toString(s.charAt(i));

if(ch.equalsIgnoreCase("X"))//give the condition

a=10;

else

a=Integer.parseInt(ch);

sum=sum+a*k;//sum of the ISBN

k--;//counter decrease

}//i loop ends

System.out.println("Output : Sum = "+sum);

if(sum%11==0)//condition for valid or invalid ISBN

System.out.println("Leaves No Remainder - Valid ISBN Code");

else

System.out.println("Leaves Remainder - Invalid ISBN Code");

}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. ch String to store each digit of ISBN


code.

2. l int to find the length.

3. sum int to store the sum.

4. i int loop variable.

5. a int

6. k int counter variable.

Q13) Write a program to accept a sentence which may be

terminated by either '.', '?' or '!' only.Check that the string is a

Snowball string or not.

Example:

input: I am the lord

output: Snowball string

ALGORITHM:
Step 1: Start.

Step 2: Accept a String as str.

Step 3: Find the length and store it in l.

Step 4: Extract the last character and store it in ch.


Step 5: Extract each word and store it in St[].

Step 6: Check for the validation by

if(ch==’?’||ch==’.’||ch==’!’)&&st.length<=20)

Step 7: Set i loop as for(i=0;i<st.length;i++)

Step 8: Set j loop as for(j=0;j<st.length-1;j++)

Step 9: Compare the string and set the flag variable accordingly.

Step 10: If flag=0,then display the message Snowball string.

Step 11: Stop.

PROGRAM:
import java.util.*;
class Q13
{//class starts
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,l,j;
System.out.println("Enter Sentence");
String str=sc.nextLine();//Enter the string
l=str.length();
char ch=str.charAt(l-1);

String st[]=str.split(“ ”);//store the words in string

if(ch==’?’||ch==’.’||ch==’!’&&(st.length)<=20)
{
System.out.println(“Valid String”);

int f=0;

for(i=0;i<st.length;i++)//set i loop

for(j=0;j<st.length-1;j++)//set j loop

if(st[j].compareTo(st[j+1])<0)//compare word

f=0;

else

f=f+1; //increase the flag variable

if(f==0)

{
System.out.println("Snowball String");//display the message
}
else
{
System.out.println("Not a Snowball String");

}
else

System.out.println(“Invalid String”);

}//class ends

VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE

1. str String to store the string.

2. l int to find the length.

3. st[] String to store each word of string.

4. i,j int loop variables.

5. f int flag variable.

6. ch char to store the last character.

You might also like