[go: up one dir, main page]

0% found this document useful (0 votes)
8 views109 pages

Computer Science

The document outlines a computer science project for the academic year 2024-25, requiring students to create a word file containing Java programs with specific details such as algorithms, outputs, and variable tables. It includes instructions for formatting and submission, as well as several example programs that involve date validation, prime-palindrome integers, Kaprekar numbers, composite magic numbers, and digit-to-word conversion. Each program must adhere to a structured format and be printed for submission.

Uploaded by

priyanshikansall
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)
8 views109 pages

Computer Science

The document outlines a computer science project for the academic year 2024-25, requiring students to create a word file containing Java programs with specific details such as algorithms, outputs, and variable tables. It includes instructions for formatting and submission, as well as several example programs that involve date validation, prime-palindrome integers, Kaprekar numbers, composite magic numbers, and digit-to-word conversion. Each program must adhere to a structured format and be printed for submission.

Uploaded by

priyanshikansall
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/ 109

COMPUTER SCIENCE

PROJECT 2024-25
XII-Sci/Com

Read the following instructions before you start with the project:

- The programs should be done in the computer lab.

- You will be allowed to take the project programs in a pen drive.

- You are supposed to create a word file which will consist of all project

programs.

- Creating and editing of the word file of your bluej programs can be done at

home.

-First page should be with school logo and complete student information

along with roll no.

- Index should be the second page which will consist of Sr. No. , Program

and Page No. in tabular format.

-Each program should contain following details:

 Question

 Algorithm

 Program with comments

 Output

 Variable used table

 The project should be submitted as the printout of the given programs

 Write all the programs using Java method only.

 Date of submission will be informed later.


Program 1

Input: Enter your date of birth in dd mm yyyy format

05

01

2010

Output: VALID DATE

Input: Enter your date of birth in dd mm yyyy format

03

04

2010

Output: VALID DATE

Input: Enter your date of birth in dd mm yyyy format

34

06

2010

Output: INVALID DATE

ALGORITHM-

Step 1 :start

Step 2:display ‘enter the year is four digits’

Step 3:read year


Step 4:if(year is divisible by 100)

{
if(year is divisible by 400)

display ‘the year is a leap year’

Step 5: else if(year is divisible by 4)

display’this is a leap year’

Step 6 else

display ‘not a leap year’

Step 7: stop

class data

public static void main(int dd,int mm,int yy)

\\to check if month consists 31 days

if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)

{
if(dd<=31&&dd>=1)

System.out.println("VALID DATE");

else

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

if(mm==4||mm==6||mm==9||mm==11)

\\to check if month consists 30 days

if(dd<=30&&dd>=1)

System.out.println("VALID DATE");

else

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

\\if the month is feburary (leap year)

if(mm==2)

if(yy%4==0)

{
if(dd<=29&&dd>=1)

System.out.println("VALID DATE");

else

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

else

if(dd<=28&&dd>=1)

\\to check if it is valid date

System.out.println("VALID DATE");

else

\\to print invalid date

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

}
}

OUTPUT:

Output: VALID DATE

Output: VALID DATE

Output: INVALID DATE

VARIABLE USED TABLE:


Variable Data Type Description

M int Number of rows in the matrix

N int Number of columns in the matrix

x[][] int[][] 2D array representing the matrix

i int Loop counter for columns

j int Loop counter for columns

k int Loop counter for selection sort

small int Current smallest element in the array

pos int Position of the smallest element

temp int Temporary variable for swapping elementsa

dd int Stores the day of the month (e.g., 01)

mm int Stores the month (e.g., 02)

yy int Stores the year (e.g., 2010)


Program 2

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 contain two positive integers m and n, where m<3000 and n<3000. 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 some random data:

Example 1

Input : m=100

n=1000

Output: The prime palindrome integers are:

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

Frequency of prime palindrome integers:15

Example 2

Input : m=100

n=5000

Output : Out of Range

ALGORITHM-

Step 1: start

Step 2: read n

Step 3: flag0
Step 4: for(i2;in;ii+1)

if(n is divisible by i)

flag1

Step 5: if(flag is 0)

display ‘prime’

else

display ‘not’

Step 6: for(i1;number>0;i++)

remnumber%10

revrev*10+rem

numbernumber/10

Step 7:if(temp is equals to rev)

display ‘the number is a palindrome’

}
else

display ‘the number is not a palindrome’

Step 8:stop

class Palinlimit

int is_prime(int n)

\\to check whether it is prime numer

int i,j,flag=0;

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

if(n%i==0)

flag=1;

break;

if(flag==0)

return 1;
}

else

return 0;

int is_palin(int n)

\\to check whether it is palindrome integers

int rev=0,rem,temp=n;

while(n>0)

rem=n%10;

rev=rev*10+rem;

n=n/10;

\\to check the necessary condition

if(rev==temp)

return 1;

