Sei sulla pagina 1di 135

Q1.

Write a program in java to count the number of words and the number of
consonants and store them in wordcount(variable) and consonants(variable)
respectively.

ALGORITHM
1. Enter a string and extract each word from it.
2. Count the no. of words and display.
3. Check whether each character is a vowel or a
consonant.
4. If a consonant, increase the count for consonants by
one. Display the count
SOURCE CODE
import java.util.*;
class count
{
public static void main(String arg [])
{
int consonants=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String str = sc.nextLine();
StringTokenizer s = new StringTokenizer(str);
int a = s.countTokens();
for(int i = 0; i < a; i++)
{
String st = s.nextToken();
int l= st.length();

for(int j=0;j<l;j++)
{
char ch=st.charAt(j);

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='
i'||ch=='o'||ch=='u')
{
}
else
{
consonants++;
}
}
}
System.out.println("Total Number Of Words In String "+str+" are
"+a);
System.out.println("Total Number Of Consonants In String
"+str+" are "+consonants);
}
}
OUTPUT
Enter a String
India won the match against Pakistan
Total Number Of Words In String are 6
Total Number Of Consonants In String are 36

Variable Description
Name Data Type Purpose

consonants int To store the no. of consonants


str String To store the input string
a int To store the no. of words
st String To store each word on the string
l int To store the length of each word
ch char To store each character of a word
i, j int Counters for “for” loop
Q2. Write a program to get two different Strings and generate Fibonacci Strings as
follows: -
Example:
INPUT:
Enter 1st String =”a”.
Enter 2nd String=”b”.
OUTPUT:
The Desired Series as Follows: - a, b, ba, bab.

ALGORITHM
1. Enter the limit for the string series
2. With the help of “for” loop, create a Fibonacci string
series by adding b to a, forming c, while doing
swapping.
3. Print the series
SOURCE CODE
import java.util.*;
class fibonacci
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit of the string");
int n = sc.nextInt();
String a = "a";
String b = "b";
String c = "ba";
System.out.print(a+" "+b);
for(int i = 2; i <= n; i++)
{
System.out.print(" "+c);
a = b;
b = c;
c = b.concat(a);
}
}

}
OUTPUT
Enter the limit of the string
7
a b ba bab babba babbabab babbababbabba babbababbabbababbabab

Variable Description
Name Data Type Purpose

a String To store the first string of the triad


b String To store the second string of the triad
c String To store the third string of the triad
n int To store the limit of the series
i int Counters for “for” loop
Q3. Write a program to interchange the first and the last alphabet of every word
in a sentence to form a new sentence using the changed words.
Example:
INPUT: It is a warm day.
OUTPUT: tI si marw yad.

ALGORITHM
1. Enter a string and extract each word from it.
2. In each word, replace the first character with the last
character and the last with the first.
3. Print the sentence with the encrypted words.
SOURCE CODE
import java.util.*;
class exchange
{
public static void main(String args[])
{
String str, rest = "";
int pos;
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A SENTENCE");
str = sc.nextLine();
StringTokenizer st = new StringTokenizer(str);
int length=st.countTokens();
for(int i = 0; i < length; i++)
{
String s = st.nextToken();
int l2 = s.length();
if(l2 == 1)
{
rest = s;
}
else
{
char ch1 = s.charAt(0);
char ch2 = s.charAt(l2-1);
rest = ch2 + s.substring(1,l2-1) + ch1;
}
System.out.print(rest+" ");
}
}
}
OUTPUT
ENTER A SENTENCE
It is a warm day
tI si a marw yad

Variable Description
Name Data Type Purpose

str String To store the input string


length int To store the no. of words
s String To store each word on the string
l2 int To store the length of each word
ch1 char To store the first character of a word
ch2 char To store the last character of a word
rest String To store the words with the swapped characters
Q4. Write a program to encrypt and de crypt data using Rot13 technique
Input String: “Encryption”
Encoded as: “Rapelcgvba”

ALGORITHM
1. Enter a string and extract each word from it.
2. In each word, replace each alphabet with the
alphabet 13 ASCII values ahead/prior.
3. Print the sentence with the encrypted words.
SOURCE CODE
import java.util.*;
class Rot13
{
public static void main(String arg [])
{

Scanner sc = new Scanner(System.in);


System.out.println("Enter a String");
String str = sc.nextLine();
StringTokenizer s = new StringTokenizer(str);
int a = s.countTokens();
System.out.print("Decoded Text : ");
for(int i = 0; i < a; i++)
{
String st = s.nextToken();
int l= st.length();
for(int j=0;j<l;j++)
{
char ch=st.charAt(j);
if(ch>='a'&&ch<='m')
{
ch+=13;
}
else if(ch>='A'&&ch<='M')
{
ch+=13;
}
else if(ch>='N'&&ch<='Z')
{
ch-=13;
}
else if(ch>='n'&&ch<='z')
{
ch-=13;
}
else
{
System.out.println("Invalid Operation");
}
System.out.print(ch);
}
System.out.print(" ");
}
}
}
OUTPUT
Enter a String
Encryption
Decoded Text : Rapelcgvba

Variable Description
Name Data Type Purpose

str String To store the input string


a int To store the no. of words
st String To store each word on the string
l int To store the length of each word
ch char To store each character of a word
Q5. Write a program to accept a plain of text of length L, where L must be greater
than 3 and less than 100.
Using the concept of Caesar cipher’s technique.
Sample Input 1
Input: The morning is cheerful!!
Output: Decoded Text : Gur zbeavat vf purreshy!!
Sample Input 2
Input: he
Output: Invalid operation

ALGORITHM
1. Enter a string and extract each word from it.
2. In each word, replace each alphabet with the
alphabet 13 ASCII values ahead/prior.
3. Print the sentence with the encrypted words.
SOURCE CODE
import java.util.*;
class caeser_cipher
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String str = sc.nextLine();
StringTokenizer s = new StringTokenizer(str);
int a = s.countTokens();
if(a > 3 && a < 100)
{
System.out.print("Decoded Text : ");
for(int i = 0; i < a; i++)
{
String st = s.nextToken();
int l= st.length();
for(int j = 0; j < l; j++)
{
char ch=st.charAt(j);
if(ch >= 'a' && ch <= 'm')
{
ch+=13;
}
else if(ch >= 'A' && ch <= 'M')
{
ch+=13;
}
else if(ch >= 'N' && ch <= 'Z')
{
ch-=13;
}
else if(ch >= 'n' && ch <= 'z')
{
ch-=13;
}
else if(ch == '!'||ch == '?'||ch == '.')
{
ch = ch;
}
else
{
System.out.println("Invalid Operation");
}
System.out.print(ch);
}
System.out.print(" ");
}
}
else
{
System.out.println("Invalid operation");
}
}
}
OUTPUT
Enter a String
The morning is cheerful!!
Decoded Text : Gur zbeavat vf purreshy!!

Variable Description
Name Data Type Purpose

str String To store the input string


