0% found this document useful (0 votes)
26 views111 pages

Computer Isc 11

Uploaded by

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

Computer Isc 11

Uploaded by

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

COMPUTER SCIENCE

ISC 2024 - 25
NAME : DHRUV MANSHANI
CLASS AND SECTION : XI - CA
ID : 4691
SUBMITTED TO : MRS. NEHA
THOMBRE
TABLE OF CONTENTS

S.NO. Date Of Practical Date Of Teacher’s


Submission Signature and
Remarks
Assignment 1 04–05-2024 18-06-2024

Assignment 2 04–05-2024 18-06-2024

Assignment 3 04–05-2024 18-06-2024

Assignment 4 04–05-2024 18-06-2024

Assignment 5 04–05-2024 18-06-2024

Assignment 6 31-08-2024 02-09-2024

Assignment 7 31-08-2024 02-09-2024

Assignment 8 31-08-2024 02-09-2024

Assignment 9 31-08-2024 02-09-2024


Assignment 10 31-08-2024 02-09-2024

Assignment 11 31-08-2024 02-09-2024

Assignment 12 31-08-2024 02-09-2024

Assignment 13 31-08-2024 02-09-2024

Assignment 14 31-08-2024 02-09-2024

Assignment 15 31-08-2024 02-09-2024

Assignment 16 30-11-2024 01-12-2024

Assignment 17 30-11-2024 01-12-2024

Assignment 18 30-11-2024 01-12-2024

Assignment 19 30-11-2024 01-12-2024

Assignment 20 30-11-2024 01-12-2024

Assignment 21 30-11-2024 01-12-2024

Assignment 22 30-11-2024 01-12-2024


Assignment 23 30-11-2024 01-12-2024

Assignment 24 30-11-2024 01-12-2024

Assignment 25 30-11-2024 01-12-2024


ASSIGNMENT 1
Problem Statement :

An Evil number is a positive whole number which has even number of 1's in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1's. A few evil numbers are 3, 5,
6, 9…. Design a program to accept a positive whole number and find the binary equivalent of the number
and count the number of 1's in it and display whether it is a Evil number or not with an appropriate
message. Output the result in format given below:
Example 1
Input: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number
Example 2
Input: 26
Binary Equivalent: 11010
No. of 1's: 3
Output: Not an Evil Number

ALGORITHM :
Step 1 : Declaring Scanner class
Step 2 : Take Input and store it in the variable n
Step 3 : Converting n into it’s binary equivalent
Step 4 : Counting the number of 1s and deciding if it’s evil or not
Step 5 : Displaying output
Step 6 : Stop

CODING :
import java.util.*;

class As1{
void main(){
//Declaring Scanner Class
Scanner sc = new Scanner(System.in);

//taking input
System.out.println("Enter no.");
int n = sc.nextInt();

int s = 0;
String t ="";
int count = 0;

//checking if n if evil or not


for(int i =n;i>0;i/=2){
s = i%2;
if(s==0){
t = "0"+t;
}else{
t= "1"+t;
count++;
}
}

//Displaying output
System.out.println("Binary Equivalent = "+t);
System.out.println("No. of 1s = "+count);
if(count%2==0)
System.out.println("Evil Number");
else
System.out.println("Not an Evil Number");
}

}
OUTPUT :

VDT :

Variable/Array Name Data Type Purpose Scope

n int To store input Global to main()

s int To store temporary Global to main()


value of the remainder

t String To store the binary Global to main()


equivalent

count int To count the number Global to main()


of ‘1s’

i int Loop variable Local to for()


ASSIGNMENT 2
Problem Statement :
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 which has more than two factors.
For example:
Factors of 10 are: 1, 2, 5, 10
Magic number: A Magic number is a number in which the eventual sum of the digit is equal to 1.
For example: 28 = 2+8=10= 1+0=1
Accept two positive integers 'm' and 'n', where m is less than n. Display the number of composite magic
integers that are in the range between m and n (both inclusive) and output them along with frequency, in
the format specified below:
Sample Input:
m=10 n=100
Output: The composite magic numbers are 10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8
Sample Input:
m=120 n=90
Output: Invalid input

ALGORITHM :
Step 1 : Taking input of m and n
Step 2 : checking for composite number in the given range
Step 3 : Checking for magic number in the given range
Step 4 : Displaying all the composite magic numbers
Step 5 : Displaying the frequency of the composite magic numbers
Step 6 : Stop

CODING :
import java.util.*;

class As2{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);
//taking input of m and n
System.out.println("Enter m");
int m = sc.nextInt();

System.out.println("Enter n");
int n = sc.nextInt();

int compmagic_count = 0;

//if n is less than m


if(m<n){

System.out.print("The Composite Magic Numbers are ");


//checking for composite magic number
for(int i = m;i<=n;i++){
int count = 0;
int tsum = 0;
for(int j = 1;j<=i;j++){
if(i%j==0)
count++;
}
if(count<=2)
continue;

for(int j = i;j>0;j/=10){
tsum += j%10;
}

while(tsum>9){
int tsum1 = tsum;
tsum = 0;
for(int f = tsum1;f>0;f/=10){
tsum += f%10;

}
//displaying composite magic numbers
if(tsum==1){
System.out.print(i+" ");
compmagic_count++;
}
}
//displaying frequency of composite magic numbers
System.out.println();
System.out.println("Frequency of Composite Magic Numbers is "+compmagic_count);
}else
System.out.println("Invalid Input");
}
}

OUTPUT :
VDT :

Variable/Array Name Data Type Purpose Scope

m int To store the starting Global to main()


range

n int To store the ending Global to main()


range

compmagic_count int To store the number Global to main()


of composite magic
numbers

i int Loop variable Local to for()

count int To count the number Local to for()


of factors

tsum int To store the total sum Local to for()


of digits

j int Loop variable Local to for()

j int Loop variable Local to for()

tsum1 int To temporarily store Local to while()


value of ‘tsum’

f int Loop variable Local to for()


ASSIGNMENT 3
Problem Statement :