else
{

return 0;

public static void main(int n ,int m)

int i,cnt=0;

Palinlimit ob1=new Palinlimit();

if(m<3000&&n<3000)

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

int R1=ob1.is_prime(i);

int R2=ob1.is_palin(i);

if(R1==1&&R2==1)

System.out.println(i);

cnt++;

\\print the frequency of prime palindrome integers

System.out.println("Frequecny of prime palindrome integers="+cnt);

else
{

\\print the out of range

System.out.println("Out of range");

OUTPUT:

Output: The prime palindrome integers are:

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

Frequency of prime palindrome integers:15

Output : Out of Range

VARIABLE USED TABLE:


Variable Name Data Type Description

n int The number being checked for prime

i int Loop counter used in is Prime

flag int Flag to indicate whether a number is prime (0for prime, 1 for non-prime)

rev int The reversed form of the number being checked for palindrome

temp int Temporary variable used to store the original number in isPalin

m int The lower bound of the range

cnt int The count of prime-palindrome integers

R1 int Result of isPrime function

R2 int Result of isPalin function


Program 3

Kaprekar number or not-:

Given the two positive integers p and q, where p < q. Write a program to determine how

many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and

output them.

About 'kaprekar' number: A positive whole number 'n' that has 'd' number of digits is

squared and split into 2 pieces, a right hand piece that has 'd' digits and a left hand

piece that has remaining 'd' or 'd-1' digits. If sum of the pieces is equal to the number

then it's a kaprekar number.

Example:

297 is a Kaprekar number for base 10, because 297² = 88209, which can be split into

88 and 209, and 88 + 209 = 297. By convention, the second part may start with the digit

0, but must be positive. For example, 999 is a Kaprekar number for base 10, because

999² = 998001, which can be split into 998 and 001, and 998 + 001 = 999. But 100 is

not; although 100² = 10000 and 100 + 00 = 100, the second part here is not positive.

INPUT:

p=1

Q=1000

OUTPUT:

THE KAPREKAR NUMBERS ARE:

1,9,45,55,99,297,703,999

FREQUENCY OF KAPREKAR NUMBERS IS:8


ALGORITHM-

Step 1:start

Step 2:read i,rem,cnt=0,rev=0,temp=n

Step 3 :while(n>0)

rem=n is divisible to 10;

cnt++;

n=n divide by 10;

Step 4: if(first+second==temp)

return 1;

else

return 0;

Step 5:stop
class kaprekar

int is_kar(int n)

\\ to intialize the data values

int i,rem,cnt=0,rev=0,temp=n;

int sq=n*n;

while(n>0)

rem=n%10;

cnt++;

n=n/10;

int div=(int)Math.pow(10,cnt);

int second=sq%div;

int first=sq=sq/div;

if(first+second==temp)

return 1;

else

return 0;

}
}

public static void main(int p,int q)

\\create the object class

kaprekar ob1=new kaprekar();

int i,count=0;

\\print the kaprekar number

System.out.println("THE KAPREKAR NUMBER ARE");

for(i=p;i<=q;i++)

int R=ob1.is_kar(i);

if(R==1)

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

count++;

\\to print the frequency of kaprekar number

System.out.println("\n FREQUENCY OF KAPREKAR NUMBERS IS :"+count);

}
OUTPUT:

OUTPUT:

THE KAPREKAR NUMBERS ARE:

1,9,45,55,99,297,703,999

FREQUENCY OF KAPREKAR NUMBERS IS:8

VARIABLE TABLE:
Variable Name Data Type Description

scanner Scanner A Scanner object to read input from the console.

p int The starting value of the range.

q int The ending value of the range.

count int A counter for the number of Kaprekar numbers found.

i int A loop variable used to iterate through the range.

num int The number being checked for the Kaprekar property.

square long The square of the number.

numDigits int The number of digits in the number.

divisor long A divisor used to split the square into two parts.

rightPart long The right part of the square after splitting.

leftPart long The left part of the square after splitting.


Program 4

A composite Magic number is a positive integer which is composite as well as a magic

number

Composite number: A composite number is a number that has more than two factors.

Factors are: 1,2,5,10

Magic number: A magic number is a number in which the eventual sum of the digits is

equal to 1.

Accept two positive integers m and n, where m is less than n as user input. Display the

number Composite Magic integers that are in the range between m and n(both

inclusive) and output them along with the frequency, in the format specified below.

Example 1:

Input : m=10 n=100

Output:

The composite magic integers are:

10,28,46,55,64,82,91,100

Frequency of composite magic integers is: 8

Example 2:

Input : m=120 n=99

Output:

Invalid input
ALGORITHM-

Step 1:start

Step 2: read values int i,flag0;

Step 3: for(i=2;i<num;i++)

if(numis divisible by==0)

flag1;

break;

Step 4:if(flag==1)

return 1;

else

return 0;

Step 5: while(num>9)

sum=0;

while(num>0)
num=sum;

Step 6:stop

import java.util.*;

class composite_magic

int is_composite(int num)

int i,flag=0;

\\to check whether it is composite integers

for(i=2;i<num;i++)

if(num%i==0)

flag=1;

break;

\\check if flag is fulfilling the condition

if(flag==1)

return 1;

}
else

return 0;

int is_magic(int num)

int rem,sum=0;

\\to check whether it is magic number

while(num>9)

sum=0;

while(num>0)

rem=num%10;

sum=sum+rem;

num=num/10;

num=sum;

if(num==1)

return 1;

}
else

return 0;

public static void main(int m,int n)

composite_magic ob1=new composite_magic();

int k,cnt=0;

if(m<n)

\\print the composite magic integers

System.out.println("The composite magic integrers are");

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

int R1=ob1.is_composite(k);

int R2=ob1.is_magic(k);

\\check if R1 is equals to R2

if(R1==1&&R2==1)

System.out.print(k+",");

cnt++;
}

\\print the frequency of composite integers

System.out.println("\n FREQUENCY OF COMPOSITE INTEGERS ARE:");

else

\\print the invalid input

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

}
OUTPUT:

Output:

The composite magic integers are:

10,28,46,55,64,82,91,100

Frequency of composite magic integers is: 8

Output:

Invalid input

VARIABLE TABLE:
Variable Name Data Type Description

m int The lower bound of the range

n int The upper bound of the range

scanner Scanner An object to read input from the user

frequency int The count of composite magic numbers

i int Loop variable for iterating through the range

sum int Variable used to calculate the sum of digits in the isMagic method

number int Variable used to store the current number being checked in the isMagic method
Program 5

Given a number N, the task is to convert every digit of the number into words.

Examples:

Input : N = 1735

Output : One Seven Three Five

Explanation:

Every digit of the given number has been converted into its corresponding word.

Input : N = 597

Output : Five Nine Seven

ALGORITHM-

Step 1:start

Step 2:read the values of n, rem,rev=0

Step 3: while(n>0)

rem=n%10;

rev=rev*10+rem;

n=n/10;

Step 4: while(rev>0)

rem=rev%10;
if(rem==0)

Step 5:display the values

Step 6:stop

import java.util.*;

class word

public static void main()