a int To store the no. of words
st String To store each word on the string
l int To store the length of each word
ch char To store each character of a word
Q6. Write a program to accept a sentence which may be terminated either
by”.”,”!” or”?”. The words may be separated by more than one blank space & are
in UPPER CASE.
Perform the following tasks: -
Find the number of words beginning & ending with a vowel.
Place the words which begin & end with a vowel at the beginning, followed by the
remaining words as they occur in the sentence.
Eg: -
Input: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
Output: No of words beginning and Ending with vowel: 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL.

ALGORITHM
1. Enter a string and extract each word from it.
2. In each word, check whether the first & the last
character are vowels.
3. If yes, increase the count for such words by one and
take it to the beginning, leaving the rest of the words
to the latter part of the sentence.
4. Display the count and the new sentence.
SOURCE CODE
import java.util.*;
class vowels
{
public static void main(String args[])
{
String vow = "";
String rest = "";
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A SENTENCE");
String str = sc.nextLine();
int length = str.length();
char ch1 = str.charAt(length-1);
if(ch1 == '!'||ch1 == '?'||ch1 == '.')
{
String c = str.substring(0, (length - 1));
StringTokenizer st = new StringTokenizer(c);
int length1 = st.countTokens();
for(int i = 0; i < length1; i++)
{
String s = st.nextToken();
int l2 = s.length();
char ch = s.charAt(0);
char lch = s.charAt(l2-1);
if((ch == 'a'||ch == 'e'||ch == 'i'||ch == 'o'||ch ==
'u'||ch == 'A'||ch == 'E'||ch == 'I'||ch == 'O'||ch == 'U') && (lch ==
'a'||lch == 'e'||lch == 'i'||lch == 'o'||lch == 'u'||lch == 'A'||lch ==
'E'||lch == 'I'||lch == 'O'||lch == 'U'))
{
count++;
vow = vow + s + " ";
}
else
rest = rest + s +" ";
}
System.out.println(vow + rest + ch1);
System.out.println("TOTAL NUMBER OF WORDS STARTING AND
ENDING WITH VOWELS : "+count);
}
else
{
System.out.println("Enter correct sentence with proper
punctuations");
}
}
}
OUTPUT
ENTER A SENTENCE
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL .
TOTAL NUMBER OF WORDS STARTING AND ENDING WITH VOWELS : 3

Variable Description
Name Data Type Purpose

vow String To store vowel starting words


rest String To store the rest of the words
C String Stores the string without the punctuation marks
Count int To store the no. of vowel-starting words
Str String To store the input string
Length Int The length of ‘str’
Ch1 char To store the last character
Length1 Char No. of words in string
S String Stores each word in the string
L2 int Length of each word
Ch Char First character of each word
lch char Last character of each word
Q7. Write a program to accept a sentence which may be terminated by either “.”
Or “?” only. Print an error message if it doesn’t.
Perform the following tasks: -
Convert the 1st letter of each word to Uppercase.
Find the number of vowels & consonants in each word & display them with
proper alignment.
Eg: -
Input: Intelligence is education
Output:
Word vowels consonants
Intelligence 5 7
Is 1 1
Education 5 4

ALGORITHM
1. Enter a string and extract each word from it.
2. Capitalize each word and check the frequency of
vowels & of consonants in the word.
3. Print each word with its respective frequencies in a
table format
SOURCE CODE
import java.util.*;
class index
{
public static void main(String args[])
{
String rest = "";
Scanner sc =new Scanner(System.in);
System.out.println("ENTER A SENTENCE");
String str = sc.nextLine();
StringTokenizer st = new StringTokenizer(str);
int length = st.countTokens();
System.out.println("WORD"+"\t\t"+"VOWEL"+"\t\t"+"CONSONANTS");
for(int i = 0; i < length; i++)
{
String s = st.nextToken();
int l2 = s.length();
String word = s.substring(0,1).toUpperCase();
s = word + s.substring(1,l2);
int cc = 0, cv = 0;
for(int j = 0; j < l2; j++)
{
char c = s.charAt(j);
if(c == 'a'||c == 'e'||c == 'i'||c == 'o'||c == 'u'||c
== 'A'||c == 'E'||c == 'I'||c == 'O'||c == 'U')
{
cv++;
}
else
{
cc++;
}
}
if(s.length() < 7)
System.out.println(s+"\t\t\t"+cv+"\t\t"+cc);
else
System.out.println(s+"\t\t"+cv+"\t\t"+cc);
}
}
}
OUTPUT
ENTER A SENTENCE
Necessity is the mother of invention
WORD VOWEL CONSONANTS
Necessity 3 6
Is 1 1
The 1 2
Mother 2 4
Of 1 1
Invention 4 5

Variable Description
Name Data Type Purpose

Str String To store the input string


Length Int No. of words in string
S String Stores each word in the string
L2 int Length of each word
C Char each character of every word
cc int To store the no. of consonants
cv int To store the no. of vowels
word String To store the first letter in UPPERCASE
Q8. Write a program to accept a sentence which may be terminated by either “.”
Or “?” only.
Perform the following tasks: -
Obtain the length of the sentence (Measure in words).
Rearrange the sentence in the ascending order of the first alphabet of every
word.
Input:
Necessity is the mother of invention
Output:
Length of sentence: 6 words.
Rearranged sentence: Invention is mother necessity of the.

ALGORITHM
1. Enter a string and extract each word from it.
2. Sort the sentence lexicographically with the help of
an array
3. Print the sorted sentence
SOURCE CODE
import java.util.*;
class ascending
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter sentence");
String in = sc.nextLine();
int l = in.length();
char ch = in.charAt(l - 1);
String s = "";
if(ch == '.'||ch == '!'||ch == '?'||ch == ','||ch == ';'||ch ==
':')
s = in.substring(0, l - 1);
else
s = in;
StringTokenizer str = new StringTokenizer(s);
int len = str.countTokens();
System.out.println("Length of sentence = "+len);
String e[] = new String[len];
for (int i = 0; i < len; i++)
{
e[i] = str.nextToken();
}
int lt = len - 1;
for(int i = 0; i < lt - 1; i++)
{
for(int j = 0; j < lt - i; j++)
{
if(e[j].compareTo(e[j + 1]) > 0)
{
String temp = e[j];
e[j] = e[j + 1];
e[j + 1] = temp;
}
}
}
for (int i = 0; i < len; i++)
{
System.out.print(e[i]);
if(i == (len - 1))
break;
System.out.print(" ");
}
}

}
OUTPUT
Enter sentence
necessity is the mother of invention
Length of sentence = 6
is invention mother necessity of the

Variable Description
Name Data Type Purpose

in String To store the input string


L Int Length of ‘in’
Ch Char Last character of ‘in’
S String To store the string without punctuation marks
Len Int No. of words
E[] String To store each word in array form
Lt Int To store (len – 1)
Q9. Write a program to accept a sentence which is terminated either by”.”,”!
“or”?”.
Perform the following tasks: -
Count the number of palindrome words in a sentence.
Display all the palindrome words in the sentence.
Input:
MOM AND DAD ARE COMING AT NOON.
Output:
Number of palindrome words:3
Palindrome words: MOM
DAD
NOON