ALGORITHM :
Step 1 : Create an array will all alphabets.
Step 2 : take input of the sentence
Step 3 : Check each alphabet with all the letters of the array
Step 4 : fill the matched letters with spaces in the array
Step 5 : If array is filled with spaces, it is panagram
Step 6 : Create a loop to check for the longest and shortest word
Step 7 ; Display Output
Step 8 : Stop

CODING :
import java.util.*;

class Panagram{
void main(){
Scanner sc = new Scanner(System.in);
//taking input
System.out.println("Enter Sentence");
String s = sc.nextLine();

if((s.charAt(s.length()-1)=='.')||(s.charAt(s.length()-1)=='?')||(s.charAt(s.length()-1)=='!')){
s = s.toLowerCase();
char a[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
StringTokenizer str = new StringTokenizer(s);
int count = str.countTokens();
String longest = "";
String shortest = "";
//checking if string is panagram
while(str.hasMoreTokens()){
String d = str.nextToken();
for(int i = 0;i<d.length();i++){
char ch = d.charAt(i);
for(int j = 0;j<26;j++){
if(a[j]==ch){
a[j] = ' ';
break;
}
}

}
//checking for longest and shortest word
if(d.length()>longest.length())
longest = d;
if(shortest == ""){
shortest = d;
continue;
}
if(d.length()<shortest.length())
shortest = d;
}
int p = 0;
for(int i = 0;i<26;i++){
if(a[i]!=' ')
p++;
}
//displaying output
if(p==0)
System.out.println("String is Panagram");
else
System.out.println("String is not Panagram");
System.out.println("Longest Word : "+longest);
System.out.println("Shortest Word : "+shortest);

}else
System.out.println("INVALID INPUT");//incase input is invalid
}}

OUTPUT :
VDT :

Variable/Array Data Type Purpose Scope


Name

s String To store the input string Global to main()

a[] char To store the alphabets Global to main()

count int To count the no. of words Global to main()

longest String To store the longest word Global to main()

shortest String To store the shortest word Global to main()

d String To store every word on a temporary Local to while


basis

i int Loop variable Local to while

ch char To store every letter on a temporary Local to while


basis

j int Loop variable Local to while

p int To check if word is panagram Global to main()

i int Loop variable Local to for()


ASSIGNMENT 4
Problem Statement :
Palindrome Number in Java: Write a program to accept a number from the user and check whether it is a
Palindrome number or not. A number is a Palindrome which when reads in reverse order is same as in the
right order.
Sample Input: 242
Sample Output: A Palindrome number
Sample Input: 467
Sample Output: Not a Palindrome number
ALGORITHM :
Step 1 : take input of number
Step 2 : count number of digits
Step 3 : reverse the number
Step 4 : check if number is palindrome
Step 5 : Display Output
Step 6 : Stop

CODING :
import java.util.*;
class As4{
void main(){
Scanner sc = new Scanner(System.in);//declaring scanner class
//taking input
System.out.println("Enter a number");
int n = sc.nextInt();

int rn = 0;
int count = 0;
int t = n;
//counting the no. of digits
for(int j = n;j>0;j/=10){
count++;
}
//reversing the number
for(int i = count-1;i>=0;i--){
rn += (Math.pow(10,i)*(n%10));
n/=10;
}

//displaying output
if(rn==t)
System.out.println("Palindrone Number");
else
System.out.println("Not A Palindrone Number");
}}

OUTPUT :

VDT :
Variable/Array Name Data Type Purpose Scope

n int To store the input Global to main()

rn int To store the reverse Global to main()


number

count int To store the number Global to main()


of digits

t int To store the value of Global to main()


input

j int Loop variable Local to for()

i int Loop variable Local to for()

ASSIGNMENT 5
Problem Statement :
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an Adam
number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 … etc.
Adam number: The square of a number and the square of its reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam
integers that are in the range between m and n (both inclusive) and output them along with the frequency,
in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT

ALGORITHM :
Step 1 : Take input of m and n
Step 2 : Check if m<n
Step 3 : Checking every number between the range if it prime
Step 4 : Checking every number between the range if it is adam
Step 5 : Displaying output
Step 6 : Stop

CODING :
import java.util.*;

class As5{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);

int adam_count = 0;

//taking input of m and n


System.out.println("Enter m");
int m = sc.nextInt();
System.out.println("Enter n");
int n = sc.nextInt();
//to check invalid input
if(n<m){
System.out.println("Invalid Input");
System.exit(0);
}

//checking for adam prime numbers


System.out.print("Prime Adam Numbers Between "+m+" and "+n+" are ");
for(int k = m;k<=n;k++){

int count = 0, rn =0, count2 = 0, count3 = 0;


for(int i = 1;i<=k;i++){
if(k%i==0)
count++;
}
if(count!=2){
continue;
}

for(int j = k;j>0;j/=10){
count2++;
}
int t = k;
for(int i = count2-1;i>=0;i--){
rn += (Math.pow(10,i)*(t%10));
t/=10;
}
int square1 = k*k;
int squaret = square1;
int sqaurern = 0;

for(int j = square1;j>0;j/=10){
count3++;
}
for(int i = count3-1;i>=0;i--){
sqaurern += (Math.pow(10,i)*(squaret%10));
squaret/=10;
}

if(sqaurern==(rn*rn)){
System.out.print(k+" ");
adam_count++;
}

}
if(adam_count==0)
System.out.print("NIL");
System.out.println();

//displaying the frequency of adam prime numbers


System.out.println("Frequency Of Prime Adam Numbers is "+adam_count);
}
}
OUTPUT :

VDT :
Variable/Array Name Data Type Purpose Scope

m int To store the starting Global to main()


range

n int To store the ending Global to main()


range

adam_count int To store the number Global to main()


of adam prime
numbers

k int Loop variable Local to for()

count int To count the number Local to for()


of factors

rn int To store the reverse Local to for()


number

count2 int To count the number Local to for()


of digits

count3 int To count the number Local to for()


of digits

i int Loop variable Local to for()

j int Loop variable Local to for()

t int To temporarily store Local to for()


the value of ‘k’

i int Loop variable Local to for()