Scanner sc=new Scanner(System.in);

\\print the enter number

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

int n=sc.nextInt();

\\intialize the data member value

int rem,rev=0;

\\check the condition

while(n>0)

rem=n%10;

rev=rev*10+rem;

n=n/10;

}
\\run the while loop if rev is greater than 0

while(rev>0)

rem=rev%10;

if(rem==0)

\\print the zero

System.out.print("ZERO");

if(rem==1)

System.out.print("ONE");

if(rem==2)

System.out.print("TWO");

if(rem==3)

System.out.print("THREE");

if(rem==4)

System.out.print("FOUR");

}
if(rem==5)

System.out.print("FIVE");

if(rem==6)

System.out.print("SIX");

if(rem==7)

System.out.print("SEVEN");

if(rem==8)

System.out.print("EIGHT");

\\ print check the rem is equals to 9

if(rem==9)

System.out.print("NINE");

rev=rev/10;

}
OUTPUT:

Output : One Seven Three Five

Output : Five Nine Seven

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner An object of the Scanner class used to read input from the keyboard

n int Stores the number entered by the user

rem int Stores the remainder when n is divided by 10


Program 6

Write a program to accept a sentence which may be terminated by either’.’, ‘?’or’!’ only.

The words may be separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

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

(b) Place the words which begin and end with a vowel at the beginning, followed by the

remaining words as they occur in the sentence.

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: HOW ARE YOU@

OUTPUT: INVALID INPUT


ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: for(i0;i<len;i++)

str2 str2 str1.charAt(i)+;

Step 7:

Check the vowels

if( ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||

ch2=='I'||ch2=='E'||ch2=='A'||ch2=='O'||ch2=='U')

display"NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL="+cnt