ALGORITHM
1. Enter a string and extract each word from it.
2. Check each word to see whether it is a palindrome
or not
3. If yes, increase the count of palindromes by one and
display the word.
4. Display the count
SOURCE CODE
import java.util.*;
class palin
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A STRING");
String str = sc.nextLine();
int l = str.length();
char ch = str.charAt(l - 1);
String c = "";
if(ch == '.'||ch == '?'||ch == ',')
{
c = str.substring(0, l - 1);
StringTokenizer s = new StringTokenizer(c);
int a = s.countTokens();
int count = 0;
for(int i = 0; i < a; i++)
{
String st = s.nextToken();
String pal = "";
int l1 = st.length();
int j = l1 - 1;
for(; j >= 0; j--)
{
pal = pal+st.charAt(j);
}
if(pal.equalsIgnoreCase(st))
{
System.out.println(pal);
count++;
}
}
System.out.println("TOTAL NUMBER OF PALINDROMES :
"+count);
}
else
{
System.out.println("ENTER YOUR SENTENCE WITH PROPER
PUNCTUATIONS");
}
}
}
OUTPUT
ENTER A STRING
MOM AND DAD ARE COMING AT NOON.
MOM
DAD
NOON
TOTAL NUMBER OF PALINDROMES : 3

Variable Description
Name Data Type Purpose

Str String input string


l Int Length of str
Ch Char The last character of str
C String The string without punctuation marks
A Int No. of words in string
Count Int To store the no. of palindrome words
St String To store each word of the string
Pal String To store the reverse of each word
L1 Int To store the length of each word
Q10. Write a program to accept a sentence in which word may be separated by
more than one blank space and are in UPPER CASES.
Perform the following tasks: -
Convert all the extra blank spaces between two words into a single space.
Accept a word which is a part of sentence along with its position number & delete
the word and display the sentence.
Input:
Enter your sentence: A morning walk is a blessing is for the whole day.
Enter the word to be deleted: is
Enter the position in the sentence: 7
Output:
A morning walk is a blessing for the whole day.

ALGORITHM
1. Enter a string and extract each word from it.
2. Compare the word on the position entered with the
word to be deleted and leave it out of the printing
command.
SOURCE CODE
import java.util.*;
class delete
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter sentence");
String in = sc.nextLine();
int l = in.length();
char ch = in.charAt(l - 1);
String s = "";
if(ch == '.'||ch == '!'||ch == '?'||ch == ','||ch == ';'||ch
== ':')
s = in.substring(0, l - 1);
else
s = in;
System.out.println("Enter word to be deleted");
String w = sc.next();
System.out.println("Enter position");
int p = sc.nextInt();
StringTokenizer str = new StringTokenizer(s);
String e = "";
int flag = 0;
int len = str.countTokens();
for (int i = 0; i < len; i++)
{
String wo = str.nextToken();
if(i == p - 1)
{
if(wo.compareTo(w) != 0)
{
System.out.println("Error in direction");
flag = 1;
break;
}
else
continue;
}
else
{
e = e + wo;
}
if (i == len-1)
{
break;
}
e = e + " ";
}
if(flag == 0)
if(ch == '.'||ch == '!'||ch == '?'||ch == ','||ch == ';'||ch
== ':')
System.out.println(e + ch);
else
System.out.println(e);
}
}
OUTPUT
Enter sentence
A morning walk is a blessing is for the whole day.
Enter word to be deleted
is
Enter position
7
A morning walk is a blessing for the whole day.

Variable Description
Name Data Type Purpose

in String input string


l Int Length of in
Ch Char The last character of in
s String The string without punctuation marks
len Int No. of words in string
p Int Position of the word to be deleted
wo String To store each word of the string
w String To store the word to be deleted
e String To store the sentence with the word deleted
flag Int To store the length of each word
Q11. To obtain the desired output as per the question.
Input 1: Kite
Output: Potential=11+9+20+5=45
Input 2: The Sky Is the Limit.
Output: The = 33
Sky = 55
Is = 28
The = 33
Limit = 63
Output: Is the The Sky Limit.

ALGORITHM
1. Enter a string and extract each word from it.
2. Sort the words in accord to their potentials
3. Print each word with its potential value and the
sorted sentence
SOURCE CODE
import java.util.*;
class potential
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE STRING");
String sent = sc.nextLine();
StringTokenizer str = new StringTokenizer(sent);
int a = str.countTokens();
String wor[] = new String[a + 1];
int pot[] = new int[a + 1];
for(int i = 0; i < a; i++)
{
String word = str.nextToken().toUpperCase();
int length = word.length();
int potential1 = 0;
for(int j = 0; j < length; j++)
{
char ch = word.charAt(j);
int ascii = ch;
if(ascii >= 65 && ascii <= 90)
{
potential1 = potential1 + (ascii-64);
}
}
System.out.println(word+"\t"+potential1);
wor[i] = word;
pot[i] = potential1;
}
System.out.println("Rearranged string");
int n = wor.length;
int temp1;
String temp2;
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
if(pot[i] > pot[j])
{
temp1 = pot[i];
pot[i] = pot[j];
pot[j] = temp1;
temp2 = wor[i];
wor[i] = wor[j];
wor[j] = temp2;
}
}
}
String h = "";
System.out.println("");
for(int c = 0; c < n; c++)
{
System.out.print(wor[c]+" ");
}
}
}
OUTPUT
ENTER THE STRING
The Sky Is the Limit.
THE 33
SKY 55
IS 28
THE 33
LIMIT. 63
Rearranged string
IS THE THE SKY LIMIT.

Variable Description
Name Data Type Purpose

Sent String Input string


A Int No. of words
Wor[] String Array to store each word
Pot[] Int Array to store the potential of each word
Word String Each word of the string in UPPERCASE
Length Int Length of word
Potential1 Int Potential of each word
Q12. To obtain the desired output as per the question.
Input: Useful
Output: U-s-e-f-u-l.
l-u-f-e-s-U.

ALGORITHM
1. Enter a string and extract each character from it.
2. Print the characters together, with a hyphen
separating each character
SOURCE CODE
import java.util.*;
class token_letter
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
String s = sc.nextLine();
int le = s.length();
for (int j = 0; j < le; j++)
{
char ch = s.charAt(j);
System.out.print(ch);
if (j == le-1)
break;
System.out.print("-");
}
System.out.println();
for (int len = le - 1; len >= 0; len--)
{
char ch1 = s.charAt(len);
System.out.print(ch1);
if (len == 0)
break;
System.out.print("-");
}
}
}
OUTPUT
Enter string
Useful
U-s-e-f-u-l
l-u-f-e-s-U

Variable Description
Name Data Type Purpose

S String Input string