square1 int To store the square of Local to for()


n

squaret int To temporarily store Local to for()


the value of ‘square1’

squarern int To store the reverse of Local to for()


the square of ‘k’

j int Loop variable Local to for()

i int Loop variable Local to for()


ASSIGNMENT 6
Problem Statement :

ALGORITHM :
Step 1 : to take input and convert into uppercase
Step 2 : to find the frequency of vowels and consonants and display with an appropriate message
Step 3 : To rearrange the word as per given condition
Step 4 : to display to the original word and the rearranged word
Step 5 : to define a main function, create an object and call other methods
Step 6 : Stop
CODING :
import java.util.*;

class Rearrange{
String wrd, newwrd;
//default constructor
Rearrange(){
wrd = "";
newwrd = "";

void readword(){
//taking input
Scanner sc = new Scanner(System.in);
System.out.println("Enter word");
wrd = sc.next();

wrd = wrd.toUpperCase();//converting to uppercase


}

void freq_vow_con(){
int v = 0, c = 0;
//counting no. of vowels and consonants
for(int i = 0;i<wrd.length();i++){
char ch = wrd.charAt(i);
if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')){
v++;
}else if((ch>=65)&&(ch<=97)){
c++;
}

}
System.out.println("Number of consonants = "+c);
System.out.println("Number of vowels = "+v);

void arrange(){
for(int i = 0;i<wrd.length();i++){
char ch = wrd.charAt(i);
if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')){
newwrd +=ch;
}
}

for(int i = 0;i<wrd.length();i++){
char ch = wrd.charAt(i);
if((ch!='A')&&(ch!='E')&&(ch!='I')&&(ch!='O')&&(ch!='U')){
newwrd +=ch;
}
}
}
//displaying output
void display(){
System.out.println("Original Word = "+wrd);
System.out.println("Rearranged Word = "+newwrd);

void main(){
Rearrange a = new Rearrange();
a.readword();
a.freq_vow_con();
a.arrange();
a.display();
}
}

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

wrd String To store the input string Global to class

newwrd String To store the rearranged string Global to class

v int To count the number of vowels Global to


feq_vow_con()

c int To count the number of consonants Global to


feq_vow_con()

i int Loop variable Local to for()

ch char To store the character of the wrd Local to for()


temporarily

i int Loop variable Local to for()

ch char To store the character of the wrd Local to for()


temporarily

i int Loop variable Local to for()

ch char To store the character of the wrd Local to for()


temporarily
ASSIGNMENT 7
Problem Statement :

ALGORITHM :
Step 1 : Create a default constructor
Step 2 : to accept word
Step 3 : convert uppercase alphabets to lowercase and vice - versa
Step 4 : display the original and the toggled word
Step 5 : to define a main function, create an object and call other methods
Step 6 : Stop
CODING :
import java.util.*;

class Toggle{
String str;
String newstr;
int len;
//deault constructor
Toggle(){
str = "";
newstr = "";
len = 0;
}
//taking input
void readword(){
Scanner sc = new Scanner(System.in);

System.out.println("Enter string");
str = sc.nextLine();
}
//toggling word
void toggle(){

for(int i = 0;i<str.length();i++){
char ch = str.charAt(i);
if(Character.isUpperCase(ch))
newstr += Character.toLowerCase(ch);
else
newstr +=Character.toUpperCase(ch);
}
}
//displaying output
void display(){
System.out.println("Original Word = "+str);
System.out.println("Toggled Word = "+newstr);
}
//main method
void main(){
Toggle a = new Toggle();
a.readword();
a.toggle();
a.display();
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

str String To store the input string Global to class

newstr String To store the toggled string Global to class

len int To store the length of str Global to class

i int Loop variable Local to for()

ch char To store each character of str temporarily Local to for()


ASSIGNMENT 8
Problem Statement :

ALGORITHM :
Step 1 : Create a default constructor
Step 2 : to accept original string and mask string and convert them to lowercase
Step 3 : to form a new string as per the given conditions
Step 4 : display the original and the newly formed string
Step 5 : to define a main function, create an object and call other methods
Step 6 : Stop

CODING :
import java.util.*;

class StringOp{
String str, msk, nstr;
//deault construtor
StringOp(){
str = "";
msk = "";
nstr = "";
}
//input
void accept(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter String");
str = sc.nextLine();
System.out.println("Enter masked String");
msk = sc.nextLine();
str = str.toLowerCase();
msk = msk.toLowerCase();

//rearranging into a new string


void form(){

for(int i = 0;i<str.length();i++){
char ch = str.charAt(i);
int temp = 0;
for(int j = 0;j<msk.length();j++){
char d = msk.charAt(j);
if(ch==d){
temp++;
break;
}

if(temp==0){
nstr +=ch;
}
}
}
//displaying output
void display(){
System.out.println("Original String"+str);
System.out.println("Output"+nstr);
}
//creating main method
void main(){

StringOp a = new StringOp();


a.accept();
a.form();
a.display();

}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

str String To store the input string Global to class

msk String To store the mask string Global to class

nstr String To store the output Global to class

i int Loop variable Local to for()

ch char To store character temporarily Local to for()

temp int To store the position of each character of Local to for()


str

j int Loop variable Local to for()

d char To store the position of each character of Local to for()


msk
ASSIGNMENT 9
Problem Statement :

ALGORITHM :
Step 1 : to take input
Step 2 : to convert the given input into uppercase
Step 3 : to check for vowels in pairs
Step 4 : to count the no. of such pairs
Step 5 : To display the pairs and their number
Step 6 : Stop

CODING :
import java.util.*;

class As9{
void main(){
Scanner sc = new Scanner(System.in);//declairng scanner class

//taking input
System.out.println("Enter String");
String s = sc.nextLine();
s = s.toUpperCase();//converting to uppercase
int sum = 0;
System.out.print("Pair of vowels: ");
//finding out pairs of vowels
for(int i = 0;i<s.length()-1;i++){
char ch = s.charAt(i);
char h = s.charAt(i+1);

if(((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))&&((h=='A')||(h=='E')||(h=='I')||(h=='O')||(h=='U')))
{
System.out.print(ch+""+h+"\t");
sum++;
}

}
System.out.println();
System.out.println("No. of Pairs = "+sum);//displaying no. of pairs
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

s String To store the string input Global to class

i int Loop variable Local to for()

sum int To store the number of pair of vowels Global to class

ch char To store the character of s temporarily Local to for()

h char To store the character of s temporarily Local to for()


ASSIGNMENT 10
Problem Statement :

Write a program to declare a square matrix A[][] of order (M × M) where 'M' must be greater than 3 and
less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:

1. Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged
matrix with their sum.

Test your program for the following data and some random data

ALGORITHM :
Step 1 : taking input of size and then elements of array
Step 2 : displaying original matrix
Step 3 : Sorting the non boundary elements into a different array and then transferring them back
Step 4 : Displaying the rearranged matrix
Step 5 : Calculating sum of both the diagonals and then displaying it
Step 6 : Stop

CODING :
import java.util.*;

class As10{
void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array");
int m = sc.nextInt();
if((m>3)&&(m<10)){
int a[][] = new int[m][m];
int temp[] = new int[(m-2)*(m-2)];
int temp1 = 0;
for(int i = 0;i<m;i++){//input
for(int j = 0;j<m;j++){
a[i][j] = sc.nextInt();
if((i!=0)&&(j!=m-1)&&(i!=m-1)&&(j!=0)){
temp[temp1] = a[i][j];
temp1++;
}
}
}
System.out.println("Original Matrix");
for(int i = 0;i<m;i++){//original matrix display
for(int j = 0;j<m;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int tt = 0;
for(int i = 0;i<((m-2)*(m-2));i++){//sorting
for(int j= 0;j<((m-2)*(m-2))-i-1;j++){
if(temp[j]>temp[j+1]){
tt = temp[j];
temp[j] = temp[j+1];
temp[j+1] = tt;
}
}
}
temp1 = 0;
for(int i = 0;i<m;i++){//transfering sorted elements
for(int j = 0;j<m;j++){
if((i!=0)&&(j!=m-1)&&(i!=m-1)&&(j!=0)){
a[i][j] = temp[temp1];
temp1++;
}
}
}
System.out.println("Rearranged Matrix");
for(int i = 0;i<m;i++){//rearranged matrix display
for(int j = 0;j<m;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int sum = 0;
for(int i = 0;i<m;i++){
for(int j = 0;j<m;j++){
if(i==j)
sum+=a[i][j];
else if(i+j==m-1)
sum+=a[i][j];
}
}
System.out.println("Diagonal Matrix");
for(int i = 0;i<m;i++){//diagonal matrix display
for(int j = 0;j<m;j++){
if((i==j)||(i+j==m-1)){
System.out.print(a[i][j]+" ");
}else
System.out.print(" ");
}
System.out.println();
}
System.out.println("Sum of diagonal = "+sum);
}else
System.out.println("Out of Range");

}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

m int To store the size of the array Global to main()

a[][] int To store input Global to main()

temp[] int To store sorted array temporarily Global to main()

temp1 int To store the position of non-boundary Global to main()


elements

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

tt int To be used for exchanging values Global to main()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()


ASSIGNMENT 11
Problem Statement :

Write a program to declare a matrix a[][] of order (m × n) where 'm' is the number of rows and 'n' is the
number of columns such that the values of both 'm' and 'n' must be greater than 2 and less than 10. Allow
the user to input integers into this matrix. Perform the following tasks on the matrix:

1. Display the original matrix.


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

ALGORITHM :

Step 1 : to take input of the size of the array

Step 2 : to take input of the elements of the array

Step 3 : To display original matrix

Step 4 : To sort each row in ascending order

Step 5 : To display the rearranged matrix

Step 6 : Stop

CODING :

import java.util.*;

class As11{

void main(){

Scanner sc = new Scanner(System.in);

//taking input of size


System.out.println("Enter size of m");

int m = sc.nextInt();

System.out.println("Enter size of n");

int n = sc.nextInt();

if((m>2)&&(m<10)&&(n>2)&&(n<10)){

//taking input of elements

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

System.out.println("Enter elements");

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

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

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

//display original matrix

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

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

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

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

}
System.out.println();

//sorting

int temp = 0;

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

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

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

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

temp = a[i][j];

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

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

//displaying rearranged matrix

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

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

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

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

}
System.out.println();

}else{

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

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

m int To store the size of the array Global to main()

n int To store the size of the array Global to main()

a[][] int To store the input Global to main()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

temp int To be used for exchanging values Global to main()


ASSIGNMENT 12
Problem Statement :

Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words are to
be separated by a single blank space and are in UPPER CASE.

Perform the following tasks:

1. Check for the validity of the accepted sentence only for the terminating character.
2. Arrange the words in ascending order of their length. If two or more words have the same length,
then sort them alphabetically.
3. Display the original sentence along with the converted sentence.

Test your program for the following data and some random data

ALGORITHM :

Step 1 : To input the sentence

Step 2 : To check for validity of the sentence as per given condition

Step 3 : To arrange words in ascending order of length

Step 4 : if length is same, arranging words alphabetically

Step 5 : Display original and converted sentence

Step 6 : Stop

CODING :

import java.util.*;

class As12{

void main(){
Scanner sc = new Scanner(System.in);//declaring scanner class

System.out.println("Enter sentence");//taking input

String s = sc.nextLine();

if((s.charAt(s.length()-1)=='.')||(s.charAt(s.length()-1)=='!')||(s.charAt(s.length()-1)=='?')){

s =s.substring(0,s.length()-1);

StringTokenizer str = new StringTokenizer(s);

int count = str.countTokens();

String a[] = new String[count];

//arranging sentence

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

a[i] = str.nextToken();

String temp = "";

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

for(int j = 0;j<count-i-1;j++){
if(a[j].length()==a[j+1].length()){

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

temp = a[j];

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

a[j+1] = temp;

}else if(a[j].length()>a[j+1].length()){

temp = a[j];

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

a[j+1] = temp;

//displaying output

System.out.println("Original Sentence = "+s);

System.out.print("Rearranged Sentence = ");

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

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

}else

System.out.println("Invalid Input");//output for invalid input


}

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

s String To store the input string Global to main()

count int To store the number of words in s. Global to main()

a[] String To store the words of s Local to if

i int Loop Variable Local to for()

temp String To be used for exchanging values Local to if

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()


ASSIGNMENT 13
Problem Statement :
ALGORITHM :

Step 1 : taking input of the string

Step 2 : Converting it to uppercase

Step 3 : finding the number of words starting and ending with a vowel

Step 4 : Placing the words starting and ending with a vowel at first and then placing the rest of the words

Step 5 : Displaying original and rearranged sentence

Step 6 : Stop

CODING :

import java.util.*;

class As13{

void main(){

Scanner sc = new Scanner(System.in);//declaring scanner class

System.out.println("Enter Sentence");//taking input

String s = sc.nextLine();

s = s.toUpperCase();

if((s.charAt(s.length()-1)=='.')||(s.charAt(s.length()-1)=='!')||(s.charAt(s.length()-1)=='?')){

s = s.substring(0,s.length()-1);

StringTokenizer str = new StringTokenizer(s);

int count = str.countTokens();


int sum = 0;

String a = "";

String temp = "";

while(str.hasMoreTokens()){

a = str.nextToken();

if(((a.charAt(0)=='A')||(a.charAt(0)=='E')||(a.charAt(0)=='I')||(a.charAt(0)=='O')||(a.charAt(0)=='U'))&&((
a.charAt(a.length()-1)=='A')||(a.charAt(a.length()-1)=='E')||(a.charAt(a.length()-1)=='I')||(a.charAt(a.lengt
h()-1)=='O')||(a.charAt(a.length()-1)=='U'))){

System.out.print(a+" ");//displaying words starting and ending with a vowel

sum++;

}else

temp = temp +a+" ";

temp = temp.trim();

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

System.out.println();

System.out.println("No. Of words beginning and ending with a vowel = "+sum);//displaying their


frequency

}else

System.out.println("Invalid Input");//output in case of invalid input

}
}

OUTPUT :

VDT :

Variable Data Type Purpose Scope


Name

s String To store the sentence input Global to main()

count int To store the number of words of s Local to if

sum int To store the number of words satisfying Local to if


the condition

temp String To store the words not satisfying the Local to if


condition

a String To store individual words Local to if


ASSIGNMENT 14
Problem Statement :
ALGORITHM :
Step 1 : taking input and converting to uppercase
Step 2 : Checking for validity of the sentence
Step 3 : counting no. of vowels and consonants in each word
Step 4 : generating the frequency in the form of a bar graph
Step 5 : Writing condition for invalid input
Step 6 : Stop

CODING :

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

s String To store the input sentence Global to main()

count int To store the number of tokens Local to if

v int To store the number of vowels of each Local to if


word

c int To store the number of consonants of each Local to if


word

i int Loop variable Local to for()

a String To temporarily store each word Local to while()

ch char To temporarily store the character of each Local to for()


word

k int Loop variable Local to for()

k int Loop variable Local to for()


ASSIGNMENT 15
Problem Statement :
ALGORITHM :
Step 1 : Taking input
Step 2 : Checking for Invalid Input
Step 3 : takeing out the factors of the input and adding them to an array
Step 4 : Checking the elements of the array for the condition of hamming number
Step 5 : displaying the output
Step 6 : Stop

CODING :
import java.util.*;

class As15{
void main(){
Scanner sc = new Scanner(System.in);//declaring scanner class

System.out.println("Enter number");//taking input of number


int n = sc.nextInt();
int temp = 0;

int a[] = new int[n];


if(n<0){
System.out.println("INVALID INPUT");//output in case of invalid input
}else{

for(int j = n;j>1;){
for(int i = 2;i<=j;i++){
if(j%i==0){
a[temp] = i;
j/=i;
temp++;
break;
}
}
}
}
int p = 0;
System.out.print(n+" : ");
//checking for hamming number
for(int i = 0;i<temp;i++){
if(i==temp-1)
System.out.print(a[i]);
else
System.out.print(a[i]+" x ");
if(a[i]!=3&&a[i]!=2&&a[i]!=5)
p++;
}

//displaying output
System.out.println();
if(p==0)
System.out.println("Hamming Number");
else
System.out.println("Not A hamming Number");
}
}

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

n int To store the input Global to main()

p int To check for factors other than 2,3 & 5 Global to main()

i int Loop variable Local to for()

temp int To count no. of factors satisfying the Global to main()


condition

j int Loop variable Local to for()

i int Loop variable Local to for()

a[] int To store factors of n Global to main()


ASSIGNMENT 16
Problem Statement :
Write a program to check if a given string is an Anagram of another string. Two strings are
anagrams if they can be rearranged to form the same string. For example, "listen" and "silent"
are anagrams.

Accept two strings from the user and check if they are anagrams of each other. Ensure that the
comparison is case-insensitive and ignores spaces. Display an appropriate message based on
whether they are anagrams or not. If any of the strings contain invalid characters (e.g., numbers
or special characters), generate an error message.

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

Example 1

INPUT:

Enter first string: Listen


Enter second string: Silent

OUTPUT: STRINGS ARE ANAGRAMS

Example 2

INPUT:
Enter first string: Dormitory
Enter second string: Dirty room

OUTPUT:

STRINGS ARE ANAGRAMS

Example 3

INPUT:

Enter first string: Hello


Enter second string: World

OUTPUT: STRINGS ARE NOT ANAGRAMS


Example 4
INPUT:
Enter first string: Test123
Enter second string: 321tset

OUTPUT :
INVALID CHARACTERS IN STRING. INVALID INPUT

ALGORITHM :
Step 1 : taking input for two strings and converting it to lowercase
Step 2 : Checking for invalid characters in the string
Step 3 : Comparing all the characters of the strings with each other by adding one’s to the array
Step 4 : Filling with spaces the matched characters
Step 5 : Displaying appropriate message in case the whole array is filled with spaces or not
Step 6 : Stop

CODING :

import java.util.*;

class As16{
void main(){

Scanner sc = new Scanner(System.in);

System.out.println("Enter first String");


String s1 = sc.nextLine();
System.out.println("Enter second String");
String s2 = sc.nextLine();

s1 = s1.toLowerCase();
s2 = s2.toLowerCase();

char a[] = new char[s1.length()];

int t = 0;
//checking for invalid characters
for(int i = 0;i<s1.length();i++){
if(s1.charAt(i)==' ')
continue;
int ch = s1.charAt(i);
if((ch>=65&&ch<=91)||(ch>=97&&ch<=123)){

}else{
t++;
break;
}
}
if(t==0){
//checking if strings are anagram
for(int i = 0;i<s1.length();i++){
char ch = s1.charAt(i);
a[i] = ch;
}

for(int i = 0;i<s2.length();i++){
char ch = s2.charAt(i);
for(int j = 0;j<s1.length();j++){
if(ch==a[j]){
a[j] = ' ';
break;
}
}
}
int p = 0;
for(int i = 0;i<a.length;i++){
if(a[i]!=' '){
p++;
}
}

//displaying Output
if(p>0){
System.out.println("Strings are not Anagram");
}else
System.out.println("Strings are Anagram");
}else
System.out.println("Invalid characters in string. Invalid Input");

}
}
OUTPUT :
VDT :
Variable Data Type Purpose Scope
Name

s1 String To store input Global to main()

s2 String To store input Global to main()

a[] int To store characters of s1 Global to main()

t int To check for invalid characters Global to main()

i int Loop variable Local to for()

ch int To store the ASCII value of characters Local to for()

i int Loop variable Local to for()

ch char To store characters of s1 Local to for()

i int Loop variable Local to for()

ch char To store characters of s2 Local to for()

j int Loop variable Local to for()

p int To check if Strings are Anagram Global to main()


ASSIGNMENT 17
Problem Statement :
ALGORITHM :
Step 1 : take input of the size of the array and it’s elements
Step 2 : displaying the iinput matrix
Step 3 : rotating the matrix 270 degrees
Step 4 : finding out the sum of the diagonals of the input matrix
Step 5 : displaying the rotated matrix and the sum
Step 6 : Stop

CODING :
import java.util.*;

class As17{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);

//taking value of m and n from user


System.out.println("Enter the value of m");
int m = sc.nextInt();
System.out.println("Enter the value of n");
int n = sc.nextInt();

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


if((m>2)&&(n>2)&&(m<10)&&(n<10)){
//taking input
System.out.println("Enter the elements");
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
a[i][j] = sc.nextInt();
}
}

//Displaying Original Matrix


System.out.println("Original Matrix");
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}

//Displaying Rotated Matrix


System.out.println("Rotated Matrix (270 degree anti clockwise)");
for(int i = 0;i<n;i++){
for(int j = m-1;j>=0;j--){
System.out.print(a[j][i]+" ");
}
System.out.println();
}

int sd = 0;
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
if(a[i][j]%2!=0)
sd+=a[i][j];
}
}
System.out.println("Sum of Odd elements : "+sd);
}else
System.out.println("Size out of range");
}
}

OUTPUT :
VDT :
Variable Data Type Purpose Scope
Name

m int To store the number of rows Global to main()

n int To store the number of columns Global to main()

a[][] int To store input elements Global to main()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()

sd int Loop variable Local to for()


ASSIGNMENT 18
Problem Statement :
ALGORITHM :
Step 1 : declaring variables and making a parameterized constructor
Step 2 : counting number of digits
Step 3 : finding the sum of digits raised to the power of the number of digits
Step 4 : Checking if number is anagram or not
Step 5 : Displaying output
Step 6 : Stop

CODING :
import java.util.*;

class ArmNum{

int n;
int l;
//parameterized constructor
ArmNum(int nn){
n = nn;
l = 0;
}
int sum_pow(){
//to count the number of digits
for(int i = n;i>0;i/=10){
l++;
}

int sum = 0;
for(int i = n;i>0;i/=10){
sum+= Math.pow((i%10),l);
}

//returning the sum


return sum;
}
//checking and displaying is number is Armstrong or not
void isArmstrong(){
if(sum_pow()==n){
System.out.println(n+" is an Armstrong Number");
}else
System.out.println(n+" is not an Armstrong Number");
}
//main method to call other methods
void main(){
ArmNum a = new ArmNum(371);
a.isArmstrong();

}
}

OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

n int To store the number Global to class

l int To store the length of n Global to class

nn int To store the number Global to class

i int Loop variable Local to for()

sum int To store the sum of digits raised to power l Local to for()

i int Loop variable Local to sum_pow()


ASSIGNMENT 19
Problem Statement :

ALGORITHM :
Step 1 : Declaring variables and making a parameterized constructor
Step 2 : finding out the sum of digits
Step 3 : finding out the product of digits
Step 4 : checking if number is spy
Step 5 : Displaying output
Step 6 : Stop

CODING :
import java.util.*;

class Spy{
int num;
//parameterized constructor
Spy(int nn){
num = nn;
}
//Sum of Digits
int sumOfDigits(int i){
if (i==0)
return 0;
else
return (i%10+sumOfDigits(i/10));

}
//Product of Digits
int prodOfDigits(int i){
if(i==0)
return 1;
else
return (i%10*prodOfDigits(i/10));
}

//checking if number is spy


void check(){
if(sumOfDigits(num)==prodOfDigits(num))
System.out.println("SPY NUMBER");
else
System.out.println("NOT A SPY NUMBER");

}
//declaring main method and calling other methods
void main(){
Spy a = new Spy(242);
a.sumOfDigits(num);
a.prodOfDigits(num);
a.check();
}
}
OUTPUT :
VDT :
Variable Data Type Purpose Scope
Name

num int To store input number Global to class

nn int To be used for assigning values to num Global to class

i int To temporarily store value for finding out Global to


the sum of digts od the assigned value sumOfDigits()

i int To temporarily store value for finding out Global to


the sum of digts od the assigned value prodOfDigits()
ASSIGNMENT 20
Problem Statement :
ALGORITHM :
Step 1 : Declaring variables and making a default constructor
Step 2 : To take input word and convert it into uppercase
Step 3 : to check if word starts and ends with a vowel
Step 4 : Displays the word along with an appropriate message
Step 5 : Create a main method and call other methods
Step 6 : Stop

CODING :
import java.util.*;

class Unique{
//declaring variables
String word;
int len;
Unique(){
word = "";
len = 0;
}
//accepting input
void accept(){
System.out.println("Enter word");
Scanner sc = new Scanner(System.in);
word = sc.next();
word = word.toUpperCase();
len = word.length();
}
//checking if word is unique
boolean checkUnique(){

if(((word.charAt(0)=='A')||(word.charAt(0)=='E')||(word.charAt(0)=='I')||(word.charAt(0)=='O')||(word.ch
arAt(0)=='U'))&&((word.charAt(len-1)=='A')||(word.charAt(len-1)=='E')||(word.charAt(len-1)=='I')||(wor
d.charAt(len-1)=='O')||(word.charAt(len-1)=='U')))
return true;
else
return false;
}
//displaying output
void display(){
System.out.println("The word is "+word);
if(checkUnique())
System.out.println("It is a Unique word");
else
System.out.println("It is not a Unique word");
}
//main method to call other methods
void main(){
Unique a = new Unique();
a.accept();
a.checkUnique();
a.display();
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

word String To store the input Global to class

len int To store the length of the word Global to class


ASSIGNMENT 21
Problem Statement :
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
than10. Allow the user to input integers into this matrix. Display appropriate error message for
an invalid input.
Perform the following tasks on the matrix.
(a) Display the input matrix
(b) Shift each row one step upwards so the first row will become the last row 2nd row will be the 1st
row and so on
(c) Display the rotated matrix along with the highest element and its location in the matrix
Test your program for the following data and some random data:
Example 1
INPUT: M =3
N=4
Enter elements in the matrix:
100 90 87 76
200 500 167 998
77 567 89 254
OUTPUT: FORMED MATRIX AFTER ROTATING:
200 500 167 998
77 567 89 254
100 90 87 76
Highest element: 998 ( Row: 0 and Column: 3 )
Example 2
INPUT: M =4
N=3
Enter elements in the matrix:
54 120 187
78 55 289
134 67 89
63 341 122
OUTPUT: FORMED MATRIX AFTER ROTATING:
78 55 289
134 67 89
63 341 122
54 120 187
Highest element: 341 ( Row: 2 and Column: 1 )
Example 3
INPUT: M = 2
N=3
OUTPUT: SIZE IS OUT OF RANGE. INVALID ENTRY
ALGORITHM :
Step 1 : To take input of the size and the elements of the array
Step 2 : Displaying original matrix
Step 3 : Moving every row one step upwards
Step 4 : Finding out the highest element with it’s position
Step 5 : Displaying the highest element and the rearranged matrix
Step 6 : Stop

CODING :
import java.util.*;

class As21{
void main(){
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];

if((M>2)&&(N>2)&&(M<10)&&(N<10)){
//taking input
System.out.println("Enter elements");
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
A[i][j] = sc.nextInt();
}
}

//displaying original Matrix


System.out.println("Original Matrix");
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
System.out.print(A[i][j]+" ");
}
System.out.println();
}

//displaying rotated matrix


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

for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
if(i==M-1){
System.out.print(A[0][j]+" ");
continue;
}
System.out.print(A[i+1][j]+" ");

}
System.out.println();
}
//finding out the highest element
int temp = A[0][0];
int ii = 0,jj = 0;
for(int i = 0;i<M;i++){
for(int j = 0;j<N;j++){
if(temp<A[i][j]){
temp = A[i][j];
ii = i;
jj = j;
}
}
}
//displaying highest element
System.out.print("Highest element = "+temp);
System.out.println(" (Row: "+ii+" Column: "+jj+")");
}else
System.out.println("SIZE OUT OF RANGE. INVALID ENTRY");
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

M int To store the number of rows of A[][] Global to main()

N int To store the number of columns of A[][] Global to main()

A[][] int To store input elements Global to main()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

temp int To store the highest value Global to main()

ii int To store the position of the highest value Global to main()

jj int To store the position of the highest value Global to main()


ASSIGNMENT 22
Problem Statement :
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 for the following data and some random data:

Example 1 :

Example 2 :
ALGORITHM :
Step 1 : to take input of size and elements
Step 2 : To display the original matrix
Step 3 : to find whether the matrix is symmetric
Step 4 : To find the sum of left and right diagonal
Step 5 : To display whether the matrix is symmetric or not and display the sum of diagonals
Step 6 : Stop
CODING :
import java.util.*;

class As22{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter M");
int M = sc.nextInt();

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

if((M>2)&&(M<10)){
//taking input
System.out.println("Enter elements");
for(int i = 0;i<M;i++){
for(int j = 0;j<M;j++){
A[i][j] = sc.nextInt();
}
}
//Displaying Original Matrix
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]+" ");
}
System.out.println();
}
//Checking if A[][] is symmetric or not
int p = 0;
for(int i = 0;i<M;i++){
for(int j = 0;j<M;j++){
if(A[i][j]!=A[j][i])
p++;
}
}
if(p>0)
System.out.println("The Given Matrix is not Symmetric");
else
System.out.println("The Given Matrix is Symmetric");

int sum1 = 0,sum2 = 0;


for(int i = 0;i<M;i++){
for(int j = 0;j<M;j++){
if(i==j)
sum1+=A[i][j];

if(i+j==M-1)
sum2 += A[i][j];
}
}
//displaying sum of right & left diagonal
System.out.println("Sum of Right Diagonal: "+sum1);
System.out.println("Sum of Left Diagonal: "+sum2);

}else
System.out.println("SIZE OUT OF RANGE. INVALID ENTRY");

}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

M int To store the number of rows and columns Global to main()


of A[][]

A[][] int To store the elements input by the user Global to main()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

p int To check if A[][] if symmetric Global to main()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

sum1 int To store the sum of right diagonal Global to main()

sum2 int To store the sum of left diagonal Global to main()


ASSIGNMENT 23
Problem Statement :

1. Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be greater than 3
and less than 10. Allow the user to accept three different characters from the keyboard and fill the
array according to the instruction given below:

(i) Fill the four corners of the square matrix by character 1.

(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.

(iii) Fill the non-boundary elements of the matrix by character 3.

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

Example 1:

INPUT: N = 4

FIRST CHARACTER: @

SECOND CHARACTER: ?

THIRD CHARACTER: #

OUTPUT:

@??@

?##?

?##?

@??@

Example 2: INPUT: N = 5

FIRST CHARACTER: A
SECOND CHARACTER: C

THIRD CHARACTER: X

OUTPUT:

ACCCA

CXXXC

CXXXC

CXXXC

ACCCA

Example 3: INPUT: N = 15

OUTPUT: SIZE OUT OF RANGE

ALGORITHM :
Step 1 : to take input of the size of the array and the three characters
Step 2 : To check the input size for validity
Step 3 : To declare the array as per the size entered
Step 4 : to fill the array with the three characters entered by the user
Step 5 : To display the output array
Step 6 : Stop

CODING :
import java.util.*;

class As23{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);
//input for rows and columns
System.out.println("Enter the value of n");
int n = sc.nextInt();
if((n>3)&&(n<10)){
char m[][] = new char[n][n];//declaring array
//input for characters
System.out.println("Enter Character one");
char c1 = sc.next().charAt(0);
System.out.println("Enter Character two");
char c2 = sc.next().charAt(0);
System.out.println("Enter Character three");
char c3 = sc.next().charAt(0);
//alloting characters
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if((i==0&&j==0)||(i==n-1&&j==n-1)||(i==0&&j==n-1)||(i==n-1&&j==0))
m[i][j] = c1;
else if(i==0||j==0||i==n-1||j==n-1)
m[i][j] = c2;
else
m[i][j] = c3;
}
}
//displaying output
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}

}else
System.out.println("Size Out Of Range");

}
}
OUTPUT :