else
{

display"INVALID INPUT"

Step 8: stop
class pro6

public static void main()

String str1="ANAMIKA AND SUSHAN ARE NEVER GOING TO BE QURAELLING


ANYMORE";

String str2="";

\\intialize the data values

int len,i,cnt=0;

len=str1.length();

String strnew1="",strnew2="",strnew3="";

char check=str1.charAt(len-1);

if(check=='.'||check=='?'||check==',')

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

if(str1.charAt(i)!=' '&&str1.charAt(i)!='.'&&str1.charAt(i)!='?')

str2=str2+str1.charAt(i);

else

\\intialize the length value


int len1=str2.length();

char ch1=str2.charAt(0);

char ch2=str2.charAt(len1-1);

\\check the vowels

if( ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||

ch2=='I'||ch2=='E'||ch2=='A'||ch2=='O'||ch2=='U')

strnew1=strnew1+str2+" ";

cnt++;

else

strnew2=strnew2+str2+" ";

str2="";

strnew3=strnew1+strnew2;

\\print the frequency of words

System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A


VOWEL="+cnt);

System.out.println(strnew3);

\\print the invalid input

else
{

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

OUTPUT:

Example 1

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL= 3

ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Example 2

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

OUTPUT: INVALID INPUT

VARIABLE TABLE:
Variable Name Data Type Description

str1 String Stores the input sentence

str2 String Used to temporarily store each word in the sentence

len int Stores the length of the input sentence

i int Loop counter

cnt int Counts the number of words beginning and ending with a vowel
strnew1 String Stores words beginning and ending with a vowel

strnew2 String Stores words not beginning and ending with a vowel

strnew3 String Stores the final output string

check char Stores the last character of the input sentence

ch1 char Stores the first character of a word

ch2 char Stores the last character of a word

len1 int Stores the length of the temporary word (str2)

rev int Stores the reversed number, initially set to 0

Program 7

Write the program to find the word in sentence. If the word is present in sentence

“search successful” otherwise “the search unsuccessful”

Input:

Enter a string: The quick brown fox jumps over the lazy dog

Enter the word to be searched: The

Output: Search successful

ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty


Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: for(i0;i<len;i++)

str2str1.charAt(i)+str2;

Step 7: A(str1 equals to str2)

display ‘palindrome’

else

display ‘not’

Step 8: stop

import java.util.*;

class proo7

public static void main()

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

\\print the string values

System.out.print("Enter a string");

String str1=sc.nextLine();

\\print the word to be searched

System.out.print("Enter the word to be searched");

String str3=sc.nextLine();

String str2="";

str1=str1+"";

\\intialize the data values

int len,i,cnt=0,flag=0;

\\intialize the length

len=str1.length();

\\run the forloop to check the condition

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

if(str1.charAt(i)!=' ')

str2=str2+str1.charAt(i);

else{

if(str2.equalsIgnoreCase(str3))

{
cnt++;

flag=1;

str2="";

\\print the succesfull search

if(flag==1)

System.out.println("Search succesfull");

else{

System.out.println("Search unsuccesfull");

OUTPUT:

Output: Search successful

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner An object used to read input from the keyboard

str1 String The original input string from the user

str3 String The word to search for in the input string

str2 String A temporary string used to build words from the


input string

len int The length of the input string

i int A loop counter variable

cnt int A counter for the number of occurrences of the

search word

flag int A flag variable used to indicate whether the search word was found (1) or not (0)

Program 8

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 headings along with the words.

Test your program with the following inputs.

Example 1

INPUT: Intelligence plus character is education.

OUTPUT:

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:

Word Vowels Consonants

God 1 2
Is 1 1
Great 2 3

OUTPUT:

INPUT: All the best!

OUTPUT:

Invalid Input.

ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: for(i0;i<len;i++)

{
str2str1.charAt(i)+str2;

Step 7:to check the vowels

Step 8: stop

class pro8

public static void main()

String str1="MASTERING INFORMATION TECHNOLOGY";

String str2="";

\\intialize the data values

int len,cntvow=0,cntcon=0,len1,j,i;

len=str1.length();

str1=str1.toLowerCase();

String strnew="";

\\intialize the length value

len=str1.length();

char check=str1.charAt(len-1);

if(check=='.'||check=='?')

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

if(str1.charAt(i)!=' '&&str1.charAt(i)!='.'&&str1.charAt(i)!='?')

str2=str2+str1.charAt(i);

else

len1=str2.length();

char ch1=str2.charAt(0);

\\convert the first letter uppercase

ch1=Character.toUpperCase(ch1);

strnew=strnew+ch1;

String sub=str2.substring(1,len1);

strnew=strnew+sub+"";

str2="";

System.out.println(strnew);

System.out.println("WORD\t vowel \t consonant");

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

{
char x=strnew.charAt(i);

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

str2=str2+str1.charAt(i);

if(Character.isLetter(x))

\\check the vowels

if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'||x=='A'||

x=='E'||x=='I'||x=='O'||x=='U')

cntvow++;

else{

cntcon++;

else

\\print the frequency of vowes and consonant

System.out.println(str2+"\t"+cntvow+"\t"+cntcon);

cntvow=0;

cntcon=0;

str2="";

}
}

else

System.out.println("invalid input");

OUTPUT:

Example 1

Word Vowels Consonants

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

Example 2

Word Vowels Consonants

God 1 2
Is 1 1
Great 2 3

Example 3

Invalid Input.
VARIABLE TABLE:
Variable Data Type Description

str1 String Stores the input sentence

str2 String Used to store individual words during processing

len int Stores the length of the input string

i int Loop counter

cntvow int Counts the number of vowels in a word

cntcon int Counts the number of consonants in a word

len1 int Stores the length of a word

x char Stores a character from a word for checking vowel/consonant

ch char Stores the first character of a word

ch1 char Stores the uppercase version of the first character

stnew String Stores a word with the first letter capitalized

sub String Stores the substring of a word (excluding the first letter)

Program 9

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 space

Perform the following tasks:

(a). Display the count of palindromic words in the sentence.

(b) Display the palindromic words in the sentence.


Example of palindromic words:

MADA M, ARORA, NOON

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

Example 1

Input : MOM AND DAD ARE COMING AT NOON.

Output : MOM DAD NOON

NUMBER OF PALINDROMIC WORDS:3

Example 2

Input: NITIN ARORA USES LIRIL SOAP.

Output: NITIN ARORA LIRIL

NUMBER OF PALINDOMIC WORDS:3

Example 3

Input: HOW ARE YOU?

Output: NO PALINDROMIC WORDS

ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length


len=str.length()

Step 6: for(i0;i<len;i++)

str2str1.charAt(i)+str2;

Step 7: A(str1 equals to str2)

display ‘palindrome’

else

display ‘not’

Step 8: stop

class pro9

public static void main()

String str1="MOM AND DAD ARE COMING AT NOON";

String str2="";

String str3="";

int len,i,cnt=0;
\\intailize the length

len=str1.length();

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

if(str1.charAt(i)!=' '&&str1.charAt(i)!='.'&&str1.charAt(i)!='?')

\\check if it is palindrome

str2=str1.charAt(i)+str2;

str3=str2+str1.charAt(i);

else

if(str2.equals(str3))

cnt++;

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

str2="";

str3="";

\\print palindrome words with frequency

if(cnt>1)

{
System.out.println("\n NUMBER OF PALINDROME WORDS:"+cnt);

else

\\print no palindrome words

System.out.println("NO PALINDROME WORDS");

OUTPUT:

Example 1

Output : MOM DAD NOON

NUMBER OF PALINDROMIC WORDS:3

Example 2

Output: NITIN ARORA LIRIL

NUMBER OF PALINDOMIC WORDS:3

Example 3

Output: NO PALINDROMIC WORDS

VARIABLE TABLE:
Variable Name Data Type Description

str1 String Stores the input sentence

str2 String Used to build a word in reverse

str3 String Used to build a word in forward order


len int Stores the length of the input sentence

i int Loop counter

cnt int Counts the number of palindromic words

Program 10

Write a program in java to accept a string (Containing three words) and display the

same in reverse order

Input: IT IS COOL

Output: COOL IS IT

ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: for(i0;i<len;i++)

str2str1.charAt(i)+str2;

Step 7: A(str1 equals to str2)

display ‘VALID’
}

else

display ‘INVALID’

Step 8: stop

class pro10

public static void main()

String str1="IT IS COOL";

String strnew="";

str1=""+str1;

\\intalize the empty string

String str2="";

int len,i;

\\intialize the length value

len=str1.length();

\\check If condition is fulfullied

for(i=len-1;i>=0;i--)

{
if(str1.charAt(i)!=' ')

str2=str1.charAt(i)+str2;

else

strnew=strnew+str2+"";

str2="";

System.out.print(strnew);

OUTPUT:

Output: COOL IS IT

VARIABLE TABLE:

Variable Name Data Type Description

str1 String Stores the input string

strnew String Stores the reversed string

str2 String Temporary string to store each word in reverse

len int Stores the length of the string

i int Loop counter


Program 11

Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by

13 places’). It is a simple letter substitution cipher that replaces a letter with the letter 13

places after it in the alphabets, with the other characters remaining unchanged.

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

less than 100.Encrypt the text if valid as per the Caesar Cipher.

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: read n

Step 3: first0

Step 4: second0

Step 5: display first,second

Step 6: for(i2;i<w;ii+1)

third=first+second

display third

firstsecond

secondthird

Step 7: stop

import java.util.*;

class asci

{
public static void main()

Scanner sc=new Scanner(System.in);

\\print the string value

System.out.println("Enter a string");

\\intialize the data values

String str=sc.nextLine();

String strnew="";

char ch;

int as,i,len;

\\intialize the length value

len=str.length();

\\check the length value

if(len>3&&len<100)

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

ch=str.charAt(i);

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

as=(int)ch+13;

ch=(char)as;

strnew=strnew+ch;

}
else

strnew=strnew+ch;

System.out.print(strnew);

else

\\print invalid length

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

OUTPUT:

Example 1

OUTPUT : The cipher text is:

Uryyb? Ubj ner lbh?

Example 2
OUTPUT : The cipher text is:

Rapelcgvba urycf gb frpher qngn.

Example 3

OUTPUT : INVALID LENGTH

VARIABLE TABLE:

Variable Name Data Type Description

sc Scanner An object used to read input from the keyboard

str String The original input string from the user

strnew String The modified output string after the ROT13

cipher is applied

ch char A single character extracted from the string

as int The ASCII value of the character being processed

i int Loop counter

len int The length of the input string

Program 12

Write a program that encodes a word into Piglatin. To translate word into Piglatin word,
convert the word into uppercase and then place the first vowel of the original word as

the start of the new word along with the remaining alphabets. The alphabets present

before the vowel being shifted towards the end followed by “AY”.

Input : London Sample

Output : ONDONLAY

Input : Olympics Sample

Output : OLYMPICSAY

ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: ch=str.charAt(i)

Step 7: for(i0;i<len;i++)

Step 8:

strnew=str2+str3+"AY";

display"The pig latin of the given string:"+strnew

import java.util.*;
class piglatin

public static void main()

Scanner sc=new Scanner(System.in);

\\print the value of string

System.out.print("Enter a string");

String str1=sc.nextLine();

\\to convert in upper case

str1=str1.toUpperCase();

String str2,str3,strnew;

int len,i,start=0;

char ch;

\\intialize the length

len=str1.length();

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

\\check if it is vowels

ch=str1.charAt(i);

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

start=i;

break;
}

\\print the piglatin string

str2=str1.substring(start);

str3=str1.substring(0,start);

strnew=str2+str3+"AY";

System.out.println("The pig latin of the given string:"+strnew);

OUTPUT:

Output : ONDONLAY

Output : OLYMPICSAY

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner An object of the Scanner class used to get input from the user

str1 String Stores the input string from the user

str2 String Stores the substring of str1 starting from the first vowel

str3 String Stores the substring of str1 before the first vowel

strnew String Stores the final Pig Latin word

len int Stores the length of str1

i int Loop counter variable

start int Stores the index of the first vowel in str1

ch char Stores the character at each position in str1 during the loop
Program 13

Write a program to accept a sentence as input. The words in the string are to be

separated by a blank. Each word must be in upper case. The sentence is terminated by

either “.” or “?”. Perform the following tasks:

Obtain the length of the sentence (measured in words)

Arrange the sentence in alphabetical order of the words

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

Example 1:

INPUT : NECESSITY IS THE MOTHER OF INVENTION.

OUTPUT: Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

Example 2:

INPUT : BE GOOD TO OTHERS.

OUTPUT: Length: 4

Rearranged Sentence: BE GOOD OTHERS TO

ALGORITHM-

Step 1: start
Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: char chstr1.charAt(i);

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

Step 7: display"Length:"+cnt

display’Rearranged sentence’

Step 8:stop

class pro13

void sort(String str1)

String str2="";

\\to initialize the data members value

int len,i,j,cnt=0,k=0;

\\to initialize the length

len=str1.length();

\\to store the value temporary


String temp;

char check=str1.charAt(len-1);

if(check=='.'||check=='?')

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

char ch=str1.charAt(i);

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

++cnt;

String a[]=new String[cnt];

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

if(str1.charAt(i)!=' '&&str1.charAt(i)!='?'&&str1.charAt(i)!='.')

str2=str2+str1.charAt(i);

else

a[k++]=str2;

str2="";

}
for(i=0;i<cnt-1;i++)

for(j=0;j<cnt-1;j++)

if(a[j].compareTo(a[j+1])>0)

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

\\to print the length frequency

System.out.println("Length:"+cnt);

\\to print the rearranged sentence

System.out.println("Rearranged sentence:");

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

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

else

\\to print the invalid input

System.out.println("Invalid input");
}

public static void main()

pro13 ob1=new pro13();

String str1="NECCESITY IS THE MOTHER OF INVENTION";

ob1.sort(str1);

OUTPUT:

Example 1:

OUTPUT: Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

Example 2:

OUTPUT: Length: 4

Rearranged Sentence: BE GOOD OTHERS TO

VARIABLE TABLE:

Variable Name Data Type Description

scanner Scanner An object to read input from the console

sentence String The input sentence


words String[] An array of words extracted from the sentence

length int The number of words in the sentence

Program 14

Accept a paragraph of text consisting of sentences that are terminated by either “.”, ”,”

, ”!” or a “?” followed by a space. Assume that there can be maximum of 05 sentences

in a paragraph.

Design a program to perform the following:

(a). Arrange the sentences in alphabetical order of words, sentence by sentence

(b) Separate the words which begin with a vowel.

Example 1:

Input: HELLO! HOW ARE YOU? WHEN ARE YOU COMING? HOPE TO SEE

YOU SOON.

Output: HELLO! ARE HOW YOU? ARE COMING WHEN YOU? HOPE SEE

SOON TO YOU

VOWELS: ARE

Example 2:

Input: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG

Output: BROWN DOG FOX JUMPED LAZY OVER QUICK THE THE VOWEL:

OVER
ALGORITHM-

Step 1: start

Step 2: str as String

Step 3: set str2 is empty

Step 4: read str1

Step 5: find str1 length

len=str.length()

Step 6: for(i0;i<len;i++)

str2str1.charAt(i)+str2;

Step 7:to check the vowels

Step 8:stop

class paragraph

void sort(String str1,char ch1)

\\intialize the values

str1=str1+"";

String str2="";
char ch;

int len,i,j,k=-1,cnt=0;

\\intialize the length

len=str1.length();

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

ch=str1.charAt(i);

if(ch==' ')

++cnt;

String a[]=new String[cnt];

\\to store the value temporary in string

String temp;

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

if(str1.charAt(i)!=' ')

str2=str2+str1.charAt(i);

else

a[++k]=str2;
}

\\sorting technique

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

for(j=0;j<cnt-1-i;j++)

if(a[j].compareTo(a[j+1])>0)

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

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

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

System.out.print(ch1);

void is_vowel(String str1)

\\intialize the values


str1=str1+"";

int len,i;

String str2="",strnew="";

len=str1.length();

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

if(str1.charAt(i)!=' ')

str2=str2+str1.charAt(i);

else

\\to check the vowels

char ch=str2.charAt(0);

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

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

str2="";

}
public static void main()