Le Int Length of string
Ch Char Each character of the string
Len Int Counter for “for” loop
J Int Counter for “for” loop
Q13. To obtain the desired output as per the question.
Input: Sample String.
Output: aSpmelS rting

ALGORITHM
1. Enter a string.
2. Swap every two letters with each other
3. Print the new encrypted string
SOURCE CODE
import java.util.*;
class swap
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
String s = sc.nextLine();
int le = s.length();
char ch0 = '\u0000';
if(le % 2 != 0)
{
le -= 1;
ch0 = s.charAt(le);
}
for (int j = 0; j < le; j += 2)
{
char ch = s.charAt(j);
char ch1 = s.charAt(j + 1);
System.out.print(ch1+""+ch);
}
if(ch0 != '\u0000')
System.out.print(ch0);
}
}
OUTPUT
Enter string
Sample String
aSpmelS rtnig

Variable Description
Name Data Type Purpose

S String Input string


Le Int Length of s
Ch0 Char The default empty variable
Ch Char The first character of the two
Ch1 Char The last character of the two
Q14. Design a class DDarray
Class name: DDarray
Data Members:
mat[][] = to store the matrix
m = integer to store no. of rows
n = integer to store no. of columns
Member Functions:
DDarray(int nr, int nc): Constructor to assign size of matrix; m = nr & n = nc
void readMatrix(): to enter the elements in the matrix of order m*n
void sums(): To compute & print the sum of left diagonal elements & the sum of
right diagonal elements and the sum of boundary elements.
void showMat(): to print the matrix of order m*n row wise. Also print only the
inner elements (except boundary) in matrix form
E.g.
9 3 7 3
2 6 1 5
5 4 8 2
7 9 3 7
Sum of left-diagonal elements = 19
Sum of right-diagonal elements = 22
Sum of boundary elements = 47
Inner Matrix:
6 1
4 8
ALGORITHM
1. DDarray(): initialise m = nr & n = nc
2. void readMatrix():enter the elements in the matrix
of order m*n
3. void sums(): To compute & print the sum of left
diagonal elements & the sum of right diagonal
elements and the sum of boundary elements.
4. void showMat(): to print the matrix of order m*n
row wise. Also print the inner elements (except
boundary) in matrix form
SOURCE CODE
import java.util.*;
class DDarray
{
Scanner sc = new Scanner(System.in);
int m, n;
int mat[][] = new int[m][n];
DDarray(int nr, int nc)
{
m = nr;
n = nc;
}
public void readMatrix()
{
System.out.println("Enter matrix elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i+1));
for(int j = 0; j < n; j++)
{
mat[i][j] = sc.nextInt();
}
}
}
public void sums()
{
int suml = 0;
int sumr = 0;
int sumb = 0;
int nu = n - 1;
for(int i = 0; i <= m; i++)
{
suml = suml + mat[i][i];
sumr = sumr + mat[i][nu];
nu--;
}
System.out.println("Sum of left diagonal numbers = "+suml);
System.out.println("Sum of right diagonal numbers = "+sumr);
for(int i = 0; i <= m; i++)
{
for(int j = 0; j <= n; j++)
{
if(i == 0 || j == 0 || i == (m - 1) || j == (n - 1))
sumb = sumb + mat[i][j];
}
}
System.out.println("Sum of boundary numbers = "+sumb);
}
public void showMats()
{
System.out.println("Original Matrix");
for(int i = 0; i <= m; i++)
{
for(int j = 0; j <= n; j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
System.out.println("Inner Matrix");
for(int i = 0; i <= m; i++)
{
for(int j = 0; j <= n; j++)
{
if(i == 0 || j == 0 || i == (m - 1) || j == (n - 1))
{
}
else
System.out.println(mat[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String arg [])
{
DDarray ob = new DDarray(3,3);
ob.readMatrix();
ob.sums();
ob.showMats();
}
}
OUTPUT
Enter matrix elements
Row 1
5
6
7
1
Row 2
8
6
4
8
Row 3
2
5
6
4
Row 4
3
3
6
9
Sum of left diagonal numbers = 26
Sum of right diagonal numbers = 13
Sum of boundary numbers = 62
Original Matrix
5 6 7 1
8 6 4 8
2 5 6 4
3 3 6 9
Inner Matrix
6 4
5 6

Variable Description
Name Data Type Purpose

Mat[][] Int Input matrix


M Int No. of rows
N Int No. of columns
suml Int Sum of left diagonal elements
Sumr Int Sum of right diagonal elements
Sumb Int Sum of boundary elements
Q15. Design a class Transarray to find the transpose of a given matrix
Data Members:
a[][] = to store the matrix
m = integer to store no. of rows
n = integer to store no. of columns
Member Functions:
Transarray() : default Constructor
Transarray(int mm, int nn): Constructor to assign size of matrix; m = mm & n = nn
void fillArray(): to enter the elements in the matrix of order m*n
void transpose(Transarray A): To find and store the transpose of a matrix from
object A.
void disparray(): display the arrays in matrix form

ALGORITHM
1. Transarray() : initialize m = mm & n = nn
2. void fillArray(): enter the elements in the matrix of
order m*n
3. void transpose():find and store the transpose of a
matrix from object A.
4. void disparray(): display the arrays in matrix form
SOURCE CODE
import java.util.*;
class Transarray
{
Scanner sc = new Scanner(System.in);
int m, n;
int mat[][];
int b[][];
Transarray(int mm, int nn)
{
m = mm;
n = nn;
mat = new int[m][n];
b = new int[n][m];
}
public void fillArray()
{
System.out.println("Enter matrix elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i+1));
for(int j = 0; j < n; j++)
{
mat[i][j] = sc.nextInt();
}
}
}
public void transpose()
{
for(int k = 0; k < 3; k++)
{
for(int l = 0; l < 3; l++)
{
b[k][l] = mat[l][k];
}
}
}
public void dispArray()
{
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
System.out.println("Transpose");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String arg [])
{
Transarray ob = new Transarray(3, 3);
ob.fillArray();
ob.transpose();
ob.dispArray();
}
}
OUTPUT
Enter matrix elements
Row 1
8
4
5
Row 2
6
8
65
Row 3
6
2
5
Original Matrix
8 4 5
6 8 65
6 2 5
Transpose
8 6 6
4 8 2
5 65 5

Variable Description
Name Data Type Purpose

mat[][] Int Input matrix


M Int No. of rows
N Int No. of columns
B[][] Int To store the transpose of mat[][]
Q16. Write a program to rotate the matrix 90⁰ clockwise as below:
Original Matrix:
1 2 3
4 5 6
7 8 9
Rotated Matrix:
7 4 1
8 5 2
9 6 3
And find the sum of the elements of the four corners of the matrix = 20

ALGORITHM
1. Enter matrix
2. Print the original matrix
3. Print the mirror image of the transpose of the matrix
in order to get the rotated matrix
SOURCE CODE
import java.util.Scanner;
class rotate
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[3][3];
int b[][] = new int[3][3];
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i][j] = sc.nextInt();
}
if(i == 3)
break;
System.out.println("Next row");
}
System.out.println("Array:");
for(int k = 0; k < 3; k++)
{
for(int l = 0; l < 3; l++)
{
System.out.print(a[k][l]+"\t");
}
System.out.println();
}
System.out.println("Rotated Matrix");
for(int k = 0; k < 3; k++)
{
for(int l = 2; l >= 0; l--)
{
System.out.print(a[l][k]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter numbers of array 'a'
1
9
7
Next row
5
3
45
Next row
5
8
6
Next row
Array:
1 9 7
5 3 45
5 8 6
Rotated Matrix
5 5 1
8 3 9
6 45 7

Variable Description
Name Data Type Purpose

A[][] Int Input matrix


I, j, k, l Int Counters for “for” loop
Q17. Write a program to check whether the given matrix is symmetric or not &
find the sum of the elements of the left diagonal and that of the right diagonal &
display them
Output:
1 2 3
2 4 5
3 5 6
Symmetric
Sum of left-diagonal elements = 11
Sum of right-diagonal elements = 10

ALGORITHM
1. Enter matrix
2. Compare the transpose with the original and
determine whether the matrix is symmetric or not.
Display the result
3. Compute the sum of right diagonal elements and of
left diagonal elements and print them both.
SOURCE CODE
import java.util.Scanner;
class symmetric
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int suml = 0;
int sumr = 0;
int nu = 2;
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i][j] = sc.nextInt();
}
if(i == 3)
break;
System.out.println("Next row");
}
for(int k = 0; k < 3; k++)
{
for(int l = 0; l < 3; l++)
{
b[k][l] = a[l][k];
}
}
int flag = 0;
for(int k = 0; k < 3; k++)
{
for(int l = 0; l < 3; l++)
{
if(b[k][l] == a[k][l])
flag++;
}
}
for(int i = 0; i < 3; i++)
{
suml = suml + a[i][i];
sumr = sumr + a[i][nu];
nu--;
}
if(flag == 9)
System.out.println("Symmetric");
else
System.out.println("Not Symmetric");
System.out.println("Sum of left diagonal elements = "+suml);
System.out.println("Sum of right diagonal elements = "+sumr);
}
}
OUTPUT
Enter numbers of array 'a'
5
9
7
Next row
4
5
6
Next row
2
3
7
Next row
Not Symmetric
Sum of left diagonal elements = 17
Sum of right diagonal elements = 14

Variable Description
Name Data Type Purpose

A[][] Int Input matrix


I, j, k, l Int Counters for “for” loop
Suml Int Sum of left diagonal elements
Sumr Int Sum of right diagonal elements
Q18. Write a program to create a mirror image of the inputted matrix:
Original Matrix:
1 2 3
4 5 6
7 8 9
Mirror Image:
3 2 1
6 5 4
9 8 7

ALGORITHM
1. Enter matrix
2. Print the original matrix and its mirror image
SOURCE CODE
import java.util.Scanner;
public class mirror
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[4][4];
int b[][] = new int[4][4];
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
a[i][j] = sc.nextInt();
}
if(i == 4)
break;
System.out.println("Next row");
}
System.out.println("Array:");
for(int k = 0; k < 4; k++)
{
for(int l = 0; l < 4; l++)
{
System.out.print(a[k][l]+"\t");
}
System.out.println();
}
System.out.println("Mirror image");
for(int k = 0; k < 4; k++)
{
for(int l = 3; l >= 0; l--)
{
System.out.print(a[k][l]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter numbers of array 'a'
5
8
4
6
Next row
2
4
3
4
Next row
5
9885
6
2
Next row
32
5
6
8
Next row
Array:
5 8 4 6
2 4 3 4
5 9885 6 2
32 5 6 8
Mirror image
6 4 8 5
4 3 4 2
2 6 9885 5
8 6 5 32

Variable Description
Name Data Type Purpose

A[][] Int Input matrix


I, j, k, l Int Counters for “for” loop
Q19. Write a program to sort each row of the matrix in ascending order using the
standard sorting technique.
5 1 3
7 4 6
9 8 2
Sorted:
1 3 5
4 6 7
2 8 9

ALGORITHM
1. Enter matrix
2. Store each row in different 1-d arrays
3. Sort all the 1-d arrays
4. Print them in 2-d order
SOURCE CODE
import java.util.Scanner;
class sort
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[3][3];
int p[] = new int[3];
int q[] = new int[3];
int r[] = new int[3];
System.out.println("Enter numbers of array");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
a[i][j] = sc.nextInt();
}
if(i == 2)
break;
System.out.println("Next row");
}
for(int k = 0; k < 3; k++)
{
for(int l = 0; l < 3; l++)
{
switch(k)
{
case 0: p[l] = a[k][l];
break;
case 1: q[l] = a[k][l];
break;
case 2: r[l] = a[k][l];
break;
}
}
}
for(int k = 0; k < 2; k++)
{
for(int l = 0; l < 2 - k; l++)
{
if(p[l] > p[l + 1])
{
int temp = p[l];
p[l] = p[l + 1];
p[l + 1] = temp;
}
if(q[l] > q[l + 1])
{
int temp = q[l];
q[l] = q[l + 1];
q[l + 1] = temp;
}
if(r[l] > r[l + 1])
{
int temp = r[l];
r[l] = r[l + 1];
r[l + 1] = temp;
}
}
}
System.out.println("Sorted Matrix");
for(int i = 0; i < 3; i++)
{
System.out.print(p[i]+" ");
}
System.out.println();
for(int i = 0; i < 3; i++)
{
System.out.print(q[i]+" ");
}
System.out.println();
for(int i = 0; i < 3; i++)
{
System.out.print(r[i]+" ");
}
}
}
OUTPUT
Enter numbers of array
8
6
4
Next row
7
5
9
Next row
2
1
3
Sorted Matrix
4 6 8
5 7 9
1 2 3