VDT:
Variable Data Type Purpose Scope
Name

n int To store input for rows and Columns Global to main()

m[][] char To store input elements Global to main()

c1 char To store input of character Global to main()

c2 char To store input of character Global to main()

c3 char To store input of character Global to main()

i int Loop Variable Local to for()

j int Loop Variable Local to for()

i int Loop Variable Local to for()

j int Loop Variable Local to for()


ASSIGNMENT 24
Problem Statement :
ALGORITHM :
Step 1 : To take input of the size and elements of the array
Step 2 : To check whether all elements are greater than zero
Step 3 : To check whether the sum of rows is equal to 1
Step 4 : To check whether the sum of columns is equal to 1
Step 5 : To display output with an appropriate message
Step 6 : Stop
CODING :
import java.util.*;

class As24{
void main(){
//declaring scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
int n = sc.nextInt();
//declaring array which will store input
double m[][] = new double[n][n];

if((n>=3)||(n<=9)){

int neg = 0;
//taking input
System.out.println("Enter elements");
for(int i = 0;i<n;i++){
for(int j= 0;j<n;j++){
m[i][j] = sc.nextDouble();
if(m[i][j]<0)
neg++;
}}
//check for doubly markov matrix
if(neg==0){
int ex1 = 0,ex2 = 0;
double sumr = 0, sumc = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
sumr+=m[i][j];
}
if(sumr!=1){
ex1++;
break;
}
sumr = 0;
}
for(int j = 0;j<n;j++){
for(int i = 0;i<n;i++){
sumc +=m[i][j];
}
if(sumc!=1){
ex2++;
break;
}
sumc = 0;
}
//displaying output
if((ex1>0)||(ex2>0))
System.out.println("Not a Doubly Markov Matrix");
else
System.out.println("Doubly Markov Matrix");

}
if(neg>0)
System.out.println("Not A Doubly Markov Matrix");
}else
System.out.println("Out of Bounds");
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