\\create the object class

paragraph ob1=new paragraph();

String par="HELLO HOW ARE YOU ?WHEN ARE YOU COMING ?HOPE TO
SEE YOU SOON";

System.out.println(par)

\\intialize the length

int len=par.length();

int p;

String str1="";

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

char ch1=par.charAt(p);

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

str1=str1+ch1;

else

ob1.sort(str1,ch1);

str1="";

}
}

\\print the vowels

System.out.print("\n vowels");

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

char ch1=par.charAt(p);

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

str1=str1+ch1;

else

str1=str1.trim();

ob1.is_vowel(str1);

str1="";

OUTPUT:

Example 1:

YOU SOON.

Output: HELLO! ARE HOW YOU? ARE COMING WHEN YOU? HOPE SEE

SOON TO YOU

VOWELS: ARE
Example 2:

Output: BROWN DOG FOX JUMPED LAZY OVER QUICK THE THE VOWEL:

OVER

VARIABLE TABLE:
Variable Name Data Type Description

scanner Scanner Object to read input from the console

paragraph String The input paragraph

sentences String[] Array of sentences in the paragraph

words String[] Array of words in a sentence

i int Loop counter

Program 15

Strong Matrix

Write a program to enter a square matrix of size r. Check whether given matrix is Strong

Matrix or not?A Strong matrix is the one where each row addition, each column addition

