Computer Science
Computer Science
PROJECT 2024-25
XII-Sci/Com
Read the following instructions before you start with the project:
- 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
- Index should be the second page which will consist of Sr. No. , Program
Question
Algorithm
Output
05
01
2010
03
04
2010
34
06
2010
ALGORITHM-
Step 1 :start
{
if(year is divisible by 400)
Step 6 else
Step 7: stop
class data
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)
if(dd<=30&&dd>=1)
System.out.println("VALID DATE");
else
System.out.println("INVALID DATE");
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)
System.out.println("VALID DATE");
else
System.out.println("INVALID DATE");
}
}
OUTPUT:
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
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
Test your program with the sample data and some random data:
Example 1
Input : m=100
n=1000
101,131,151,181,191,313,353,373,383,727,757,787,797,919,929
Example 2
Input : m=100
n=5000
ALGORITHM-
Step 1: start
Step 2: read n
Step 3: flag0
Step 4: for(i2;in;ii+1)
if(n is divisible by i)
flag1
Step 5: if(flag is 0)
display ‘prime’
else
display ‘not’
Step 6: for(i1;number>0;i++)
remnumber%10
revrev*10+rem
numbernumber/10
}
else
Step 8:stop
class Palinlimit
int is_prime(int n)
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)
int rev=0,rem,temp=n;
while(n>0)
rem=n%10;
rev=rev*10+rem;
n=n/10;
if(rev==temp)
return 1;
else
{
return 0;
int i,cnt=0;
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++;
else
{
System.out.println("Out of range");
OUTPUT:
101,131,151,181,191,313,353,373,383,727,757,787,797,919,929
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
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
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:
1,9,45,55,99,297,703,999
Step 1:start
Step 3 :while(n>0)
cnt++;
Step 4: if(first+second==temp)
return 1;
else
return 0;
Step 5:stop
class kaprekar
int is_kar(int n)
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;
}
}
int i,count=0;
for(i=p;i<=q;i++)
int R=ob1.is_kar(i);
if(R==1)
System.out.print(i+",");
count++;
}
OUTPUT:
OUTPUT:
1,9,45,55,99,297,703,999
VARIABLE TABLE:
Variable Name Data Type Description
num int The number being checked for the Kaprekar property.
divisor long A divisor used to split the square into two parts.
number
Composite number: A composite number is a number that has more than two factors.
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:
Output:
10,28,46,55,64,82,91,100
Example 2:
Output:
Invalid input
ALGORITHM-
Step 1:start
Step 3: for(i=2;i<num;i++)
flag1;
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 i,flag=0;
for(i=2;i<num;i++)
if(num%i==0)
flag=1;
break;
if(flag==1)
return 1;
}
else
return 0;
int rem,sum=0;
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;
int k,cnt=0;
if(m<n)
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++;
}
else
System.out.println("Invalid input");
}
OUTPUT:
Output:
10,28,46,55,64,82,91,100
Output:
Invalid input
VARIABLE TABLE:
Variable Name Data Type Description
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
Explanation:
Every digit of the given number has been converted into its corresponding word.
Input : N = 597
ALGORITHM-
Step 1:start
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 6:stop
import java.util.*;
class word
System.out.println("Enter a number");
int n=sc.nextInt();
int rem,rev=0;
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)
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");
if(rem==9)
System.out.print("NINE");
rev=rev/10;
}
OUTPUT:
VARIABLE TABLE:
Variable Name Data Type Description
sc Scanner An object of the Scanner class used to read input from the keyboard
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.
(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
Test your program with the sample data and some random data:
Example 1
Example 2
INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE
TODAY.
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
Step 1: start
len=str.length()
Step 6: for(i0;i<len;i++)
Step 7:
if( ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||
ch2=='I'||ch2=='E'||ch2=='A'||ch2=='O'||ch2=='U')
else
{
display"INVALID INPUT"
Step 8: stop
class pro6
String str2="";
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
char ch1=str2.charAt(0);
char ch2=str2.charAt(len1-1);
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;
System.out.println(strnew3);
else
{
System.out.println("INVALID INPUT");
OUTPUT:
Example 1
Example 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
VARIABLE TABLE:
Variable Name Data Type Description
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
Program 7
Write the program to find the word in sentence. If the word is present in sentence
Input:
Enter a string: The quick brown fox jumps over the lazy dog
ALGORITHM-
Step 1: start
len=str.length()
Step 6: for(i0;i<len;i++)
str2str1.charAt(i)+str2;
display ‘palindrome’
else
display ‘not’
Step 8: stop
import java.util.*;
class proo7
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string");
String str1=sc.nextLine();
String str3=sc.nextLine();
String str2="";
str1=str1+"";
int len,i,cnt=0,flag=0;
len=str1.length();
for(i=0;i<len;i++)
if(str1.charAt(i)!=' ')
str2=str2+str1.charAt(i);
else{
if(str2.equalsIgnoreCase(str3))
{
cnt++;
flag=1;
str2="";
if(flag==1)
System.out.println("Search succesfull");
else{
System.out.println("Search unsuccesfull");
OUTPUT:
VARIABLE TABLE:
Variable Name Data Type Description
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
(ii) Find the number of vowels and consonants in each word and display them with
Example 1
OUTPUT:
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
OUTPUT:
God 1 2
Is 1 1
Great 2 3
OUTPUT:
OUTPUT:
Invalid Input.
ALGORITHM-
Step 1: start
len=str.length()
Step 6: for(i0;i<len;i++)
{
str2str1.charAt(i)+str2;
Step 8: stop
class pro8
String str2="";
int len,cntvow=0,cntcon=0,len1,j,i;
len=str1.length();
str1=str1.toLowerCase();
String strnew="";
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);
ch1=Character.toUpperCase(ch1);
strnew=strnew+ch1;
String sub=str2.substring(1,len1);
strnew=strnew+sub+"";
str2="";
System.out.println(strnew);
for(i=0;i<len;i++)
{
char x=strnew.charAt(i);
if(x!=' '&&x!='.'&&x!='?')
str2=str2+str1.charAt(i);
if(Character.isLetter(x))
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
System.out.println(str2+"\t"+cntvow+"\t"+cntcon);
cntvow=0;
cntcon=0;
str2="";
}
}
else
System.out.println("invalid input");
OUTPUT:
Example 1
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
God 1 2
Is 1 1
Great 2 3
Example 3
Invalid Input.
VARIABLE TABLE:
Variable Data Type Description
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
Test your program with the sample data and some random data:
Example 1
Example 2
Example 3
ALGORITHM-
Step 1: start
Step 6: for(i0;i<len;i++)
str2str1.charAt(i)+str2;
display ‘palindrome’
else
display ‘not’
Step 8: stop
class pro9
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="";
if(cnt>1)
{
System.out.println("\n NUMBER OF PALINDROME WORDS:"+cnt);
else
OUTPUT:
Example 1
Example 2
Example 3
VARIABLE TABLE:
Variable Name Data Type Description
Program 10
Write a program in java to accept a string (Containing three words) and display the
Input: IT IS COOL
Output: COOL IS IT
ALGORITHM-
Step 1: start
len=str.length()
Step 6: for(i0;i<len;i++)
str2str1.charAt(i)+str2;
display ‘VALID’
}
else
display ‘INVALID’
Step 8: stop
class pro10
String strnew="";
str1=""+str1;
String str2="";
int len,i;
len=str1.length();
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:
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
Example 2
Example 3
INPUT : You
ALGORITHM-
Step 1: start
Step 2: read n
Step 3: first0
Step 4: second0
Step 6: for(i2;i<w;ii+1)
third=first+second
display third
firstsecond
secondthird
Step 7: stop
import java.util.*;
class asci
{
public static void main()
System.out.println("Enter a string");
String str=sc.nextLine();
String strnew="";
char ch;
int as,i,len;
len=str.length();
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
System.out.println("Invalid length");
OUTPUT:
Example 1
Example 2
OUTPUT : The cipher text is:
Example 3
VARIABLE TABLE:
cipher is applied
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”.
Output : ONDONLAY
Output : OLYMPICSAY
ALGORITHM-
Step 1: start
len=str.length()
Step 6: ch=str.charAt(i)
Step 7: for(i0;i<len;i++)
Step 8:
strnew=str2+str3+"AY";
import java.util.*;
class piglatin
System.out.print("Enter a string");
String str1=sc.nextLine();
str1=str1.toUpperCase();
String str2,str3,strnew;
int len,i,start=0;
char ch;
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;
}
str2=str1.substring(start);
str3=str1.substring(0,start);
strnew=str2+str3+"AY";
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
str2 String Stores the substring of str1 starting from the first vowel
str3 String Stores the substring of str1 before the first vowel
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
Test your program with the sample data and some random data:
Example 1:
OUTPUT: Length: 6
Rearranged Sentence:
Example 2:
OUTPUT: Length: 4
ALGORITHM-
Step 1: start
Step 2: str as String
len=str.length()
for(i0;i<len;i++)
Step 7: display"Length:"+cnt
display’Rearranged sentence’
Step 8:stop
class pro13
String str2="";
int len,i,j,cnt=0,k=0;
len=str1.length();
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;
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;
System.out.println("Length:"+cnt);
System.out.println("Rearranged sentence:");
for(i=0;i<cnt;i++)
System.out.println(a[i]+"");
else
System.out.println("Invalid input");
}
ob1.sort(str1);
OUTPUT:
Example 1:
OUTPUT: Length: 6
Rearranged Sentence:
Example 2:
OUTPUT: Length: 4
VARIABLE TABLE:
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.
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
len=str.length()
Step 6: for(i0;i<len;i++)
str2str1.charAt(i)+str2;
Step 8:stop
class paragraph
str1=str1+"";
String str2="";
char ch;
int len,i,j,k=-1,cnt=0;
len=str1.length();
for(i=0;i<len;i++)
ch=str1.charAt(i);
if(ch==' ')
++cnt;
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);
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
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()
String par="HELLO HOW ARE YOU ?WHEN ARE YOU COMING ?HOPE TO
SEE YOU SOON";
System.out.println(par)
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="";
}
}
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
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
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 3:input the no. of rows ‘n’ and no of column ‘n’ to be inputed
import java.util.*;
class strong
{
Scanner sc=new Scanner(System.in);
int i,j,n,sum=0,k=0,compare,flag=0;
n=sc.nextInt();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(i=0;i<n;i++)
sum=0;
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
b[k++]=sum;
for(i=0;i<n;i++)
sum=0;
for(j=0;j<n;j++)
sum=sum+a[j][i];
b[k++]=sum;
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=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;
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;
if(flag==0)
System.out.println("strong matrix");
else
}
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
flag int A flag to indicate whether the matrix is strong (0) or not (1)
Program 16
Symmetric matrix
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:
(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
(c) Find the sum of the elements of left diagonal and the sum of the elements of
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
Example 2
INPUT : M = 22
ALGORITHM-
Step 1: start
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
import java.util.*;
class symmetric
int i,j,M,n,sum=0,k=0,compare,flag=0;
M=sc.nextInt();
if(M>2&&M<10)
for(i=0;i<M;i++)
for(j=0;j<M;j++)
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)
else
for(i=0;i<M;i++)
for(j=0;j<M;i++)
if(i==j)
sum=sum+x[i][j];
for(i=0;i<M;i++)
for(j=0;j<M;j++)
if(i+j==(M-1))
sum=sum+x[i][j];
else
OUTPUT:
OUTPUT :
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
Example 2
VARIABLE TABLE:
Variable Name Data Type Description
M int The order of the square matrix (number of rows and columns)
Program 17
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
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
12 16 4
14 2 8
3 16
Example 2
INPUT : M = 22
Step 1: start
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 7: continue 2nd loop till j=n and continue 1st loop till i=m;
Step 8: for(j=M-1;j>=0;j--)
import java.util.*;
class mirror
int i,j,M,n,sum=0,k=0,compare,flag=0;
System.out.print("enter a value of M");
M=sc.nextInt();
if(M>2&&M<20)
for(i=0;i<M;i++)
for(j=0;j<M;j++)
A[i][j]=sc.nextInt();
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();
}
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
}
OUTPUT:
ORIGINAL MATRIX
4 16 12
8 2 14
413
12 16 4
14 2 8
3 16
Example 2
VARIABLE TABLE:
Variable Name Data Type Description
Program 18
Matrix addition
ALGORITHM-
Step 1: start
Step 3:input the no. of rows ‘m’ and no of column ‘n’ to be inputed
class matrixaddition
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
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++)
b[i][j]=sc.nextInt();
int sum=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
System.out.print(b[i][j]+"\t");
System.out.println();
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
Matrix multiplication
ALGORITHM-
Step 1: start
Step 3:input the no. of rows ‘i’ and no of column ‘j’ to be inputed
class Matrixmultiplication
int i,j,k;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
a[i][j]=sc.nextInt();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
b[i][j]=nextInt();
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;
for(j=0;j<=2;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
System.out.print(b[i][j]+"\t");
System.out.println();
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:
sum int Used to calculate the sum of products during matrix multiplication
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.
(b). Find the maximum and minimum value in the matrix and display them
(c) . Sort the elements of the matrix in ascending order using any standard sorting
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
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
ALGORITHM-
Step 1: start
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
import java.util.*;
class pro
int i,j,M,n,sum=0,k=0,compare,flag=0,temp,row=0,
col=0,row1=0,col1=0,pos,small,big;
M=sc.nextInt();
int size=M*M;
for(i=0;i<M;i++)
for(j=0;j<M;j++)
{
x[i][j]=sc.nextInt();
a[k++]=x[i][j];
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;
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();
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);
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:
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
VARIABLE TABLE