Variable Description
Name Data Type Purpose

A[][] Int Input matrix


I, j, k, l Int Counters for “for” loop
P[] Int First row elements
q[] Int Second row elements
r[] Int Third row elements
Q20. Write a program to enter a sentence and return the sentence sorted in
uppercase

ALGORITHM
SOURCE CODE
import java.util.*;
class praeqn
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String ss = sc.nextLine();
int l = ss.length();
String s = "";
for(int i = 0; i < l; i++)
{
s = s + Character.toUpperCase(ss.charAt(i));
}
StringTokenizer st = new StringTokenizer(s, "!?.");
int c = st.countTokens();
int sum = 0;
for(int i = 0; i < c; i++)
{
String s1 = st.nextToken();
StringTokenizer st1 = new StringTokenizer(s1);
int c1 = st1.countTokens();
int ll = s1.length();
sum = sum + ll;
char ch = s.charAt(sum);
String a[] = new String[c1];
for(int j = 0; j < c1; j++)
{
a[j] = st1.nextToken();
}
for(int m = 0; m < c1; m++)
{
for(int n = 1; n < c1 - m; n++)
{
if(a[n - 1].compareTo(a[n]) > 0)
{
String temp = a[n];
a[n] = a[n - 1];
a[n - 1] = temp;
}
}
}
for(int j = 0; j < c1; j++)
{
System.out.print(a[j]+" ");
}
System.out.print(ch);
sum--;
}
}
}
OUTPUT
Enter the string
March is the month of the ISC & ICSE exams.
& EXAMS ICSE IS ISC MARCH MONTH OF THE THE .