and each diagonal addition is same.

Example

r=3

816

357

492

r=5

17 24 1 8 15

23 5 7 14 16
4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

ALGORITHM-

Step 1: start

Step 2: declare 2 two dimensional array

int a[][]=new int[n][n], int b[]=new int[(n*2)+2];

Step 3:input the no. of rows ‘n’ and no of column ‘n’ to be inputed

Step 4: set the first loop for i=1

Step 5: set the first loop for j=1

Step 6: input the values a[i][j] and b[i][j]

Assign C[i][j]= A[i][j]- B[i][j]

Step 7: continue 2nd loop till j=n;

Step 8: continue 1st loop till i=n;

Step 9: display Orignal matrix

Step 10: stop

import java.util.*;

class strong

public static void main()

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

\\to initialize the data values

int i,j,n,sum=0,k=0,compare,flag=0;

\\to print the n value

System.out.print("enter a value of n");

n=sc.nextInt();

\\to store value in array

int a[][]=new int[n][n];

int b[]=new int[(n*2)+2];

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

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

System.out.print("enter any number");

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

//sum of each row

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

sum=0;

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

{
sum=sum+a[i][j];

b[k++]=sum;

//sum of each column

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

sum=0;

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

sum=sum+a[j][i];

b[k++]=sum;

//sum of left diagnol

sum=0;

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

sum=0;

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

if(i==j)

{
sum=sum+a[i][j];

b[k++]=sum;

//sum of right diagnol

sum=0;

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

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

if(i+j==(n-1))

sum=sum+a[i][j];

b[k++]=sum;

\\print the original matrix

System.out.println("Orignal matrix");

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

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

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

System.out.println();

compare=b[0];

for(k=0;k<(n*2)+2;k++)

if(compare!=b[k])

flag=1;

\\print the strong matrix

if(flag==0)

System.out.println("strong matrix");

else

System.out.println("Not a strong matrix");

}
OUTPUT:

r=3

816

357

492

r=5

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner A Scanner object to read input from the user

n int The size of the square matrix

a int[][] The 2D array representing the matrix

b int[] An array to store the sums of rows, columns, and diagonals

i int Loop counter for rows

j int Loop counter for columns

k int Loop counter for the b array


sum int The sum of elements in a row, column, or diagonal compare

flag int A flag to indicate whether the matrix is strong (0) or not (1)

Program 16

Symmetric matrix

Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the

number of rows and the number of columns such that M must be greater than 2 and

less than 10. Accept the value of M as user input. Display an appropriate message for

an invalid input. Allow the user to input integers into this matrix. Perform the following

tasks:

(a) Display the original matrix.

(b) Check if the given matrix is Symmetric or not. A square matrix is said to be

symmetric. if the element of the ith row and jth column is equal to the element of

the jth row and ith column

(c) Find the sum of the elements of left diagonal and the sum of the elements of

right Diagonal of the matrix and display them.

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

Example

INPUT : M = 3

1 2 3

2 4 5

3 5 6
OUTPUT :

ORIGINAL MATRIX

1 2 3

2 4 5

3 5 6

THE GIVEN MATRIX IS SYMMETRIC

The sum of the left diagonal = 11

The sum of the right diagonal = 10

Example 2

INPUT : M = 22

OUTPUT : THE MATRIX SIZE IS OUT OF RANGE

ALGORITHM-

Step 1: start

Step 2: declare int x[][]=new int[M][M];

Step 3:input the no. of rows ‘M’ and no of column ‘M’ to be inputed

Step 4: set the first loop for i=1 to M and : set the first loop for j=1 to M

Step 5: display original matrix

Step 6:set the limit to M >2 and M<10

Step 7: continue 2nd loop till j=M;

Step 8: continue 1st loop till i=M;


Step 9:display symmetric matrix

Step 10: stop

import java.util.*;

class symmetric

public static void main()

Scanner sc=new Scanner(System.in);

\\intialize the data members

int i,j,M,n,sum=0,k=0,compare,flag=0;

System.out.print("enter a value of M");

M=sc.nextInt();

int x[][]=new int[M][M];

if(M>2&&M<10)

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

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

System.out.print("enter any number");


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

\\print the original matrix

System.out.println("Orignal matrix");

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

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

System.out.print(x[i][j]+"\t");

System.out.println();

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

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

if(x[i][j]!=x[j][i])

flag=1;

}
\\print the symmetric matrix

