XIIPROGRAMS
PROGRAM 1
Write a program to accept a Two dimensional array and find the boundary elements sum
SAMPLE INPUT
1 2 3
4 5 6
7 8 9
SAMPLE OUTPUT
1+2+3+4+5+6+7+8+9=40
PROGRAM 2
Write a program to accept a two dimensional array with mxn elements. Sort the array using any
of the sorting techniques.
SAMPLE INPUT
55 14 2
14 57 95
46 35 455
SAMPLE OUTPUT
2 14 14
35 46 55
57 95 455
PROGRAM 3
Write a program in java to find the mirror image of a two dimensional array.
SAMPLE INPUT
25 45 9
55 6 45
Computer Science
Page 1
21 95 58
SAMPLE OUTPUT
9 45 25
45 6 55
58 95 21
PROGRAM 4
Write a program to accept n numbers and sort the number in ascending order using Bubble Sort.
SAMPLE INPUT
Size 7
23,45,76,89,92,1,6
SAMPLE OUTPUT
1,6,23,45,76,89,92
PROGRAM 5
Write a program to accept n names and store these names in ascending order using Selection sort
SAMPLE INPUT
Size 9
Salim,warner,kohli,alister,aron,zayne,broad,broan
SAMPLE OUTPUT
Alister,arjun,aron,broad,broan,kohli,salim,warner,zayne
PROGRAM 6
Write a program to accept in names and search for particular names in linear search
SAMPLE INPUT
Size 5
Kiara,lucky,subi,nimish,ashish
Name to be searched is lucky
SAMPLE OUTPUT
Computer Science
Page 2
It is found in 2nd item
PROGRAM 7
Write a program to accept n numbers and search for a particular number using binary search
technique
SAMPLE INPUT
Size 9
1,23,34,45,56,67,78,89,90
Number to be searched 23
SAMPLE OUTPUT
Search successful
PROGRAM 8
Write a program to accept n names and search for a particular name using binary search
technique
SAMPLE INPUT
Size 5
Bitty,lekshmi,johan,keasv
Name to be searched
SAMPLE OUTPUT
Search successful
PROGRAM 9
Write a program to store n names in ascending order Using bubble sort technique.
SAMPLE INPUT
Size 7
Erik,zack,zed,poli,may,ray,tim
SAMPLE OUTPUT
Erik,may,poli,ray,tim,zack,zed
Computer Science
Page 3
PROGRAM 10
Write a program to accept a square matrix and find sum of rows and columns
SAMPLE INPUT
10 15 16 18
15 14 11 11
11 12 16 17
12 10 14 16
SAMPLE OUTPUT
10 15 16 18 59
15 14 12 11 52
11 12 16 17 56
12 10 14 16 52
48 51 58 62 0
PROGRAM 11
Design a class Rearrange using the description of the data members and member function given
below:
Class name : Rearrange
Data members/instance variables :
Txt : to store a word
Cxt : to store the rearranged word
len : to store the length word
Member functions :
Rearrange( ) : constructor to initialize the instance variables
void readword( ) : to accept the word input in UPPER CASE
void convert( ) : converts the word into its changed form and stores it
in string Cxt
Computer Science
Page 4
void display( ) : displays the original and the changed word
PROGRAM 12
Design a class FiboString to generate Fibonacci strings. Some of the members of the class are
given below:
Class name : FiboString
Data members / instance variables :
x : to store the first strings
y : to store the second string
z : to store the concatenation of the previous two strings
n : to store the number of terms
Member functions / methods :
FiboString( ) : constructor to assign x=”a”,y=”b” and z=”ba”
void accept( ) : to accept the number of terms ‘n’
void generate( ) : to generate and print the fibonacci string. The sum of
(‘+’ i.e., concatenation) first two strings is the third
String. For example, “a” is the first string, “b” is
second string then the third will be”ba” , and fourth
will be “bab” and so on.
Specify the class FiboString , giving details of the constructor ( ),void accept ( ) and void
generate( ).
Define the main( ) function to create an object and call the functions accordingly to enable the
task.
PROGRAM 13
A class ConsChange has been defined with the following details:
Class name : ConsChange
Data members/instance variables :
Computer Science
Page 5
word : stores the word
ten : stores the length of the word
Member function / methods :
ConsChange( ) : default constructor
void readword( ) : accepts the word in lowercase
void shiftcons( ) : shifts all athe consonants of the word at the beginning
followed by the vowels e.g., (spoon becomes spnoo)
void changeword( ) : changes the case of all occurring consonants of the shifted
word to uppercase, e.g., (spnoo becomes SPNoo)
void show( ) : displays the original word, shifted word and the changed
word
Specify the class Conschange giving the details of the constructor( ),void readword( ), void
shiftycons( ) void changeword( ), and void show( ). Define the main( ) function to create an
oblect and call the functions accordingly to enable the task.
PROGRAM 14
Write a program that inputs the numbers into two different Array. Array A has m number while
Array B has n number of numbers , with no repetition in either of them. Merge A&B into a
single array C,such that the resulting array is sorted alphapetically.
Sample input
1st array element
56
25
45
2nd array element
95
77
32
Computer Science
Page 6
Sample output
56
25
45
95
77
32
PROGRAM 15
A prime palindrome integer is a positive integer (without leaving zeros) which is prime as well
as 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<1 and n<1000. Display the 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 same random data:
Input: m=1
N=1000
Output: 101,131,151,181,191,313,353,373,383,727,757,787,797,919,929
PROGRAM 16
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.
Computer Science
Page 7
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
INPUT:
m=5
n = 100
OUTPUT:
The Prime-Adam integers are:
11, 13, 31
Frequency of Prime-Adam integers is: 3
PROGRAM 17
A number is called an emirp number if we get another prime number on reversing the number
itself. In other words, an emirp number is a number that is prime forwards or backward. It is also
known as twisted prime numbers.
9 is a prime number means that divisible by 1 and self only. On reversing the number, we get 97
which is another prime number. Therefore, 79 and 97 both are prime numbers. Hence, 79 is a
prime number. Similarly, we can check other numbers also.
Some other emirp numbers are 13, 199, 107, 113, 1399, 1583, 1201, 3049, etc.
PROGRAM 18
The consecutive prime numbers are known as prime triplets if they satisfy the following
satisfaction.(n,n+2,n+6) are all prime or(n,n+4,n+6) are all prime. Where n is an integer number
n=5,then 5(5+2=7),(5+6=11). Here 5,7,11 all are so(5,7,11) are prime triplets. If n=7,then 7,
(7+2=9),(7+6=13). Here 7,9,13; all are not primes so(7,0,13) are not prime triplet
PROGRAM 19
A palindrome is a word that may be read the same way in either direction.Accept a sentence in
Upper Case which is terminated by either “.””,””?” or”!”.
Each word of the sentence is separated by a single blank apace.
Perform the following tasks:
(a) Display the count of palindromic words in the sentence.
(b) Display the palindromic word in the sentence.
Computer Science
Page 8
Example of palindromic word:
MADAM,ARORA,NOON
Test your program for the following data and some random data.
Input: MOM AND DAD ARE NOT AT HOME.
Output:
MOM
DAD
PROGRAM 20
Write a program to accept a word/string and display the number of consecutive vowels present
in it.
S.I : BEAUTIFUL BEAUTIES
S.O: Pair of vowels – 5
[ EA, AU, EA,AU,IE]
Question 21
Design a program to accept a day number (between 1 and 366), year (in 4 digits)
from the user to generate and display the corresponding date. Also, accept 'N' (1
<= N <= 100) from the user to compute and display the future date
corresponding to 'N' days after the generated date. Display an error message if
the value of the day number, year and N are not within the limit or not according
to the condition specified.
Test your program with the following data and some random data:
Example 1
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018
Example 2
Computer Science
Page 9
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019
Example 3
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT:
DAY NUMBER OUT OF RANGE
Example 4
INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE
Solution
import java.util.Scanner;
public class DateCalculator
{
public static boolean isLeapYear(int y) {
boolean ret = false;
if (y % 400 == 0) {
ret = true;
}
else if (y % 100 == 0) {
ret = false;
}
else if (y % 4 == 0) {
ret = true;
}
else {
ret = false;
}
Computer Science
Page 10
return ret;
}
public static String computeDate(int day, int year) {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31};
String monthNames[] = {"JANUARY", "FEBRUARY", "MARCH",
"APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER",
"OCTOBER", "NOVEMBER",
"DECEMBER"};
boolean leap = isLeapYear(year);
if (leap) {
monthDays[1] = 29;
}
int i = 0;
int daySum = 0;
for (i = 0; i < monthDays.length; i++) {
daySum += monthDays[i];
if (daySum >= day) {
break;
}
}
int date = day + monthDays[i] - daySum;
StringBuffer sb = new StringBuffer();
sb.append(date);
sb.append("TH ");
sb.append(monthNames[i]);
sb.append(", ");
sb.append(year);
return sb.toString();
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("DAY NUMBER: ");
int dayNum = in.nextInt();
System.out.print("YEAR: ");
int year = in.nextInt();
System.out.print("DATE AFTER (N DAYS): ");
Computer Science
Page 11
int n = in.nextInt();
if (dayNum < 1 || dayNum > 366) {
System.out.println("DAY NUMBER OUT OF RANGE");
return;
}
if (n < 1 || n > 100) {
System.out.println("DATE AFTER (N DAYS) OUT OF
RANGE");
return;
}
String dateStr = computeDate(dayNum, year);
int nDays = dayNum + n;
int nYear = year;
boolean leap = isLeapYear(year);
if (leap && nDays > 366) {
nYear = nYear + 1;
nDays = nDays - 366;
}
else if (nDays > 365) {
nYear = nYear + 1;
nDays = nDays - 365;
}
String nDateStr = computeDate(nDays, nYear);
System.out.println();
System.out.println("DATE: " + dateStr);
System.out.println("DATE AFTER " + n
+ " DAYS: " + nDateStr);
}
}
Question 22
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.
Computer Science
Page 12
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:
1258
1251
1212
1125
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
137
131
113
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
Computer Science
Page 13
25625
25256
2 2 5 6 10
Solution
import java.util.Scanner;
public class Array
{
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();
if (n <= 2 || n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE");
return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE
DIMENSIONAL ARRAY:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
Computer Science
Page 14
}
for (int i = n - 1, r = 0; i >= 0; i--, r++) {
for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
}
for (int k = n - 1; k > i; k--) {
b[r][k] = a[k - i - 1];
}
}
System.out.println();
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] + " ");
}
System.out.println();
}
}
}
Question 23
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
uppercase.
Perform the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-palindrome words of the sentence into palindrome words by
concatenating the word by its reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting the last alphabet) and by
concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP
becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB would
become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Computer Science
Page 15
Example 1
INPUT:
THE BIRD IS FLYING.
OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT:
IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
INPUT:
THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 3
INPUT:
YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT
Solution
import java.util.*;
public class Palindrome
{
public static boolean isPalindrome(String word) {
boolean palin = true;
int len = word.length();
for (int i = 0; i <= len / 2; i++) {
if (word.charAt(i) != word.charAt(len - 1 - i)) {
palin = false;
break;
}
Computer Science
Page 16
}
return palin;
}
public static String makePalindrome(String word) {
int len = word.length();
char lastChar = word.charAt(len - 1);
int i = len - 1;
while (word.charAt(i) == lastChar) {
i--;
}
StringBuffer sb = new StringBuffer(word);
for (int j = i; j >= 0; j--) {
sb.append(word.charAt(j));
}
return sb.toString();
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();
char lastChar = ipStr.charAt(len - 1);
if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
}
String str = ipStr.substring(0, len - 1);
StringTokenizer st = new StringTokenizer(str);
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb .append(word);
}
else {
String palinWord = makePalindrome(word);
sb .append(palinWord);
Computer Science
Page 17
}
sb.append(" ");
}
String convertedStr = sb.toString().trim();
System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
}
}
Question24
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'.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 14
OUTPUT:
PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT:
N = 30
OUTPUT:
PRIME PAIRS ARE:
7, 23
Computer Science
Page 18
11, 19
13, 17
Example 3
INPUT:
N = 17
OUTPUT:
INVALID INPUT. NUMBER IS ODD.
Example 4
INPUT:
N = 126
OUTPUT:
INVALID INPUT. NUMBER OUT OF RANGE.
Solution
import java.util.Scanner;
public class GoldbachNumber
{
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 void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
if (n <= 9 || n >= 50) {
System.out.println("INVALID INPUT. NUMBER OUT OF
RANGE.");
return;
}
if (n % 2 != 0) {
Computer Science
Page 19
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
}
System.out.println("PRIME PAIRS ARE:");
int a = 3;
int b = 0;
while (a <= n / 2) {
b = n - a;
if (isPrime(a) && isPrime(b)) {
System.out.println(a + ", " + b);
}
a += 2;
}
}
}
Question25
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
ENTER ELEMENTS OF MATRIX:
11 −2 3
5 16 7
9 0 4
3 1 8
Computer Science
Page 20
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
ENTER ELEMENTS OF MATRIX
22 5 19
7 36 12
9 13 6
OUTPUT:
ORIGINAL MATRIX
22 5 19
7 36 12
9 13 6
MATRIX AFTER SORTING ROWS
5 19 22
7 12 36
6 9 13
Computer Science
Page 21
Example 3
INPUT:
M = 11
N=5
OUTPUT:
MATRIX SIZE OUT OF RANGE.
Solution
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();
}
}
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();
}
Computer Science
Page 22
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++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Question26
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
Computer Science
Page 23
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
Solution
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();
in.nextLine();
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();
if (teams[i].length() > highLen) {
Computer Science
Page 24
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();
}
}
}
Computer Science
Page 25