Variable Description
Name Data Type Purpose
Q21. Write a program to enter a matrix and return the rotated matrix and the
sum of the corner elements.

ALGORITHM
SOURCE CODE
import java.util.*;
class matrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("Rotated Matrix");
for(int i = 0; i < m; i++)
{
for(int j = m - 1; j >= 0; j--)
System.out.print(a[j][i]+"\t");
System.out.println();
}
int sum = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if((i == 0 || i == (m - 1)) && (j == 0 || j == (m -
1)))
sum = sum + a[i][j];
}
}
System.out.println("Sum of the corner elements = "+sum);
}
}
OUTPUT
Enter M
3
Enter elements
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
Original Matrix
1 2 3
4 5 6
7 8 9
Rotated Matrix
7 4 1
8 5 2
9 6 3
Sum of the corner elements = 20

Variable Description
Name Data Type Purpose
Q22. Write a program to enter a matrix and check whether the matrix is skew-
symmetric.

ALGORITHM
SOURCE CODE
import java.util.Scanner;
class skew
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
int b[][] = new int[m][m];
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int k = 0; k < m; k++)
{
for(int l = 0; l < m; l++)
{
b[l][k] = a[k][l];
}
}
int flag = 0;
for(int k = 0; k < m; k++)
{
for(int l = 0; l < m; l++)
{
if(b[k][l] == -a[k][l])
flag++;
}
}
if(flag == (m * m))
System.out.println("Skew-symmetric matrix");
else
System.out.println("Not a Skew-symmetric matrix");
}
}

OUTPUT
Enter M
3
Enter numbers of array 'a'
Row 1
0
1
1
Row 2
-1
0
1
Row 3
-1
-1
0
Array:
0 1 1
-1 0 1
-1 -1 0
Skew-symmetric matrix

Variable Description
Name Data Type Purpose
Q23. Write a program to sort a double-dimensional array

ALGORITHM
SOURCE CODE
import java.util.*;
class praeqn2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
int mm = m * m;
int b[] = new int[mm];
int q = 0, z = 0;
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
{
a[i][j] = sc.nextInt();
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
b[q] = a[i][j];
q++;
}
}
for(int k = 0; k < mm - 2; k++)
{
for(int l = 0; l < ((mm - 1) - k); l++)
{
if(b[l] > b[l + 1])
{
int temp = b[l];
b[l] = b[l + 1];
b[l + 1] = temp;
}
}
}
System.out.println("Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Sorted Array:");
for(int k = 0; k < m; k++)
{
for(int l = 0; l < m; l++)
{
System.out.print(b[z]+"\t");
z++;
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter numbers of array 'a'
Row 1
3
4
9
Row 2
7
6
1
Row 3
2
8
5
Array:
3 4 9
7 6 1
2 8 5
Sorted Array:
1 2 3
4 5 6
7 8 9

Variable Description
Name Data Type Purpose
Q24. Write a program to give the output:
Input: He did not cut the deal. Now I can roam free!
Output: CUT DEAL DID HE NOT THE. CAN FREE I NOW ROAM!

ALGORITHM
SOURCE CODE
import java.util.*;
class StringSep
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String ss = sc.nextLine();
int l = ss.length();
String s = "";
for(int i = 0; i < l; i++)
{
s = s + Character.toUpperCase(ss.charAt(i));
}
StringTokenizer st = new StringTokenizer(s, "!?.,");
int c = st.countTokens();
int sum = 0;
for(int i = 0; i < c; i++)
{
String s1 = st.nextToken();
StringTokenizer st1 = new StringTokenizer(s1);
int c1 = st1.countTokens();
int ll = s1.length();
sum = sum + ll;
char ch = s.charAt(sum++);
String a[] = new String[c1];
for(int j = 0; j < c1; j++)
{
a[j] = st1.nextToken();
}
for(int m = 0; m < c1; m++)
{
for(int n = 1; n < c1 - m; n++)
{
if(a[n - 1].compareTo(a[n]) > 0)
{
String temp = a[n];
a[n] = a[n - 1];
a[n - 1] = temp;
}
}
}
for(int j = 0; j < c1; j++)
{
System.out.print(a[j]+" ");
}
System.out.print(ch);
}
System.out.println();
}
}

OUTPUT
Enter the string
Wow! I did not know that. How did you know?
WOW !DID I KNOW NOT THAT .DID HOW KNOW YOU ?

Variable Description
Name Data Type Purpose
Q25. Write a program to add the no. of days entered to the entered date
according to the Gregorian calendar and give the desired date.

ALGORITHM
SOURCE CODE
import java.util.*;
class date
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Date(dd)");
int dd = sc.nextInt();
System.out.println("Enter Month(mm)");
int mm = sc.nextInt();
System.out.println("Enter Year(yyyy)");
int yyyy = sc.nextInt();
System.out.println("Enter no. of days");
int days = sc.nextInt();
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
int count = 0;
if(yyyy % 4 == 0)
month[2] = 29;
while(count < days)
{
dd++;
count++;
if(dd > month[mm])
{
dd = 1;
mm++;
}
if(mm > 12)
{
mm = 1;
yyyy++;
if(yyyy % 4 == 0)
month[2] = 29;
else
month[2] = 28;
}
}
System.out.println("The Desired Date: "+dd+"-"+mm+"-"+yyyy);
}
}
OUTPUT
Enter Date(dd)
1
Enter Month(mm)
1
Enter Year(yyyy)
1
Enter no. of days
123456789
The Desired Date: 9-4-338007

Variable Description
Name Data Type Purpose
Q26. Write a program to print the inner elements and the corner elements of the
entered matrix.

ALGORITHM
SOURCE CODE
import java.util.*;
class printmat
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("Output Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if((i == 0 || i == (m - 1)) && (j == 0 || j == (m -
1)))
System.out.print(a[i][j]+"\t");
else if(i == 0 || j == 0 || i == (m - 1) || j == (m -
1))
System.out.print(" "+"\t");
else
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter elements
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
Original Matrix
1 2 3
4 5 6
7 8 9
Output Matrix
1 3
5
7 9

Variable Description
Name Data Type Purpose
Q27. Write a program to sort the inner elements of the entered matrix.