if(flag==0)

System.out.println("The given matrix is symmetric");

else

System.out.println("The given matrix is not symmetric");

//sum of left diagnol

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

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

if(i==j)

sum=sum+x[i][j];

\\ print sum of left diagnol

System.out.println("The sum of the left diagnol+"+sum);

//sum of right diagnol


sum=0;

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

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

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

sum=sum+x[i][j];

\\print the sum of rigjt diagnol

System.out.println("the sum of right diagnol="+sum);

else

System.out.println("the matrix size is out of range");

OUTPUT:

OUTPUT :

ORIGINAL MATRIX
1 2 3

2 4 5

3 5 6

THE GIVEN MATRIX IS SYMMETRIC

The sum of the left diagonal = 11

The sum of the right diagonal = 10

Example 2

OUTPUT : THE MATRIX SIZE IS OUT OF RANGE

VARIABLE TABLE:
Variable Name Data Type Description

M int The order of the square matrix (number of rows and columns)

A int[][] The 2D array representing the matrix

scanner Scanner Object for reading input from the user

i int Loop counter variable for rows

j int Loop counter variable for columns

isSymmetric boolean Flag to indicate whether the matrix is symmetric or not

leftDiagonalSum int Sum of the elements on the left diagonal

rightDiagonalSum int Sum of the elements on the right diagonal

Program 17

Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the

number of rows and the number of columns such that M must be greater than 2 and
less than 20. Allow the user to input integers into this matrix. Display appropriate error

message for an invalid input. Perform the following tasks:

(a) Display the input matrix

(b) Crete a mirror image matrix

(c) Display the mirror image matrix.

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

Example 1

INPUT : M = 3

4 16 12

8 2 14

413

OUTPUT :

ORIGINAL MATRIX

4 16 12

8 2 14

413

MIRROR IMAGE MATRIX

12 16 4

14 2 8

3 16

Example 2

INPUT : M = 22

OUTPUT : SIZE OUT OF RANGE


ALGORITHM-

Step 1: start

Step 2: declare 2 two dimensional array A[M][M], B[M][M]

Step 3:input the no. of rows ‘M’ and no of column ‘M’ to be inputed

Step 4: set the first loop for i=1 to M and set the first loop for j=1 to M

Step 5: to set the limit M>2 and M<20

Step 6: input the values A[i][j] and B[i][j]

Step 7: continue 2nd loop till j=n and continue 1st loop till i=m;

Step 8: for(j=M-1;j>=0;j--)

Step 9: display mirror image

Step 10: stop

import java.util.*;

class mirror

public static void main()

Scanner sc=new Scanner(System.in);

\\intialize the data members

int i,j,M,n,sum=0,k=0,compare,flag=0;
System.out.print("enter a value of M");

M=sc.nextInt();

\\intialize the array

int A[][]=new int[M][M];

int B[][]=new int[M][M];

if(M>2&&M<20)

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

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

System.out.print("enter any number");

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

\\print the original matrix

System.out.println("Orignal matrix");

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

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

System.out.print(A[i][j]+"\t");

System.out.println();
}

\\print the mirror image

System.out.println("Mirror image");

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

k=0;

for(j=M-1;j>=0;j--)

B[i][k]=A[i][j];

System.out.print(B[i][j]+"\t");

k++;

System.out.println();

else

\\ print size out of range

System.out.println("size out of range");

}
OUTPUT:

ORIGINAL MATRIX

4 16 12

8 2 14

413

MIRROR IMAGE MATRIX

12 16 4

14 2 8

3 16

Example 2

OUTPUT : SIZE OUT OF RANGE

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner An object used to read input from the keyboard.

i int A loop counter variable for iterating over rows.

j int A loop counter variable for iterating over columns.

k int A variable used to index the mirror image array.

M int The size of the square matrices.

A int[][] A 2D array representing the original matrix.


B int[][] A 2D array representing the mirror image of the original matrix.

Program 18

Matrix addition

ALGORITHM-

Step 1: start

Step 2: declare 3 two dimensional array a[][], b[][],c[][]

Step 3:input the no. of rows ‘m’ and no of column ‘n’ to be inputed

Step 4: set the first loop for i=1 to i

Step 5: set the first loop for j=1 to j

Step 6: input the values a[i][j] and b[i][j]

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

Step 7: continue 2nd loop till j=2;

Step 8: continue 1st loop till i=2;

Step 9: display c[i][j] via two loops

Step 10: stop


import java.util.*;

class matrixaddition

public static void main()

Scanner sc=new Scanner(System.in);

\\intialize the array data members

int a[][]=new int[3][3];

int b[][]=new int[3][3];

int c[][]=new int[3][3];

int i,j,k;

\\to print the first matrix elements

System.out.println("Enter the elements of first matrix");

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

for(j=0;j<3;j++)

System.out.print("enter any number");

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

}
System.out.println("Enter the elements of second matrix");

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

for(j=0;j<3;j++)

System.out.print("enter any number");

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

\\printing the sum of two values in array matrix

int sum=0;

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

for(j=0;j<3;j++)

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

\\printing the first matrix

System.out.println("Matrix first element");

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

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

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

System.out.println();

\\printing the second matrix

System.out.println("Matrix second element");

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

for(j=0;j<3;j++)

System.out.print(b[i][j]+"\t");

System.out.println();

\\printing the resultant matrix

System.out.println("Matrix resultant element");

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

for(j=0;j<3;j++)

System.out.print(c[i][j]+"\t");

System.out.println();
}

OUTPUT:

VARIABLE TABLE:
Variable Name Data Type Description

sc Scanner An object used to read input from the user.

m int Number of rows in the matrices.

n int Number of columns in the matrices.

a[][] int[][] A 2D array representing the first matrix.

b[][] int[][] A 2D array representing the second matrix.

c[][] int[][] A 2D array representing the resultant (sum) matrix.

i int Loop counter for rows.

j int Loop counter for columns.


Program 19

Matrix multiplication