n int To store the no. of rows and columns Global to main()

m[][] int To store input elements Global to main()

neg int To check for any negative no Global to main()

i int Loop variable Local to for()

j int Loop variable Local to for()

ex1 int To check for condition of markov matrix Global to main()

ex2 int To check for condition of markov matrix Global to main()

sumr int To store sum of rows Global to main()

sumc int To store sum of columns Global to main()

i int Loop variable Local to for()

j int Loop variable Local to for()

i int Loop variable Local to for()

j int Loop variable Local to for()


ASSIGNMENT 25
Problem Statement :

ALGORITHM :
Step 1 : To declare variables and initializing them with a parameterized constructor
Step 2 : To create methods Check() for checking whether a no. is automorphic or not
Step 3 : To create method list() to find out the list of automorphic numbers between l and u and display
them.
Step 4 : To count and display the frequency of automorphic numbers
Step 5 : to create a main method and call other methods
Step 6 : Stop
CODING :
import java.util.*;

class Automorphic{
int l, u;
int count;

Automorphic(int ll, int uu){


l = ll;
u = uu;
}

boolean check(int n){


int temp = n;
int d = 0;
while(temp>0){
temp/=10;
d++;
}

if(n==((n*n)%Math.pow(10,d))){
return true;
}else
return false;
}
void list(){

System.out.println("Number\tSquare");

for(int i = l;i<=u;i++){
if(check(i)){
System.out.println(i+"\t"+(i*i));
count++;
}
}
System.out.println("Frequency of Automorphic Numbers between "+l+" and "+u+" is "+ count);
}
void main(){
Automorphic a = new Automorphic(1,1000);
a.list();
}
}
OUTPUT :
VDT :

Variable Data Type Purpose Scope


Name

l int To store the lower limit Global to class

u int To store the upper limit Global to class

ll int To assign value to l Global to class

uu int To assign value to u Global to class

count int To count the number of digits of n Global to class

temp int To temporarily store the value of n Global to check(int n)

d int To store the frequency Global to check(int n)

n int To store number on a temporary basis Global to check(int n)

You might also like