ALGORITHM
SOURCE CODE
import java.util.*;
class innersort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
int n = m - 2;
int nn = n * n;
int b[] = new int[nn];
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
int q = 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))
{
}
else
b[q++] = a[i][j];
}
}
for(int i = 0; i < nn; i++)
{
for(int j = 1; j < nn - i; j++)
{
if(b[j - 1] > b[j])
{
int temp = b[j - 1];
b[j - 1] = b[j];
b[j] = temp;
}
}
}
q = 0;
System.out.println("Output Matrix");
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))
System.out.print(a[i][j]+"\t");
else
System.out.print(b[q++]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter N
3
Enter elements
Row 1
2
5
9
Row 2
7
6
3
Row 3
1
9
4
Original Matrix
2 5 9
7 6 3
1 9 4
Largest Number = 9
Row = 1
Column = 3
Smallest Number = 1
Row = 3
Column = 1
Largest Number = 9
Row = 3
Column = 2
Rearranged Matrix
1 2 3
4 5 6
7 9 9

Variable Description
Name Data Type Purpose
Q28. Write a program to enter a number and check whether it’s an evil number or
not.

ALGORITHM
SOURCE CODE
import java.util.*;
class evil
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
if(n > 2 && n < 100)
{
int nn = n;
int bi = 0;
int i = 0;
int count = 0;
while(nn > 0)
{
bi = bi + (int)((nn % 2) * Math.pow(10, i++));
if(nn % 2 == 1)
count++;
nn = nn / 2;
}
System.out.println("Binary Equivalent of "+n+" = "+bi);
if(count % 2 == 0)
System.out.println("Evil number");
else
System.out.println("Not an Evil number");
}
else
System.out.println("Out of Range");
}
}
OUTPUT
Enter number
12
Binary Equivalent of 12 = 1100
Evil number

Variable Description
Name Data Type Purpose
Q29. Write a program to enter three characters and form a matrix according to
the given sample output.
Input:
Enter M
3
Enter characters 1, 2 and 3
*
:
.
Output Matrix
* : *
: . :
* : *

ALGORITHM
SOURCE CODE
import java.util.*;
class mat
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
char a[][] = new char[m][m];
System.out.println("Enter characters 1, 2 and 3");
char c1 = sc.next().charAt(0);
char c2 = sc.next().charAt(0);
char c3 = sc.next().charAt(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))
a[i][j] = c2;
else
a[i][j] = c3;
if((i == 0 || i == (m - 1)) && (j == 0 || j == (m -
1)))
a[i][j] = c1;
}
}
System.out.println("Output Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
6
Enter characters 1, 2 and 3
@
#
$
Output Matrix
@ # # # # @
# $ $ $ $ #
# $ $ $ $ #
# $ $ $ $ #
# $ $ $ $ #
@ # # # # @

Variable Description
Name Data Type Purpose
Q30. Write a program to reverse each number in the matrix entered.

ALGORITHM
SOURCE CODE
import java.util.*;
class matRev
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("Output Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
int rev = 0, rem = 0, p = 0;
int num = a[i][j];
int n = num;
while(num > 0)
{
p++;
num = num / 10;
}
while(n > 0)
{
rem = n % 10;
rev = rev + (int)(rem * Math.pow(10, --p));
n = n / 10;
}
System.out.print(rev+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter elements
Row 1
125
58
22
Row 2
447
256
888
Row 3
3265
154
3525
Original Matrix
125 58 22
447 256 888
3265 154 3525
Output Matrix
521 85 22
744 652 888
5623 451 5253

Variable Description
Name Data Type Purpose
Q31. Write a program to check whether the entered number is an Armstrong
number.

ALGORITHM
SOURCE CODE
import java.util.*;
class ArmNum
{
int n, l, co;
ArmNum(int nn)
{
n = nn;
co = 0;
}
public int leng(int q)
{
if(q > 0)
return (1 + leng((q / 10)));
else
return 0;
}
public int sum_pow(int i)
{
if(co == 0)
l = leng(i);
int rem;
if(i > 0)
{
rem = i % 10;
co++;
return (sum_pow((i / 10)) + (int)Math.pow(rem, l));
}
else
return 0;
}
public void isArmstrong()
{
if(n == sum_pow(n))
System.out.println("Armstrong Number");
else
System.out.println("Not an Armstrong Number");
}
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number");
int num = sc.nextInt();
ArmNum ob = new ArmNum(num);
ob.isArmstrong();
}
}
OUTPUT
Enter Number
153
Armstrong Number

Variable Description
Name Data Type Purpose
Q32. Write a program to print three strings in banner form.

ALGORITHM
SOURCE CODE
import java.util.*;
class banner
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 3 strings");
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String s3 = sc.nextLine();
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
int max = Math.max(l1, Math.max(l2, l3));
if(l1 == max && l2 != max && l3 != max)
{
for(int i = l2; i < max; i++)
s2 = s2 + " ";
for(int i = l3; i < max; i++)
s3 = s3 + " ";
}
else if(l2 == max && l1 != max && l3 != max)
{
for(int i = l1; i < max; i++)
s1 = s1 + " ";
for(int i = l3; i < max; i++)
s3 = s3 + " ";
}
else if(l3 == max && l2 != max && l1 != max)
{
for(int i = l2; i < max; i++)
s2 = s2 + " ";
for(int i = l1; i < max; i++)
s1 = s1 + " ";
}
for(int i = 0; i < max; i++)
System.out.println(s1.charAt(i)+" "+s2.charAt(i)+"
"+s3.charAt(i));
}
}
OUTPUT
Enter 3 strings
Ferrari
Pagani
Spaghetti
F P S
e a p
r g a
r a g
a n h
r i e
i t
t
i

Variable Description
Name Data Type Purpose
Q33. Write a program to print a matrix with the sums of each columns
and rows.

ALGORITHM
SOURCE CODE
import java.util.*;
class rowcol
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Original Matrix");
for(int i = 0; i < m; i++)
{
int sum = 0;
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j]+"\t");
sum = sum + a[i][j];
}
System.out.print("|\t"+sum);
System.out.println();
}
for(int i = 0; i < m; i++)
{
int sum = 0;
for(int j = 0; j < m; j++)
{
sum = sum + a[j][i];
}
System.out.print(sum+"\t");
}
}
}
OUTPUT
Enter M
3
Enter elements
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
Original Matrix
1 2 3 | 6
4 5 6 | 15
7 8 9 | 24
12 15 18

Variable Description
Name Data Type Purpose
Q34.