ALGORITHM-

Step 1: start

Step 2: declare 3 two dimensional array a[][], b[][],c[][]

Step 3:input the no. of rows ‘i’ and no of column ‘j’ to be inputed

Step 4: set the first loop for i=1 to i

Step 5: set the first loop for j=1 to j

Step 6: input the values a[i][j] and b[i][j]

Assign sum =sum+a[i][j]*b[k][j]

Step 7: continue 2nd loop till j=2;

Step 8: continue 1st loop till i=2;

Step 9: display c[i][j] via two loops

Step 10: stop


import java.util.*;

class Matrixmultiplication

public static void main()

Scanner sc=new Scanner(System.in);

\\intialize the array data member

int a[][]=new int[3][3];

int b[][]=new int[3][3];

int c[][]=new int[3][3];

int i,j,k;

\\to print first matrix values

System.out.println("Enter the elements of first matrix");

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

for(j=0;j<=2;j++)

System.out.print("Enter any number ");

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

\\to print second matrix values


System.out.println("Enter the elements of second matrix");

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

for(j=0;j<=2;j++)

System.out.print("Enter any number");

b[i][j]=nextInt();

\\for multiplication forloop

int sum=0;

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

for(j=0;j<=2;j++)

for(k=0;k<=2;k++)

sum=sum+(a[i][k]*b[k][j]);

c[i][j]=sum;

sum=0;

\\printing the first matrix

System.out.println("Matrix first element ");


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

for(j=0;j<=2;j++)

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

System.out.println();

\\printing the second matrix

System.out.println("Matrix second element ");

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

for(j=0;j<=2;j++)

System.out.print(b[i][j]+"\t");

System.out.println();

\\printing the resultant mtrix

System.out.println("Resultant Matrix");

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

for(j=0;j<=2;j++)

System.out.print(c[i][j]+"\t");
}

System.out.println();

OUTPUT:

VARIABLE TABLE:

Variable | Data Type | Description

a 2D int array Stores the elements of the first matrix

b 2D int array Stores the elements of the second matrix

c 2D int array Stores the result of the matrix multiplication

i int Loop counter for rows

j int Loop counter for columns

k int Loop counter for matrix multiplication

m int Number of rows in the first matrix

n int Number of columns in the first matrix

p int Number of columns in the second matrix

sum int Used to calculate the sum of products during matrix multiplication

sc Scanner object Used to read input from the keyboard


Program 20

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 both M and N must be greater than 2 and

less than 20. Allow the user to input integers into this matrix.

(a). Display the input matrix

(b). Find the maximum and minimum value in the matrix and display them

along with their position

(c) . Sort the elements of the matrix in ascending order using any standard sorting

technique and rearrange them in the matrix.

(d). output the rearranged matrix.

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

Example 1

Input : M=3

N=4

8 7 9 3

-2 0 4 5

1 3 6 -4

Output: Original Matrix

8 7 9 3

-2 0 4 5

1 3 6 -4
Largest number 9

Row=0

Column=2

Smallest number:-4

Row =2

Column=3

Rearranged matrix

-4 -2 0 1

3 3 4 5

6 7 8 9

Example 2

Input: M=3

N=22

Output: Size out of Range

ALGORITHM-

Step 1: start

Step 2: declare 3 two dimensional array x[][], y[][],a[]

Step 3:input the no. of rows ‘M’ and no of column ‘M’ to be inputed

Step 4: set the first loop for i=1 and set the first loop for j=1

Step 5: display original matrix

Step 6: input the values x[i][j] and y[i][j]

Step 7: continue 2nd loop till j=M;

Step 8: continue 1st loop till i=M;

Step 9: display y[i][j] via two loops


Step 10: stop

import java.util.*;

class pro

public static void main()

Scanner sc=new Scanner(System.in);

\\intialize the data member

int i,j,M,n,sum=0,k=0,compare,flag=0,temp,row=0,

col=0,row1=0,col1=0,pos,small,big;

\\print the value of M

System.out.print("enter a value of M");

M=sc.nextInt();

int x[][]=new int[M][M];

int y[][]=new int[M][M];

int size=M*M;

int a[]=new int[size];

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

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

System.out.print("enter any number");

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

a[k++]=x[i][j];

\\for sorting technique

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

for(j=0;j<size-i-1;j++)

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

\\printing original matrix

System.out.println("Orignal matrix");

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

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

System.out.print(x[i][j]+"\t");

System.out.println();

\\arrange the values according to the size

big=x[0][0];

small=x[0][0];

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

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

if(x[i][j]>big)

big=x[i][j];

row=i;

col=j;

if(x[i][j]<small)

small=x[i][j];

row1=i;

col1=j;

}
}

System.out.println("Largest number="+big);

System.out.println("Row ="+row);

System.out.println("column"+col);

System.out.println("smallest"+small);

System.out.println("Row ="+row1);

System.out.println("column"+col1);

System.out.println("Matrix after sorting");

k=0;

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

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

y[i][j]=a[k++];

System.out.print(y[i][j]+"\t");

System.out.println();

OUTPUT:

Output: Original Matrix

8 7 9 3
-2 0 4 5

1 3 6 -4

Largest number 9

Row=0

Column=2

Smallest number:-4

Row =2

Column=3

Rearranged matrix

-4 -2 0 1

3 3 4 5

6 7 8 9

Example 2

Output: Size out of Range

VARIABLE TABLE

Variable | Data Type | Description

sc | Scanner | An object to read input from the keyboard

i| int | Loop counter variable

j| int | Loop counter variable

k| int | Index for the a array

big | int | Not used in the code

small | int | Not used in the code


pos | int | Not used in the code

temp | int | Temporary variable for swapping elements

row | int | Not used in the code

col| int | Not used in the code

row1 int | Not used in the code

col1 | int | Not used in the code

M| int | Size of the matrix (number of rows and columns)

X| int[][] | 2D array to store the matrix

Y| int[][] | Not used in the code

size | int | Total number of elements

You might also like