ALGORITHM
SOURCE CODE
import java.util.Scanner;
public class EqMat
{
private int a[][];
private int m, n;
public EqMat(int mm, int nn)
{
this.m = mm;
this.n = nn;
a = new int[m][n];
}
public void readArray()
{
Scanner s = new Scanner(System.in);
for(int i = 0; i < this.m; i++)
{
for(int j = 0; j < this.n; j++)
{
a[i][j] = s.nextInt();
}
}
}
public int[][] getArray()
{
return this.a;
}
public int getM()
{
return this.m;
}
public int getN()
{
return this.n;
}
public int check(EqMat P, EqMat Q)
{
int p_array[][] = P.getArray();
int q_array[][] = Q.getArray();
int p_m = P.getM(), p_n = P.getN();
int q_m = Q.getM(), q_n = Q.getN();
if(p_m != q_m)
return 0;
if(p_n != q_n)
return 0;
for(int i = 0; i < p_m; i++)
{
for(int j = 0; j < p_n; j++)
{
if(p_array[i][j] != q_array[i][j])
return 0;
}
}
return 1;
}
public void print()
{
for(int i = 0; i < this.m; i++)
{
for(int j = 0; j < this.n; j++)
{
System.out.print(this.a[i][j]+" ");
}
System.out.println();
}
}
public static void main(String arg[])
{
EqMat P = new EqMat(3, 3);
EqMat Q = new EqMat(3, 3);
P.readArray();
P.readArray();
System.out.println(P.check(P, Q));
P.print();
Q.print();
}
}
OUTPUT
Enter array
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
Enter array
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
0
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 0

Variable Description
Name Data Type Purpose
Q35. Write a program to print the upper and the lower triangular matrix of the
array entered.

ALGORITHM
SOURCE CODE
import java.util.*;
class trimat
{
public static void main(String arg [])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int a[][] = new int[m][m];
System.out.println("Enter numbers of array 'a'");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Upper Triangular Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if(i > j)
System.out.print(" "+"\t");
else
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Lower Triangular Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if(i < j)
System.out.print(" "+"\t");
else
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter numbers of array 'a'
Row 1
1
2
3
Row 2
4
5
6
Row 3
7
8
9
Array:
1 2 3
4 5 6
7 8 9
Upper Triangular Array:
1 2 3
5 6
9
Lower Triangular Array:
1
4 5
7 8 9

Variable Description
Name Data Type Purpose
Q36. Write a program to find the prim-orial(prime factorial) of the number
entered.

ALGORITHM
SOURCE CODE
import java.util.*;
class primorial
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
int prod = 1;
for(int i = 1; i <= n; i++)
{
int l = 2;
int count = 0;
while(l <= i)
{
if(i % l == 0)
count++;
l++;
}
if(count > 2)
{
prod = prod * i;
}
}
System.out.println(prod);
}
}
OUTPUT
Enter number
12
2310

Variable Description
Name Data Type Purpose
Q37. Write a program to print the reverse of the string entered(sentence
backwards).

ALGORITHM
SOURCE CODE
import java.util.*;
class reverse
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
int l = s.length();
StringTokenizer st = new StringTokenizer(s);
int c = st.countTokens();
String a[] = new String[c];
for(int i = 0; i < c; i++)
{
String s1 = st.nextToken();
a[i] = s1;
}
for(int k = c - 1; k >= 0; k--)
{
System.out.print(a[k]+" ");
}
}
}
OUTPUT
Enter the string
I might as well go mad
mad go well as might I

Variable Description
Name Data Type Purpose
Q38. Write a program to give the maximum and the minimum value in an array
and print the sorted array.

ALGORITHM
SOURCE CODE
import java.util.*;
class maxmin
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
System.out.println("Enter N");
int n = sc.nextInt();
int a[][] = new int[m][n];
int nn = m * n;
int b[] = new int[nn];
int max, min;
System.out.println("Enter elements");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < n; j++)
a[i][j] = sc.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]+"\t");
System.out.println();
}
int q = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
b[q++] = a[i][j];
}
}
for(int i = 0; i < nn; i++)
{
for(int j = 1; j < nn - i; j++)
{
if(b[j - 1] > b[j])
{
int temp = b[j - 1];
b[j - 1] = b[j];
b[j] = temp;
}
}
}
max = b[nn - 1];
min = b[0];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(a[i][j] == max)
{
System.out.println("Largest Number = "+max);
System.out.println("Row = "+(i + 1));
System.out.println("Column = "+(j + 1));
}
if(a[i][j] == min)
{
System.out.println("Smallest Number = "+min);
System.out.println("Row = "+(i + 1));
System.out.println("Column = "+(j + 1));
}
}
}
q = 0;
System.out.println("Rearranged Matrix");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(b[q++]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter N
4
Enter elements
Row 1
6
5
8
2
Row 2
1
5
9
7
Row 3
3
5
4
8
Original Matrix
6 5 8 2
1 5 9 7
3 5 4 8
Smallest Number = 1
Row = 2
Column = 1
Largest Number = 9
Row = 2
Column = 3
Rearranged Matrix
1 2 3 4
5 5 5 6
7 8 8 9

Variable Description
Name Data Type Purpose
Q39.

ALGORITHM
SOURCE CODE
import java.util.*;
class repalin
{
public int rev(int num)
{
int rem = 0;
int sum = 0;
int n = num;
int i = 0;
while(n > 0)
{
rem = n % 10;
sum = sum + (int)(rem * Math.pow(10, i++));
n = n / 10;
}
return sum;
}
public static void main(String args[])
{
repalin ob = new repalin();
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
System.out.println(ob.rev(n));
int sum = 0;
int i = 0;
while((i == 0)||(sum != ob.rev(sum)))
{
i = 1;
sum = sum + n + ob.rev(n);
n = sum;
}
}
}
Variable Description
Name Data Type Purpose
Q40. Write a program to sort the boundary of an array clockwise.

ALGORITHM
SOURCE CODE
import java.util.*;
class spiral_sort
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int m = sc.nextInt();
int A[][] = new int[m][m];
int B[] = new int[m * m];
int sum = 0;
System.out.println("Enter the array");
for(int i = 0; i < m; i++)
{
System.out.println("Row "+(i + 1));
for(int j = 0; j < m; j++)
{
A[i][j] = sc.nextInt();
}
}
System.out.println("Original Array:");
for (int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int 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)
{
B[x] = A[i][j];
x++;
sum = sum + A[i][j];
}
}
}
for(int i = 0; i < (x - 1); i++)
{
for(int j = 0; j < (x - i - 1); j++)
{
if(B[j] > B[j + 1])
{
int temp = B[j];
B[j] = B[j + 1];
B[j + 1] = temp;
}
}
}
int R1 = 0, R2 = m - 1, C1 = 0, C2 = m - 1; x = 0;
for(int i = C1; i <= C2; i++)
{
A[R1][i] = B[x++];
}
for(int i = R1 + 1; i <= R2; i++)
{
A[i][C2] = B[x++];
}
for(int i = C2 - 1; i >= C1; i--)
{
A[R2][i] = B[x++];
}
for(int i = R2 - 1; i >= R1 + 1; i--)
{
A[i][C1] = B[x++];
}
System.out.println("Sorted Array:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Enter M
3
Enter the array
Row 1
9
8
7
Row 2
4
1
2
Row 3
3
6
5
Original Array:
9 8 7
4 1 2
3 6 5
Sorted Array:
2 3 4
9 1 5
8 7 6

Variable Description
Name Data Type Purpose

Potrebbero piacerti anche