Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
259 views

Computer Science Project On Java Language

This document contains an assessment sheet for a student named Alok Singh who is in class 12th C. It includes details of the student like roll number, subject (Computer Science), and teacher (Mrs Manju ma'am). The document then lists 30 programming problems/programs along with the student's solutions. For each problem, it provides the problem statement, algorithm, code solution in Java, and sample output. It acts as a record of programs solved by the student as part of their Computer Science assessment.

Uploaded by

Alok Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views

Computer Science Project On Java Language

This document contains an assessment sheet for a student named Alok Singh who is in class 12th C. It includes details of the student like roll number, subject (Computer Science), and teacher (Mrs Manju ma'am). The document then lists 30 programming problems/programs along with the student's solutions. For each problem, it provides the problem statement, algorithm, code solution in Java, and sample output. It acts as a record of programs solved by the student as part of their Computer Science assessment.

Uploaded by

Alok Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 163

ASSESSMENT SHEET

NAME – Alok Singh


CLASS – 12th C
ROLL NO. –
SESSION – 2019 – 2020
SUBJECT – COMPUTER SCIENCE
SUBJECT – Mrs Manju ma’am
TEACHER

SIGNATURE

INTERNAL EXAMINER –

EXTERNAL EXAMINER–
ACKNOWLEDGEMENT

INDEX
S.NO TOPIC TEACHER’S SIGNATURE
1. Program 1
2. Program 2
3. Program3
4. Program 4
5. Program 5
6. Program 6
7. Program 7
8. Program 8
9. Program 9
10. Program 10
11. Program 11
12. Program 12
13. Program 13
14. Program 14
15. Program 15
16. Program 16
17. Program 17
18. Program 18
19. Program 19
20. Program 20
21. Program 21
22. Program 22
23. Program 23
24. Program 24
25. Program 25
26. Program 26
27. Program 27
28. Program 28
29. Program 29
30. Program 30

Program – 1
Write a program to accept a double dimensional array from
the user with the dimension of user‟s choice and sort the left
diagonal elements and arrange the elements back to its place
in sorted form.
Example:
Dimension: (3 x 3)
Original Matrix:
3 5 6
4 9 8
7 3 2
Rearranged Matrix:
2 5 6
4 3 8
7 3 9

ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 -create main ( )
STEP 4 - make scanner object „sc‟
STEP 5 - accept the size in „m‟
STEP 6 -check the „m‟ is it with in range or not
STEP 7 - if „m‟ is out of range then print the statement
“size is out of range”
STEP 8 - if size is within range then do STEP 9
STEP 9 -accept the elements in array “A[][]”
STEP10-print the original array
STEP11- declare a new array „a[ ]‟to store diagonal
Elements
STEP12- extract the diagonal elements and storing in A[ ]
STEP13- sort the diagonal element in ascending order
STEP14- put the elements to back to its place to 2D matrix
STEP15- print the rearranged matrix
STEP16 - END
SOLUTION:

import java.util.*;
public class program_1 //creating class
{
public static void main()//creating main( )
{
Scanner sc=new Scanner(System.in);
//making scanner object ‘sc’
System.out.println("Enter Size :");
int m=sc.nextInt();
if(m<3||m>9)//Required Size
System.out.println("Size out of Range :");
else
{
int A[][]=new int[m][m];
System.out.println("Enter Values :");
for(int i=0;i<m;i++)//accepting elements in array
{
for(int j=0;j<m;j++)
A[i][j]=sc.nextInt();
}
System.out.println("Orignal Matrix :");
for(int i=0;i<m;i++) //printing original array
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int a[]=new int[m];//Array for saving the diagonal elements
for(int i=0;i<m;i++)
{
a[i]=A[i][i];//diagonal Elements
}
for(int i=0;i<m;i++)
{
for(int j=0;j<m-1;j++)
{
if(a[j]>a[j+1])//sorting in ascending order
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<m;i++)
A[i][i]=a[i];//putting elements to back to its place
System.out.println("Rearranged Matrix : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
}
}
Output:
Enter Size :
3
Enter Values :
3
4
5
1
2
6
9
7
8
Orignal Matrix :
3 4 5
1 2 6
9 7 8
Rearranged Matrix :
2 4 5
1 3 6
9 7 8
Program - 2
Write a program to accept a double dimensional array from
the user with the dimension of user‟s choice and sort theright
diagonal elements and rearrange the elements back to its
place in sorted form.

Example:
Dimension: (3 x 3)
Original Matrix:
2 5 6
9 8 7
4 2 1
Rearranged Matrix:
2 5 4
9 6 7
8 2 1
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 -create main ( )
STEP 4 - make scanner object „sc‟
STEP5 - accept the size in „m‟ from user
STEP 6 - check the „m‟ is it with in range or not
STEP 7 - if „m‟ is out of range then print the statement
“size is out of range”
STEP 8 - if size is within range then do STEP 9
STEP 9 - accept the elements in array “A[][]”
STEP10- print the original array
STEP11- declare a new array „a[ ]‟to store diagonal
Elements
STEP12- extract the diagonal elements and storing in A[ ]
STEP13- sort the diagonal element in ascending order
STEP14- put the elements to back to its place to 2D matrix
STEP15- print the rearranged matrix
STEP16 - END
SOLUTION:

import java.util.*;
public class program_2//creating class
{
public static void main()//creating main( )
{
Scanner sc=new Scanner(System.in);
//making scanner object ‘sc’
System.out.println("Enter Size :");
int m=sc.nextInt();//accepting the value from user
if(m<3||m>9)//Required Size
System.out.println("Size out of Range :");
else
{
int A[][]=new int[m][m];
System.out.println("Enter Values :");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
A[i][j]=sc.nextInt();
}
System.out.println("Orignal Matrix :");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
int a[]=new int[m];//array for saving the diagonals
for(int i=0;i<m;i++)
{
a[i]=A[i][m-1-i];//diagonal Elemnts
}
for(int i=0;i<m;i++)
{
for(int j=0;j<m-1;j++)
{
if(a[j]>a[j+1])//sorting in ascending order
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int i=0;i<m;i++)
A[i][m-1-i]=a[i];//putting elements back to its place
System.out.println("Rearranged Matrix : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
}
}
Output
Enter Size :
3
Enter Values :
1
3
5
4
2
9
7
8
6
Orignal Matrix :
1 3 5
4 2 9
7 8 6
Rearranged Matrix :
1 3 2
4 5 9
7 8 6
Program - 3
Write a program to accept a double dimensional array with
dimension given by the user, make a Mirror Matrix of the
original by the columns, and print it.

Also find the sum of all the corner elements of the given
matrix and print it along with the rotated matrix.

Example:
Dimension: (3 x 3)
Original Matrix:
1 3 5
2 4 9
8 6 7
Matrix after Rotation:
5 3 1
9 4 2
7 6 8

Sum of Elements of four corners of Matrix :21


ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 -create main ( )
STEP 4 - make scanner object „sc‟
STEP5 - accept the size in „m‟ from user
STEP 6 - check the „m‟ is it with in range or not
STEP 7 - if „m‟ is out of range then print the statement
“size is out of range”
STEP 8 - if size is within range then do STEP 9
STEP 9 - accept the elements in array “A[][]”
STEP10- print the original array
STEP11- print the rotated matrix
STEP12- find the sum of corner elements and store in „s‟
STEP13- print the sum of corner
STEP14- END
SOLUTION:

import java.util.*; //calling java.util package


class program_3
{
public static void main() //creating main( )
{
Scanner sc=new Scanner(System.in); //creating object
System.out.println("Enter Size :");
int m=sc.nextInt(); // accepting the size
if(m<2||m>10)
System.out.println("Size out of range :");
else
{
int A[][]=new int[m][m]; //declaring array
System.out.println("Enter Values :");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
A[i][j]=sc.nextInt(); //user enters matrix
}
System.out.println("Orignal Matrix :");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.print(A[i][j]+"\t"); //matrix rotated
System.out.println();
}
System.out.println("Matrix after Rotation :");
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
System.out.print(A[i][j]+"\t");
System.out.println();
}
int s=A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1];//
sum of four corners
System.out.println("Sum of Elements of four corners of
Matrix : "+s);
}
}
}
Output:
Enter Size :
4
Enter Values :
3
6
4
5
7
1
9
2
8
11
19
18
16
12
17
13
Orignal Matrix :
3 6 4 5
7 1 9 2
8 11 19 18
16 12 17 13
Matrix after Rotation :
5 4 6 3
2 9 1 7
18 19 11 8
13 17 12 16
Sum of Elements of four corners of Matrix : 37
Program- 4
Write a program to accept two matrix of different size, check
if the matrices are compatible for multiplication, if yes then
multiply the two matrix and display the resulting matrix, if not
then display appropriate message.
Example:
Enter the number of rows and columns of first matrix
2
2
Enter the elements of first matrix
12
34
Enter the number of rows and columns of second matrix
2
4
Enter the elements of second matrix
1234
5678
Product of entered matrices:-
11 14 17 20
23 30 37 44
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 -create main ( )
STEP 4 - declare the required variables
STEP 5 - make scanner object „sc‟
STEP 6 - accept the size of row and column of first array
STEP 7 - declare the first array
STEP 8 - accept the element of first array
STEP 9 - accept the size of second array from user
STEP10 - check the condition if(b!=p) then GOTO STEP11
else GOTO STEP12
STEP11 - print statement "Matrices with entered orders can't
be multiplied with each other”
STEP12 - declare a array to store multiplication of two array
STEP13 – print the multiplication of array
STEP14- END
SOLUTION:
import java.util.*;
class program_4 //creating class
{
public static void main(String args[]) //creating main
{
int a, b, p, q, sum = 0, i, j, k; //declaring variables

Scanner sc = new Scanner(System.in); //creating object


System.out.println("Enter the number of rows and
columns of first matrix");
a = sc.nextInt(); //accepting size
b = sc.nextInt();

int arr1[][] = new int[a][b]; //declaring array

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


for ( i = 0 ; i < a ; i++ )
for ( j = 0 ; j < b ; j++ )
arr1[i][j] = sc.nextInt();
System.out.println("Enter the number of rows and columns
of second matrix");
p = sc.nextInt(); //accepting size
q = sc.nextInt();

if ( b != p )
System.out.println("Matrices with entered orders can't
be multiplied with each other.");
else
{
int arr2[][] = new int[p][q];
int mult[][] = new int[a][q];
System.out.println("Enter elements of second Matrix:”);
for ( i = 0 ; i < p ; i++ )
for ( j = 0 ; j < q ; j++ )
arr2[i][j] = sc.nextInt();
for ( i = 0 ; i < a ; i++ )
{
for ( j = 0 ; j < q ; j++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + arr1[i][k]*arr2[k][j]; //sum
}

mult[i][j] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices:-");
for ( i = 0 ; i < a ; i++ )
{
for ( j = 0 ; j < q ; j++ )
System.out.print(mult[i][j]+"\t"); //printing

System.out.print("\n");
}
}
}
}
Output:
Enter the number of rows and columns of first matrix
3
3
Enter elements of first matrix
1
2
3
4
5
6
7
8
9
Enter the number of rows and columns of second matrix
3
4
Enter elements of second matrix
1
2
3
4
5
6
7
8
9
10
11
12
Product of entered matrices:-
38 44 50 56
83 98 113 128
128 152 176 200
Program-5
Write a program to accept a one dimensional array by the user
and sort the array using Insertion sorting and print the sorted
matrix.

Example:

Original Array:
6
5
4
9
7
8
3
1
2
8
Sorted Array:
1
2
3
4
5
6
7
8
8
9
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create main ( )
STEP 4 - make scanner object „sc‟
STEP 5 - accept the size of row and column of array
STEP 6 - declare the array
STEP 7 - accept the element of array from user
STEP 9 - declare the required variables
STEP10 - check the condition if(b!=p) then GOTO STEP11
else GOTO STEP12
STEP11 - print statement "Matrices with entered orders can't
be multiplied with each other”
STEP12 - declare a array to store multiplication of two array
STEP13 – print the multiplication of array
STEP14- END
SOLUTION:
import java.util.*;
public class program_5 //creating class
{
public static void main() //creating main()
{
Scanner sc=new Scanner(System.in); //creating object
System.out.println("Enter size of array :");
int n=sc.nextInt(); //accepting size
int a[]=new int[n];
System.out.println("Enter Array elements :");
for(int i=0;i<n;i++)
a[i]=sc.nextInt(); //accepting elements
int temp,j;
for(int i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
System.out.println("Sorted Array :");
for(int i=0;i<n;i++)
{
System.out.println(a[i]); //printing
}
}
}
Output:
Enter size of array :
10
Enter Array elements :
6
0
2
3
4
5
7
9
8
1
Sorted Array :
0
1
2
3
4
5
6
7
8
9
Program-6
A class program_6 has a double dimensional array of m x n,
where maximum value of m and n is 10, design the class
program_6 to find the saddle point in the matrix provided by
the user.
Note: (Saddle point in a matrix is a point which is smallest in
its row and greatest in its column, there can be multiple saddle
point in the matrix and no saddle point also.)
Class name: program_6
Instance variables:
int m, int n – to store the number of rows and number of columns for
the given matrix.
int arr[][]- an double dimensional array to store the elements.
Data members:
program_6() – a constructor to store the initial value of the elements
in the array to its maximum size i.e.10.
program_6(int mm, int nn) – a parametrized constructor to sore the
value of mm to m and nn to n.
void get_mat() – to input he values given by the user to the array
arr[][] of size(m x n) with proper message.
void display_mat() – to display the original matrix given by the user
and display the proper message.
void print_saddle_point() – a function to find the saddle point in the
given matrix and print the saddle point if there are any and if there are
no saddle points present, show proper message for the same.
Create the main method and invoke all the member function
listed above.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_6
STEP 4 - declare the required variables and array
STEP 5 - make scanner object „sc‟
STEP 6 - create the program_6( )
STEP 7 - create the program_6 () to accept size of array
STEP 8 - accept the elements in array from user
STEP 9 - create a function to display the original matrix
STEP10 - find out the saddle point in array
STEP11 - display the saddle point
STEP12 - create main()
STEP13 - call all the function for result
STEP14 - END
SOLUTION:
import java.util.*;
public class program_6 //creating class
{
int arr[][]=new int[10][10]; //declaring array
int m,n; //declaring variables
Scanner sc=new Scanner(System.in); //creating object
program_6()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
arr[i][j]=0;
}
}
program_6(int mm,int nn)
{
m=mm; //assign mm to m
n=nn;
}
void get_mat()
{
System.out.println("Enter Values :");
Scanner sc=new Scanner(System.in);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt(); //accepting elements
}
}
}
void display_mat()
{
System.out.println("Orignal Matrix :");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+"\t"); //printing
System.out.println();
}
}
void print_saddle_point()
{
int max,min,x,f=0;
for(int i=0;i<n;i++)
{
min=arr[i][0];
x=0;
for(int j=0;j<n;j++)
{
if(arr[i][j]<min)
{
min=arr[i][j];
x=j;
}
}
max=arr[0][x];
for(int k=0;k<n;k++)
{
if(arr[k][x]>max)
max=arr[k][x];
}
if(max==min)
{
System.out.println("**************");
System.out.println("Saddle point = "+max);
//printing saddle point
System.out.println("Present at row = "+(i+1));
System.out.println("Present at column = "+(x+1));
System.out.println("**************");
f=1;
}
}
if(f==0)
{
System.out.println("**************");
System.out.println("No saddle point found ");
// printing no saddle point
System.out.println("**************");
}
}
public static void main() //creating main
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of Rows and columns
:"+"\n");
int q=sc.nextInt();
int w=sc.nextInt();
program_6 ob=new program_6(q,w); //creating object
ob.get_mat(); //invoking function
ob.display_mat();
ob.print_saddle_point();
}
}
Output:
Enter number of Rows and columns :
3
3
Enter Values :
1
2
3
4
5
6
7
8
9
Orignal Matrix :
1 2 3
4 5 6
7 8 9
**************
Saddle point = 7
Present at row = 3
Present at column = 1
**************
Question-7
A class program_7 is designed to input a number by the user
and check if the number is circular prime or not., design the
class program_7 to find the condition to be true for them
being circular prime for the number provided by the user.
(NOTE: A circular prime is a prime number with the
property that the number generated at each
intermediate step when cyclically permuting its (base 10)
digits will beprime.
Example -1139)

Class name: program_7


Data Members:
boolean isPrime( int n) – a boolean function to check if the number
entered by the user is a prime number, return true if the number is
prime else return false.
int circulate( int n) – to shuffle the digits of the number to be
checked, shuffle digits one by one from back and check the shuffled
number again and again to check for it being prime.
void isCircularPrime( int n) – to data member to check if the given
number is circular prime or not and print the result for it being
circular prime or not.Create main method and invoke all the
above listed data members and run the program.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_7
STEP 4 - create the boolean isPrime()
STEP 5 - declare the required variables
STEP 6 - make scanner object „sc‟
STEP 7 -accept the number from the user
STEP 8 - check the circular prime number
STEP 9 - create main()
STEP10 - call all the function for result
STEP11 - END
SOLUTION:
import java.util.*; //calling java package
import java.io.*;
class program_7 //creating class
{
boolean isPrime(int n)
{
int c=0; //declaring variable
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
int circulate(int n)
{
String s=Integer.toString(n); //declaring string
variables
String p=s.substring(1)+s.charAt(0);
int a=Integer.parseInt(p);
return a;
}
void isCircularPrime(int n)
{
int f=0,a=n;
do
{
System.out.println(a);
if(isPrime(a)==false)
{
f=1;
}
a=circulate(a);
}
while(a!=n);
if(f==1)
System.out.println(n+" IS NOT A CIRCULAR
PRIME");//printing
else
System.out.println(n+" IS A CIRCULAR PRIME");
//printing
}
public static void main() //creating main( )
{
Scanner sc=new Scanner(System.in);
program_7 ob=new program_7(); //creating object
System.out.println("Enter a number : ");
int n=sc.nextInt();
ob.isCircularPrime(n); //invoking function
}
}
Output:
Enter a number :
1193

1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
PROGRAM-8
A class SwapSort has been defined to perform string
related operations on a word input. Some of the
members of the class are as follows:

Class name :SwapSort


Data members/instance
variables:
wrd : to store a word
len : integer to store length of the word
swapwrd : to store the swapped word
sortwrd : to store the sorted word
Member functions/methods:
SwapSort( ) : default constructor to initialize data
members with legal initial values
void readword( ) : to accept a word in UPPER CASE
void swapchar( ) : to interchange/swap the first and last
characters of the word in „wrd‟ and stores the new word
in „swapwrd’
void sortword( ) : sorts the characters of the original
word in alphabetical order and stores it in „sortwrd’
void display( ) : displays the original word, swapped
word and the sorted word

Specify the class SwapSort, giving the details of the


constructor( ), void readword( ), void swapchar( and
void Define the main( object and call the functions
accordingly to enable thetask.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_8
STEP 4 - declare the required integer& string variables
STEP 6 - create the default constructor to initialize data
STEP 7 - make scanner object „sc‟
STEP 8 - accept the word in upper case from user
STEP 9 - find out the length of word
STEP10 - swap the character of word and store in new string
STEP11 - sort the character of word and store in new string
STEP12 - create main()
STEP13 - call all the function for result
STEP14 - END
SOLUTION:
import java.util.*;
import java.io.*;
public class program_8 //creating class
{
String wrd,swapwrd,sortwrd; //declaring variables
int len;
program_8()
{
swapwrd="";
sortwrd="";
}
void readword()
{
Scanner sc=new Scanner(System.in); //creating object
System.out.println("Enter word in Upper Case :");
wrd=sc.nextLine(); //accepting word
len=wrd.length(); //finding length
}
void swapchar()
{
swapwrd=wrd.charAt(len-1)+wrd.substring(1,len-
1)+wrd.charAt(0);
}
void sortword()
{
char c;
for(int i=65;i<=90;i++)
{
for(int j=0;j<len;j++)
{
c=wrd.charAt(j);
if(c==i)
sortwrd+=c;
}
}
}
void display() //printing
{
System.out.println("Orignal Word = "+wrd);
System.out.println("Swapped Word = "+swapwrd);
System.out.println("Sorted word = "+sortwrd);
}
public static void main() //creting main
{
program_8 x=new program_8(); //creating object
x.readword(); //invoking function
x.swapchar();
x.sortword();
x.display();
}
}
Output:
Enter word in Upper Case :
PROGRAMMING IS FUN

Orignal Word = PROGRAMMING IS FUN


Swapped Word = NROGRAMMING IS FUP
Sorted word = AFGGIIMMNNOPRRSU
PROGRAM-9
write a program in java to check whether a given number is
special two digit number or not.
Sample input: 59
Sum of digits:5+9=14
Product of digits:5*9=45
Sum of both=59(original number)

Sample output:
59 is a 2 digit special number.
ALGORITHM:

STEP 1 - START
STEP 2 - Create class and main().
STEP 3 - Create the Scanner object.
STEP 4 - Input the value of n from the user.
STEP 5 - Initialize i=n;c=0.
STEP 6 - If i is not equal to 0 then goto step 7 else goto step 9.
STEP 7 - Divide i by 10 and increment c by 10.
STEP 8 - Goto step 6.
STEP 9 - If c is equal to 2 then goto step 10 else goto step 18.
STEP10 - Initialize sum=0,pro=1,i=n.
STEP11 - If i is not equal to 0 then goto step 12 else goto step
16.
STEP12 - Find the remainder when i is divisible by 10.
STEP13 - Store the remainder in sum and add them.
STEP14 - Multiply the remainder by pro and store it in pro.
STEP15 - Goto step 11.
STEP16 - If sum+pro is equal to n then print “special no.”
STEP17 - Goto step 19.
STEP18 - Print “not a 2 digit no.”
STEP19 - End of class and main().
STEP20 - END.
SOLUTION:

import java.util.*;
class special //creating class
{
public static void main()
{
Scanner sc=new Scanner(System.in); //creating scanner
object
int n=sc.nextInt();
int c=0;
for(inti=n;i!=0;i=i/10)
{
c++; //counting number of digits
}
if(c==2) //if number of digit is two
{
int sum=0;
int pro=1;
for(inti=n;i!=0;i=i/10)
{
sum=sum+(i%10); //finding sum of digits
{
pro=pro*(i%10); //finding product of digits
}
if(sum=pro==n) //if two digit special number
{
System.out.println(n+ “is a two digit special no.”);
}
else
{
System.out.println(“not a 2 digit special no.”);
}
}}
Output:

Enter no.
59

59 is a 2 digit special no.


Program-10
Write a program with class name program_10 to print a
matrix in spiral form starting from one, ending in the center
with the greatest integer.
Design the class in such a way that the program makes a
square matrix asking for number of rows or columns only
with start of one. Ending in the center.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_10
STEP 4 - create main ()
STEP 5 - create object sc
STEP 6 - accept size of array from user
STEP 7 - declare the required integer& string variables
and array
STEP 8 - use the while to store the spiral form of array in
array
STEP 9 - use for loop to print the spiral form of array
STEP10- END
SOLUTION:

import java.util.*;
public class program_10 //creating class
{
public static void main() //creating main()
{
Scanner sc = new Scanner(System.in); //creating object
System.out.println("Enter The Value For N :");
int n = sc.nextInt(); //accepting size
int[][] spiral = new int[n][n]; //declaring array
int value = 1;
int minCol = 0;
int maxCol = n-1;
int minRow = 0;
int maxRow = n-1;
while (value <= n*n)
{
for (int i = minCol; i <= maxCol; i++)
{
spiral[minRow][i] = value;
value++;
}
for (int i = minRow+1; i <= maxRow; i++)
{
spiral[i][maxCol] = value;
value++;
}
for (int i = maxCol-1; i >= minCol; i--)
{
spiral[maxRow][i] = value;
value++;
}
for (int i = maxRow-1; i >= minRow+1; i--)
{
spiral[i][minCol] = value;
value++;
}
minCol++;
minRow++;
maxCol--;
maxRow--;
}
for (int i = 0; i < spiral.length; i++)
{
for (int j = 0; j < spiral.length; j++)
{
System.out.print(spiral[i][j]+ "\t"); //printing
}
System.out.println(); //changing row
}
}
}
Output:

Enter The Value For N :


10
1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19
Program-11
Write a program in java to input a matrix and check whether it
is a symmetric matric or not.
Sample input;
1 2 3
2 1 3
3 3 1
Sample output;
Symmetric
ALGORITHM:

STEP 1 - Start
STEP 2 - Create class and the main()function.
STEP 3 - Create Scanner object.
STEP 4 - Create 2 arrays a[][] and b[][] of size
n*n
STEP 5 - Enter the elements from the user in
a[ ][ ]
STEP 6 - Store the elements of „i‟ row and of „j‟ column of
array[][] in the „j‟ row and „i‟ column of b[][].
STEP 7 - Initialize flag=0.
STEP 8 - If a[i][j] is not equal to b[i][j] then goto step 10 else
continue checking.
STEP 9 - Goto step 11.
STEP10 - Make flag=1.
STEP11 - if(flag==1)then goto step 12 else goto step 13.
STEP12 - Print the message “not symmetric” and goto step
14.
STEP11 - Print the message “symmetric”.
STEP12 - Close the main() and the class.
STEP13 - END
SOLUTION:

import java.util.*;
class symmetry //creating the class
{
public static void main()
{
Scanner sc=new Scanner(System.in); //creating Scanner
object
System.out.println(“enter the size”);
int n=sc.nextInt();
int a[][]= new int[n][n]; //creating array
int b[][]=new int[n][n];
System.out.println(“enter the elements”);
for(inti=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt(); //inputting element in first array
}
}
for(inti=0;i<n;i++)
{
for(int j-0;j<n;j++)
{
b[j][i]=a[i][j]; //inputting elements in second array
}
}
int flag=0;
for(inti=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]!=b[i][j]) //checking for symmetry of arrays
{
flag=1; //if not symmetric
break;
}
}
}
if(flag==1)
System.out.println(“not symmetric”)l //printing proper
message
else
System.out.println(“symmetric”);
}
}
Output:

enter the size


3
enter the elements
1
2
3
2
1
3
3
3
1
Symmetric
Program-12
The co-ordinate of a point on a two-dimensional plane can be

represented by P(x,y) and x-coordinate and a y-coordinate, a

class program_12 is declared as following:

Class name: program_12

Instance variable:

double x, double y – double data type for coordinates

Member functions:

program_12() – construct to initialize 0 to x and 0 to y.

void readpoint() – input the coordinate of x and y.

void displaypoint() – to print (x,y) co-ordinates with suitable

headings.

program_12 midpoint(program_12 a, program_12 b) – to

calculate the co-ordinates of a midpoint between the two given point

P1 and P2 and return them.

Create a main method and invoke the above listed methods

sequentially.
Example:
program_12 a program_12 b Returning
Object
Object 1 Object 2

x co-ordinate M1 M2 (M1+M2)/2

y co-ordinate N1 N2 (N1+N2)/2

a b obj

ob std

X 2 4 (2+4)/2 = 3

Y 6 8 (6+8)/2 = 7
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_12
STEP 4 - declare the double type variables to store
co-ordinates
STEP 5 - create constructor to aasign‟ 0‟
STEP 6 - accept the values of co-ordinate
STEP 7 - accept size of array from user
STEP 8 - find out the mid point of two co-ordinates
STEP 9 - return the mid point
STEP10 - create main()
STEP11 - create object
STEP12 - call all the functions to find result
STEP13- END
SOLUTION:

import java.util.*;
public class program_12
{
double x,y;//Data members
program_12()//constructor
{
x=0;
y=0;
}
void readpoint() //to accept values
{
Scanner sc=new Scanner(System.in);
System.out.println("Input Value of X-Coordinate : ");
x=sc.nextDouble();
System.out.println("Input value of Y-coordinate : ");
y=sc.nextDouble();
}
void displaypoint()
{
System.out.println("X-coordinate = "+x);
System.out.println("Y-coordinate = "+y);
}
program_12 midpoint(program_12 a,program_12 b)
//to calculate midpoint
{
program_12 obj=new program_12();
obj.x=(a.x+b.x)/2.0;
obj.y=(a.y+b.y)/2.0;
return obj;
}
public static void main() //creating main()
{
program_12 ob=new program_12(); //object creation
program_12 std=new program_12();
program_12 bb=new program_12();
System.out.println("Input First Coordinate : ");
ob.readpoint(); //calling readpoint( )
System.out.println("Input Second Coordinate : ");
std.readpoint();
bb=bb.midpoint(ob,std);
System.out.println("Output"+"\n_______");
System.out.println("First point : " );
ob.displaypoint();
System.out.println("Second point : ");
std.displaypoint();
System.out.println("Mid Point : ");
bb.displaypoint();
}
}
Output:
Input First Coordinate :
Input Value of X-Coordinate :
2
Input value of Y-coordinate :
3
Input Second Coordinate :
Input Value of X-Coordinate :
4
Input value of Y-coordinate :
5
Output
_______
First point :
X-coordinate = 2.0
Y-coordinate = 3.0
Second point :
X-coordinate = 4.0
Y-coordinate = 5.0
Mid Point :
X-coordinate = 3.0
Y-coordinate = 4.0
Program-13
A class program_13 contains a Double dimensional array of
order (m x n), where maximum value of m and n is 5, design
the class program_13 to shuffle the matrix.

Class Name: program_13

Instance Variables:

int m,n –to store the number of rows and columns of the 2D-Array.

int mat[][] – to store array elements.

Data Members:

program_13(int mm,int nn) – parametrized constructor for m=mm


and n=nn.

void cyclic(program_13 p) – enables the matrix of object p to


program_13 each row upwards in cyclic manner and store in object.

void display() – print the array after the desired operation is


performed

Write the main method to invoke the above listed Data


members.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_13
STEP 4 - declare the required integer variables
and array with size 5*5
STEP 5 - create parametrized constructor
STEP 6 - accept elements of array from user
STEP 7 - swap elements of first row with last row using for
loop
STEP 8 - create display() so as to display cyclic matrix
STEP 9 - create main()
STEP10 - create object
STEP11 - call all the function for result
STEP12 - END
SOLUTION:

import java.util.*; //calling java.util package


public class program_13 //creating class
{
int m;//declaring variables
int n;
int mat[][]=new int[5][5]; //declaring array
program_13(int mm,int nn)
{
m=mm;n=nn; //assigning size
}
void input()
{
Scanner sc=new Scanner(System.in); //creating object
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Input a number : ");
mat[i][j]=sc.nextInt();//accepting elements
}
}
}
void cyclic(program_13 p)
{
for(int i=1;i<m;i++)
{
for(int j=0;j<n;j++)
{
mat[i-1][j]=p.mat[i][j]; //swifting elements
}
}
for(int j=0;j<n;j++)
{
mat[m-1][j]=p.mat[0][j];
}
}
void display()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
System.out.print(mat[i][j]+"\t"); //printing
System.out.println();
}
}
public static void main() //creating main
{
Scanner sc=new Scanner(System.in);
System.out.print("Number of Rows : ");
int row=sc.nextInt(); //accepting size of row
System.out.print("Number of Column : ");
int col=sc.nextInt();//accepting size of row
program_13 ob1=new program_13(row,col);
program_13 ob2=new program_13(row,col);
ob1.input();
System.out.println("\nOrignal Matrix : ");
ob1.display(); //invoking function
ob2.cyclic(ob1);
System.out.println("\nShuffled Matrix : ");
ob2.display();
}
}
Output:
Number of Rows : 3
Number of Column : 3
Input a number :
3
Input a number :
1
Input a number :
2
Input a number :
5
Input a number :
8
Input a number :
7
Input a number :
9
Input a number :
4
Input a number :
6

Orignal Matrix :
3 1 2
5 8 7
9 4 6

Shuffled Matrix :
5 8 7
9 4 6
3 1 2
Program-14
write a program to input two integers x and y,and calculate
smallest base for x and smallest base for y(likely differ from
x)so that x and y represent the same value the base associated
with x and y will be between 1 and 20(both inclusive) in
representing these numbers the digits 0 to 9 have their usual
decimal interpretations,the upper case alphabetic characters a
trough j represents digits 10 through 19 respectively.

Sample input:
Enter 2 numbers
12
5
Sample output:
First number=12 the second no. is 5
12(base 3)=5(base 1)
ALGORITHM:

STEP 1 - START
STEP 2 - Input two nos in n and m.
STEP 3 - print m and n.
STEP 4 - Initialize x to 1.
STEP 5 - x <=20.
STEP 6 - Initialize i to n, repeat step 7 to 9 until i>0.
STEP 7 - Extract digit from i and store it into d.
STEP 8 - Store power of d in to tt.
STEP 9 - Devide i/10; initialize by 1.
STEP10 - Initialize m to j.
STEP11 - Extract digit from j and store in d.
STEP12 - Store power of digit into kk.
STEP13 - Check if tt =kk than dispalay n base x and m base y.
STEP14 - Increament y by 1, x by 1.
STEP15 - If c is <1 then display n!=m for any base.
STEP16 - END
SOLUTION:

import java.util.*;
class program_14 // creating class
{
public static void main()
{
Scanner sc =new Scanner (System.in); //creating object for
Scanner
for (y=1;y<=20;y++)
{
qq=0;kk=0 ;
for (j=m;j>0;j=j/10)
{
d=j%10;
kk= kk+d*(int) Math.pow(y,qq);
qq++;
}
If(tt=kk)
{
S ystem.out.println(n+ “(base “+x+”)=”+m+”(base”+ y+”)”);
// printing
c++;
x=21;
y=21;
}
}
}
if(c<1)
{
System.println(n+” is not equal”+m+” to any base between 1
to 20”); // printing
}
}
}
Output:

Enter 2 numbers
12
5
First number=12 the second number is 5
12(base3)=5(base1)
Program-15
A class program_15 is designed to accept a sentence ending

with .?or !.and check for number of words starting and ending

with vowels, put those words at the beginning of the sentence

and put all other words after them. Print the number of such

words starting and ending with vowels.

(NOTE: if a sentence doesn‟t end with .? or !, show

appropriate message for the same.)

Example:

1) Enter a Sentence:

John Eats an Apple.

Number of words starting and ending with vowels 2

Apple John Eats an

2) Enter a Sentence:

John Eats an Apple

SENTENCE MUST END WITH . ? or !


ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_15
STEP 4 - create main()
STEP 5 - create object
STEP 6 - declare required
STEP 7 - accept the sentence in string variable
STEP 8 - check sentence is ending with . ? or ! if not then
GOTO STEP 9 else STEP 10
STEP 9 - print message "SENTENCE MUST END WITH . ?
or ! and break
STEP10 - find and count the no of word starting & ending
with vowel
STEP11 – print the no word starting & ending with vowel
STEP12 - END
SOLUTION:

import java.util.*; //calling java.util package


public class program_15 //creating class
{
public static void main()throws IOException //creating main
{
Scanner sc=new Scanner(System.in);
String s,nstr=""; //declaring variables
int c=0;
System.out.println("Enter a String : ");
s=sc.nextLine(); //accepting string
Boolean flag1=false,flag2=false;
s=s.trim();
String vowel="",conso="";
int l=s.length(); //finding length
s=s.toUpperCase(); //converting in uppercase
String s1=s.substring(0,l-1);
if((s.charAt(l-1)=='.')||(s.charAt(l-1)=='?')||(s.charAt(l-1)=='!'))
{
String []a=s1.split(" ");
for(int z=0;z<a.length;z++)
{
nstr=a[z];
char ch=nstr.charAt(0);
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': flag1=true;
break;
default : flag1=false;
}
char ch1=nstr.charAt(nstr.length()-1);
switch(ch1)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': flag2=true;
break;
default : flag2=false;
}
if(flag1==true && flag2==true)
{
vowel+=nstr+" ";
c++;
}
else
conso+=nstr+" ";
}
String fin=vowel+conso;
fin.trim();
System.out.println("NUMBER OF WORDS STARTING
AND ENDING WITH VOWELS : "+c);
System.out.println(fin); //printing
}
else
System.out.println("SENTENCE MUST END WITH . ? or !
");
}
}
Output:
Enter a String :
Apple Bat Can Dose Ego.

NUMBER OF WORDS STARTING AND ENDING WITH


VOWELS : 2
APPLE EGO BAT CAN DOSE
Program-16
A class program_16 is designed to accept the month, year and the

day falling in 1st of the month, and print the whole calendar for the

particular month sequentially.

Example:

Enter the year : 2019

Enter the month name (e.g. January) : May

Enter the week day name (e.g. Sunday) of 1st day of May :

Wednesday

----------------------------------------------------
May 2019
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4
----------------------------------------------------
5 6 7 8 9 10 11
----------------------------------------------------
12 13 14 15 16 17 18
----------------------------------------------------
19 20 21 22 23 24 25
----------------------------------------------------
26 27 28 29 30 31
----------------------------------------------------
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_16
STEP 4 - declare the two array one for month & no of days in
Month
STEP 5 - check whether year is leap year or not if leap year
then change no of days in febuary
STEP 6 - create function for calendar
STEP 7 - create main()
STEP 8 - create object
STEP 9 - accept year ,month &week day name from user
STEP10 - call all the function for calendar
STEP11 - END
SOLUTION:

import java.util.*; //calling java.util package


class program_16 //creating class
{
//Function to match the given month and return its
maximum days
int findMaxDay(String mname, int y)
{
String months[] = {"","January", "February", "March",
"April", "May", "June",
"July", "August", "September", "October",
"November", "December"};
int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if((y%400==0) || ((y%100!=0)&&(y%4==0)))
{
D[2]=29;
}
int max = 0;
for(int i=1; i<=12; i++)
{
if(mname.equalsIgnoreCase(months[i]))
{
max = D[i]; //Saving maximum day of given month
}
}
return max;
}
//Function to match the given weekday name and return
its weekday no.
int findDayNo(String wname)
{
String days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"};
int f = 0;
for(int i=0; i<7; i++)
{
if(wname.equalsIgnoreCase(days[i]))
{
f = i; //Saving week day no. given day (e.g. '0' for
Sunday)
}
}
return f;
}
//Function for creating the calendar
void fillCalendar(int max, int f, String mname, int y)
{
int A[][] = new int[6][7];
int x = 1, z = f;
for(int i=0;i<6;i++)
{
for(int j=f; j<7; j++)
{
if(x<=max)
{
A[i][j] = x;
x++;
}
}
f = 0;
}
for(int j=0; j<z; j++) //Adjustment to bring last (6th) row
elements to first row
{
A[0][j]=A[5][j];
}
printCalendar(A, mname, y); //Calling function to print the
calendar
}
//Function for printing the calendar
void printCalendar(int A[][], String mname, int y)
{
System.out.println("\n\t----------------------------------------
------------");
System.out.println("\t\t\t "+mname+" "+y);
System.out.println("\t-------------------------------------------
---------");

System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tS
AT");
System.out.println("\t-------------------------------------------
---------");
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 7; j++)
{
if(A[i][j]!=0)
System.out.print("\t "+A[i][j]);
else
System.out.print("\t ");
}
System.out.println("\n\t-------------------------------------
---------------");
}
}
public static void main(String args[])
{
program_16 ob = new program_16();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the year : ");
int y = sc.nextInt();
System.out.print("Enter the month name (e.g. January) : ");
String mname = sc.next();
System.out.print("Enter the week day name (e.g. Sunday) of
1st day of "+mname+" : ");
String wname = sc.next();
int max = ob.findMaxDay(mname,y);
int f = ob.findDayNo(wname);
ob.fillCalendar(max,f,mname,y);
}
}
Output:
Enter the year : 2019
Enter the month name (e.g. January) : July
Enter the week day name (e.g. Sunday) of 1st day of July : Monday

----------------------------------------------------
July 2019
----------------------------------------------------
SUN MON TUE WED THU FRI SAT
----------------------------------------------------
1 2 3 4 5 6
----------------------------------------------------
7 8 9 10 11 12 13
----------------------------------------------------
14 15 16 17 18 19 20
----------------------------------------------------
21 22 23 24 25 26 27
----------------------------------------------------
28 29 30 31
----------------------------------------------------
Program-17
A class program_17 is designed to sort the non-boundary
elements of a square double dimensional array (Matrix) and
print the diagonal elements of the matrix in such an order that
they remain at their respective index values and do not shuffle
from their positions and also find the sum of those diagonal
elements.
(NOTE: the number of rows/columns should not be less than
4 and greater than 10)
Class name: program_17
Instance variables:
int A[][] – to store the double dimensional square matrix in it.
int b[] – a single dimensional array to store the non-boundary
elements and later use it to sort those elements in ascending order.
int m,n – variables to store the number of rows and columns, yet both
having same values.(m=n).
Data members:
void input() – a member function to input the elements for the square
matrix A[][] for the size of (m x n).
void convert(int s) – a parametrized member function for storing
non-boundary elements to a single dimensional array and storing them
back to their respective array and in a sorted manner.
void printDiagonal() – a member function to print the sorted double
dimensional array in matrix form and also only the sorted the diagonal
array elements in such a way that there index number elements is not
disturbed and do not shuffle from their original elements in the
shuffled sorted array and also print the sum of diagonal elements.
Write the main method and invoke the above mentioned data
members in sequential manner.
Example:
*********************
The original matrix:
*********************
3 1 5 9
7 6 4 2
8 13 15 16
10 12 14 11
*********************
The Rearranged matrix:
*********************
3 1 5 9
7 4 6 2
8 13 15 16
10 12 14 11
*********************
The Diagonal Elements:
*********************
3 9
4 6
13 15
10 11
Sum of the Diagonal Elements :71
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name program_17
STEP 4 - declare the required variables & array
STEP 5 - create object
STEP 6 - accept the size of array from user
STEP 7 - check whether (size<4 || size>10) if then GOTO
STEP 8 & STEP 9
STEP 8 - print invalid range
STEP 9 - print the diagonal elements
STEP10 - find and store sum of diagonal elements
STEP11 - create main()
STEP12 - call all the function for result
STEP13 - END
SOLUTION:
import java.util.*;
class program_17 //creating class
{
int A[][],B[],m,n;
void input() //Function for taking all the necessary inputs
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
m=sc.nextInt();
if(m<4 || m>10)
{
System.out.println("Invalid Range");
System.exit(0);
}
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n]; //Array to store Non-Boundary
Elements
System.out.println("Enter the elements of the Matrix : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a value : ");
A[i][j]=sc.nextInt();
}
}
}
}
void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}
}
}
}
void sortArray() //Function for sorting Non-Boundary
elements stored in array B[]
{
int c = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(B[i]>B[j])
{
c = B[i];
B[i] = B[j];
B[j] = c;
}
}
}
}
void printArray() //Function for printing the array A[][]
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
void printDiagonal() //Function for printing the diagonal
elements and their sum
{
int sum = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j || (i+j)==m-1)
{
System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println("Sum of the Diagonal Elements : "+sum);
}
public static void main(String args[])
{
program_17 ob = new program_17();
ob.input();
System.out.println("*********************");
System.out.println("The original matrix:");
System.out.println("*********************");
ob.printArray(); //Printing the original array
ob.convert(1); //Storing Non-Boundary elements to a 1-D
array
ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal
Elements)
ob.convert(2); //Storing the sorted Non-Boundary elements
back to original 2-D array
System.out.println("*********************");
System.out.println("The Rearranged matrix:");
System.out.println("*********************");
ob.printArray(); //Printing the rearranged array
System.out.println("*********************");
System.out.println("The Diagonal Elements:");
System.out.println("*********************");
ob.printDiagonal(); //Printing the diagonal elements and
their sum
}
}
Output:

Enter the size of the square matrix : 4


Enter the elements of the Matrix :
Enter a value : 3
Enter a value : 2
Enter a value : 4
Enter a value : 5
Enter a value : 9
Enter a value : 8
Enter a value : 7
Enter a value : 6
Enter a value : 1
Enter a value : 12
Enter a value : 13
Enter a value : 10
Enter a value : 11
Enter a value : 15
Enter a value : 14
Enter a value : 16
*********************
The original matrix:
*********************
3 2 4 5
9 8 7 6
1 12 13 10
11 15 14 16
*********************
The Rearranged matrix:
*********************
3 2 4 5
9 7 8 6
1 12 13 10
11 15 14 16
*********************
The Diagonal Elements:
*********************
3 5
7 8
12 13
11 16
Sum of the Diagonal Elements : 75
Program-18
Two matrices are said to be equal if they have the same
dimension and their corresponding
elements are equal.
For example, the two matrices A and B given below are equal:
Matrix A Matrix B
123 123
245 245
356 356
Design a class EqMat to check if two matrices are equal or
not. Assume that the two
matrices have the same dimension.
Some of the members of the class are given below:

Class name :EqMat


Data members/instance variables:
a[ ][ ] : to store integer elements
m : to store the number of rows
n : to store the number of columns
Member functions/methods:
EqMat(int mm, int nn) : parameterised constructor to
initialise the data members m = mm and n = nn
void readarray( ) : to enter elements in the array
int check(EqMat P, EqMat Q) : checks if the parameterized
objects P and Q are equal and returns 1 if true, otherwise
returns 0
void print( ) : displays the array elements
Define the class EqMat giving details of the constructor( ),
void readarray( ),
int check(EqMat, EqMat) and void print( ). Define the
main( ) function to create objects
and call the functions accordingly to enable the task.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name EqMat
STEP 4 - declare two array & integer variables
STEP 5 – create object
STEP 6 – accept size first & second of array
STEP 7 – accept the elements in first array from user
STEP 8 – accept the elements in second array from user
STEP 9 – check & returnwhether both array are equal or not
STEP10 - create main()
STEP11 - create object
STEP12 - call all the function for calendar
STEP13 - END
SOLUTION:

import java.util.*; //calling java.io package


class EqMat //creating class
{
int a[][]; //declaring array
int m;
int n;
static Scanner sc=new Scanner(System.in); //creating object
EqMat(int mm,int nn)
{ m=mm;
n=nn;
a=new int[m][n];
}
void readarray()
{ System.out.println("enter" + (m*n) + "elements" );
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
a[i][j]=sc.nextInt();//accepting elements
}
int check(EqMat P,EqMat Q)
{ for (int i=0;i<P.m;i++)
for (int j=0;j<P.n;j++)
{ if (P.a[i][j]!=Q.a[i][j])
return 0;
}
return 1;
}
void print()
{ for (int i=0;i<m;i++)
{ System.out.println();
for (int j=0;j<n;j++)
System.out.print(a[i][j]+"\t"); //printing
}
}
public static void main() //creating main
{ EqMat ob1=new EqMat(3,3);
EqMat ob2=new EqMat(3,3);
System.out.println("enter nos for the Ist Matrix");
ob1.readarray(); //invoking function
System.out.println("enter nos for the 2nd Matrix");
ob2.readarray();
if (ob1.check(ob1,ob2)==1)
{ System.out.println("Equal Matrix");
ob1.print();
ob2.print();
}
else
System.out.println("not equal");
}
}
Output:
enter no for the Ist Matrix
enter 9 elements
7
8
9
4
5
6
1
2
3
enter no for the 2nd Matrix
enter 9 elements
7
8
9
4
5
6
1
2
3
Equal Matrix
7 8 9
4 5 6
1 2 3

7 8 9
4 5 6
1 2 3
Program-19
is an entity which can hold maximum of 20
characters. The restriction is that a character can be added or
removed from one end only.
Some of the members of classes are given below:
:

ch[ ] : character array to hold the character elements


capacity : integer variable to store the maximum capacity
top : to point to the index of the topmost element

WordPile( int cap) : constructor to initialise the data member


capacity = cap, top = -1 and create the
WordPile
void pushChar( char v) : adds the character to the top of
WordPile if possible, otherwise output a message “WordPile
is full”
char popChar() : returns the deleted character from the top of
the WordPile if possible, otherwise it returns „\\‟

Specify the class giving the details of the

and .
.
SOLUTION:

public class WordPile //creating class


{
char ch[] = new char[20]; //creating array
int capacity,top; //declaring variables
WordPile(int cap)
{ capacity=cap;//assign size
top=-1;
ch=new char[capacity];
}
void pushChar(char v)
{ if(top<capacity-1)
ch[++top]=v;
else
System.out.println("WordPile is full"); //printing
}
char popChar()
{
if(top>=0)
return ch[top--];
else
return '\\';
}
}
Program-20
A disarium number is a number in which the sum of the
digits to the power of their respective position is equal to
the number itself.
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.

Design a class Disarium to check if a given number is a


disarium number or not. Some
of the members of the class are given below:
Class name :Disarium
Data members/instance variables:
int num : stores the number
int size : stores the size of the number
Methods/Member functions:
Disarium(int nn) : parameterized constructor to initialize
the data members n = nn and size = 0
void countDigit( ) : counts the total number of digits
and assigns it to size
int sumofDigits(int n, int p) : returns the sum of the
digits of the number(n) to the power of their respective
positions(p) using recursive technique
void check( ) : checks whether the number is a disarium
numberand displays the result with an appropriate
message
Specify the class Disarium giving the details of the
constructor( ), void countDigit( ),
int sumofDigits(int, int) and void check( ). Define the
main( ) function to create an object and call the functions
accordingly to enable the task.
ALGORITHM:

STEP 1 - START
STEP 2 - call java.util package
STEP 3 - create the class with name Disarium
STEP 4 - declare the required variables
STEP 5 - create object
STEP 6 - create parameterized constructor to initialize the
data members n = nn and size = 0
STEP 7 - count the no of digits in number
STEP 8 - check no is disrium or not
STEP 9 - create main()
STEP10 - create object
STEP11 - call all the function for result
STEP12 - END
SOLUTION:
import java.util.*; //invoking java.util package
public class Disarium //creating class
{
int num,size;//declaring variables
static Scanner sc=new Scanner(System.in);
Disarium(int nn)
{
num=nn;
size=0;
}
void countDigits()
{
int a=num;
while(a!=0)
{ a=a/10;
size++;
}
}
int sumofDigits(int n, int p)
{
return (n==0)? 0: sumofDigits(n/10,p-1) +
(int)Math.pow(n%10,p);
}
void check()
{
if(num==sumofDigits(num,size))
System.out.print("\n Disarium Number");
else
System.out.print("\n Not a Disarium Number");
}
static void main() //creating main
{
System.out.println("Input a Number");
int m=sc.nextInt();//accepting no
Disarium x= new Disarium(m);
x.countDigits(); //invoking function
x.check();
}
}
Output:
Input a Number
135
Disarium Number
Program-21
A class Shift contains a two dimensional integer array of
order (m×n) where the maximum values of both m and n
is 5. Design the class Shift to shuffle the matrix (i.e. the
first row becomes the last, the second row becomes the
first and so on). The details of the members of the class
are given below:

Class name :Shift


Data member/instance variable:
mat[ ][ ] : stores the array element
m : integer to store the number of rows
n : integer to store the number of columns
Member functions/methods:
Shift(int mm, int nn ) : parameterized constructor to
initialize the data members m = mm and n = nn
void input( ) :to accept the elements
void cyclic(Shift P) : enables the matrix of the object(P)
to shift each row upwards in a cyclic manner and store
the resultant matrix in the current object
void display( ) : displays the matrix elements
Specify the class Shift giving details of the
constructor(), void input( ), void cyclic(Shift)
andvoid display( ). Define the main( ) function to create
an object and call the methods accordingly to enable the
task of shifting the array elements.
ALGORITHM:

STEP 1 - START
STEP 2 - Call java.util package
STEP 3 - Create a class with name shift
STEP 4 - Create an object
STEP 5 - Declare integer variables & array
STEP 6 - Create shift( ) to assign size
STEP 7 - Accept the elements in array from
User
STEP 8 - Use for loop for printing orginal
Matrix
STEP 9 - Check whether if(i!=0) then GOTO
STEP10 else STEP11
STEP10 - mat[i-1][j]=P.mat[i][j]
STEP11 - mat[m-1][j]=P.mat[0][j];
STEP12 - Create main()
STEP13 - Call the function for result
STEP14 - END
SOLUTION:

import java.util.*; //calling java.util package


public class Shift //creating class
{
static Scanner sc=new Scanner(System.in);
//creating object
int mat[][]; //declaring array
int m,n;
Shift(int mm,int nn)
{
m=mm;//assign size
n=nn;
mat=new int[m][n];
}
void input()
{
System.out.println("Enter elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
mat[i][j]=sc.nextInt();//accepting elements
}
}
void display()
{
for(int i=0;i<m;i++)
{
System.out.println();
for(int j=0;j<n;j++)
System.out.print(mat[i][j] +"\t"); //printing
}
}
void cyclic(Shift P)
{
for(int i=0;i<m;i++)
for(int j =0;j<n;j++)
{
if(i!=0)
mat[i-1][j]=P.mat[i][j];
else
mat[m-1][j]=P.mat[0][j];
}
}
static void main() //creating main
{
Shift x=new Shift(3,4);
Shift y=new Shift(3,4);
x.input(); //invoking function

y.cyclic(x);
System.out.println(“original matrix:”);
x.display();
System.out.println(“cyclic shift matrix:”);
y.display();
}
}
Output:
Enter elements
7
8
9
4
5
6
1
2
3
8
5
2
Original matrix:
7 8 9 4
5 6 1 2
3 8 5 2
Cyclic shift matrix:
5 6 1 2
3 8 5 2
7 8 9 4
Program-22
A class ConsChange has been defined with the
following details:
Class name :ConsChange
Data members/instance variables:
word : stores the word
len : stores the length of the word
Member functions/methods:
ConsChange( ) : default constructor
void readword( ) : accepts the word in lowercase
void shiftcons( ) : shifts all the consonants of the word
at the beginning followed by the vowels (e.g. spoon
becomes spnoo)
void changeword( ) : changes the case of all occurring
consonants of the shifted word to uppercase, for e.g.
(spnoo becomes SPNoo)
void show( ): displays the original word, shifted word
and the
changed word
Specify the class ConsChange giving the details of the
constructor( ),
void readword( ), void shiftcons( ), void
changeword( ) and void show( ). Define the main( ).
ALGORITHM:

STEP 1 - START
STEP 2 - Call the java.util package
STEP 3 - Create class name with ConsChange
STEP 4 - Declare required integer & string
variables
STEP 5 - Create object
STEP 6 - Create ConsChange() for default
Constructor
STEP 7 - Accept the word in lower case from
User
STEP 8 - Find out the length of word using
length( )
STEP 9 - Extract the characters form word &
sort them
STEP10 - Store the sorted character in new
string
STEP11 - Create main()
STEP12 - Call all the function for result
STEP13 - END
SOLUTION:

import java.util.Scanner;
public class ConsChange //creating class
{
String word; //declaring variables
int len;
static Scanner sc=new Scanner(System.in);
//creating object
ConsChange()
{
len=0; word= “”;
}
void readword()
{
System.out.println("Enter word in Lower case");
word=sc.next(); //accepting word
len=word.length(); //finding length
}
void shiftcons()
{
String s=""; char c;
for(int i=0;i<len;i++)
{
c=word.charAt(i); //extracting characters
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
s +=c;
}
for(int i=0;i<len;i++)
{
c=word.charAt(i);
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
s +=c;
}
System.out.print("\n Sorted Word="+s);
word=s;
}
void changeword()
{
char c; String s="";
for(int i=0;i<len;i++)
{
c=word.charAt(i); //extracting character
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
s +=(char)(c-32);
else
s +=c;
}
System.out.println("\n Changed word= " + s);
}
void show()
{
System.out.print("\n Original word= " + word);
shiftcons();
changeword();
}
static void main() //creating main( )
{
ConsChange X=new ConsChange(); //creating object
X.readword(); //invoking function
X.show();
}
}
Output:

Enter word in Lower case


computer
Original word= computer
Sorted word= cmptroue
Changed word= CMPTRoue
Program-23
A class Adder has been defined to add any two
accepted time.
Example: Time A - 6 hours 35 minutes
Time B - 7 hours 45 minutes
Their sum is - 14 hours 20 minutes
The details of the members of the class are given below:

Class name :Adder

Data member/instance
variable:
a[ ] : integer array to hold two elements.

Member functions/methods:
Adder( ) : constructor to assign 0 to the array elements
Void readtime( ) : to enter the elements of the array
void addtime( ): adds the time of the two parameterized
objects X and Y and displays (i.e. minutes = )
void distime( ) : to display added time.

Specify constructor( ),readtime( ), addtime( )Define


the main( ) function and call the functions accordingly.
SOLUTION:

import java.util.*; //calling java.util package


public class Adder //creating class
{
int a[]=new int[2]; //declaring array
static Scanner x=new Scanner(System.in);
Adder()
{
a[0]=0;a[1]=0;//assigning 0
}
void readtime()
{
System.out.println("Enter hours and minutes");
a[0]=x.nextInt(); //accepting hours
a[1]=x.nextInt(); //accepting minutes
}
void adddtime(Adder x,Adder y)
{
a[1]=x.a[1]+y.a[1];
a[0]=a[1]/60;
a[1]=a[1]%60;
a[0]=a[0]+x.a[0]+y.a[0];
}
void disptime()
{
System.out.println("Hours=" + a[0]); //printing
System.out.println(“Minutes=" a[1]);
}
static void main() //creating main
{
Adder a =new Adder( );
Adder b =new Adder( );
Adder c =new Adder( );
a.readtime( ); //invoking function
b.readtime( );
c.addtime(a,b);
c.display( );
}
}
Output:
Enter hours and minutes
7
45
Enter hours and minutes
8
55
Hours = 16
Minutes = 40
Program – 24
Write a program to create a Pascal triangle:
Example:
1)
n=3
1
11
121

2)
n=7
1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
ALGORITHM:

STEP 1 - START
STEP 2 - pas[0]=1
STEP 3 - if i=0 then goto step 4
STEP 4 - if j=0 then goto step 5
STEP 5 - print pas[j]+” “
STEP 6 - i++ & if i<n goto step 4
STEP 7 - j=0& if j<=1 goto step 5
STEP 8 - if j=i+1 then goto step 7
STEP 9 - pas[j]=pas[j]+pas[j-1]
STEP10 - j-- & if j>0 goto step 9
STEP11 - END
SOLUTION:

import java.io.*;
class program_24
{
public void main()throws IOException //program_24w()
function
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a no.");
int n=Integer.parseInt(br.readLine()); //accepting value
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++) //loop evaluating the elements
{
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" "); //printing the Triangle
elements
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}
Output:
Enter a no.
7

1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
Program – 25
Write a program to print an Arithmetic Progression and the
sum of progression.

Accept the Starting number, common difference and the


number of terms from the user to print the progression and
the sum.

Example:

Starting point: 12.0

Common difference: 22.0

Number of terms: 25

Progression:

12.0 ,34.0 ,56.0 ,78.0 ,100.0 ,122.0 ,144.0 ,166.0 ,188.0 ,210.0 ,232.0
,254.0 ,276.0 ,298.0 ,320.0 ,342.0 ,364.0 ,386.0 ,408.0 ,430.0 ,452.0
,474.0 ,496.0 ,518.0 ,540.0

Sum: 6900.0
ALGORITHM:

STEP 1 - START
STEP 2 - Declare and initializes required variables
STEP 3 - Read values of m,n as start and end limits
STEP 4 - Start a loop of j from upto n assign 0 to variable
check and repeat step 5-10
STEP 5 - Start a loop of d from 0 to 9 and repeat step
STEP 6 - Assign value of j to num and 0 to variable no. of
digits
STEP 7 - Using suitable loop extract digit from num till
num>0 repeat step 7
STEP 8 - Check if digit and value of d is same then add 1 to
number of digits thereformnum=num/10
STEP 9 - Print the value of count as frequency of unique
STEP 10 - END
SOLUTION:

import java.io.*;
class program_25
{
private double a,d;
program_25() //default constructor
{
a = d = 0;
}
program_25(double a,double d) //parameterized
constructor

{
this.a = a;
this.d = d;
}
double nTHTerm(int n) //final AP term
{
return (a+(n-1)*d);
}
double Sum(int n) //function calculating sum
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n) //displaying AP Series
{
System.out.print("\n\tSeries\n\t");
for(int i=1;i<=n;i++)
{
System.out.print(nTHTerm(i)+" ,");
}
System.out.print("\n\tSum :"+Sum(n));
}
void main()throws IOException //main function
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 1st term"); //accepting 1st
term
a=Integer.parseInt(br.readLine());
System.out.println("Enter Common difference");
d=Integer.parseInt(br.readLine()); //accepting common
difference
System.out.println("Enter no.of terms");
int n=Integer.parseInt(br.readLine()); //accepting no. of
terms
nTHTerm(n);
Sum(n);
showSeries(n);
}
}
Output:
Series
23.0 ,49.0 ,75.0 ,101.0 ,127.0 ,153.0 ,179.0 ,205.0 ,231.0 ,257.0
,283.0 ,309.0 ,335.0 ,361.0 ,387.0 ,413.0 ,439.0 ,465.0 ,491.0 ,517.0
,543.0 ,569.0 ,595.0 ,621.0 ,647.0 ,
Sum :8375.0
Program – 26
Write a program to print a square matrix in spiral form
starting for the center:

NOTE: Enter a number of rows and columns for dimension in


Odd Figure to get the perfect spiral.
Example:

Dimension (n x n) = 7

43 44 45 46 47 48 49

42 21 22 23 24 25 26

41 20 7 8 9 10 27

40 19 6 1 2 11 28

39 18 5 4 3 12 29

38 17 16 15 14 13 30

37 36 35 34 33 32 31
ALGORITHM:

STEP 1 - START
STEP 2 - Create class spiral
STEP 3 - Create main() function
STEP 4 - Create buffered class and input the size of
array[n][n]
STEP 5 - Create buffered class and input the size of
array[n+1][n+1]
STEP 6 - Declare a variable p=1 and another variables sq to
store number of n
STEP 7 - If p<=sq then goto 8 else goto step 9
STEP 8 - Create a inner loop to insert number from east to
west increment the value of p by 1
STEP 9 - END
SOLUTION:

import java.io.*;
class program_26
{
public static void main(String[] args) throws IOException
//main function
{
int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("enter the
dimension of matrix A x A =");
int l = Integer.parseInt(br.readLine()); //accepting
dimension of square spiral matrix
a=new int[l][l];
r=l/2;c=r-1;
if(l%2==0)
{
System.out.println("wrong entry for spiral path");
System.exit(0);
}
/*Calculating and displaying spiral matrix*/
while(p!=(int)Math.pow(l,2))
{
if(co!=0)
re=1;
for(int ri=1;ri<=k1-re;ri++)
{
p++;
c++;
if(c==l)break;
a[r][c]=p;
}
if(c==l)
break;
for(int dw=1;dw<=k1-1;dw++)
{
p++;r++;a[r][c]=p;
}
for(int le=1;le<=k2-1;le++)
{
p++;c--;a[r][c]=p;
}
for(int up=1;up<=k2-1;up++)
{
p++;r--;a[r][c]=p;
}
k1=k1+2;
k2=k2+2;
co++;
}
for(int y=0;y<l;y++) //Displaying matrix
{
for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);
System.out.println();
System.out.println();
}
}
}
Output:
enter the dimension of matrix A x A =
11
111 112 113 114 115 116 117 118 119 120 121

110 73 74 75 76 77 78 79 80 81 82

109 72 43 44 45 46 47 48 49 50 83

108 71 42 21 22 23 24 25 26 51 84

107 70 41 20 7 8 9 10 27 52 85

106 69 40 19 6 1 2 11 28 53 86

105 68 39 18 5 4 3 12 29 54 87

104 67 38 17 16 15 14 13 30 55 88

103 66 37 36 35 34 33 32 31 56 89

102 65 64 63 62 61 60 59 58 57 90

101 100 99 98 97 96 95 94 93 92 91
Program – 27
Write a program to print a Magical square matrix for the
dimension given by the user:

A Magical square is a square grid filled with distinct positive


integers in the range such that each cell contains a different
integer and the sum of the integers in each row, column and
diagonal is equal.

NOTE: Enter a number of rows and columns for dimension in


Odd Figure to get the perfect Magical square.
Example:
Dimension (n x n): 7

30 39 48 1 10 19 28

38 47 7 9 18 27 29

46 6 8 17 26 35 37

5 14 16 25 34 36 45

13 15 24 33 42 44 4

21 23 32 41 43 3 12

22 31 40 49 2 11 20

Sum of any numbers throughout any(same) row or columns:


ALGORITHM:

STEP 1 - START
STEP 2 - Call the java.io package
STEP 3 - Create class name with program_27
STEP 4 - Create main( )
STEP 5 - Input size of magical square in n
STEP 6 - Create a 2d array “a” of dimension n*n
STEP 7 - Initialize digit – 1r-0,c-(n/2)
STEP 8 - Assign a[r][c]=digit
STEP 9 - Increment digit and cby 1 and each decrement r by
1
STEP10 - Increment r by 2
STEP11 - if “u” less than n-1let r=0
STEP12 - If c greater than n-1 let c=0
STEP13 - if a[r][c]is equal to let a[r][c]digit
STEP14 - END
SOLUTION:

import java.io.*; //calling java.io package


class program_27 //creating class
{
public static void main(String args[])throws Exception
//main function
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the dimension of magical
square=");
int n = Integer.parseInt(br.readLine());
//accepting dimensions
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++)//loop for finding magic
square elements
{
r--;
c++;
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]!=0)
{
r=r+2;
c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1;
c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0)
arr[++r][c]=++num;
}
System.out.println();
for(r=0;r<n;r++) //loop displaying magic square
{
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" "); //printing
System.out.println();

}
}
}
Output:
enter the dimension of magical square=
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
Program – 28
Write a program to input a decimal number and convert the
given number into its binary form.

Example 1:

Number: 2653

Binary form: 101001011101

Example 2:

Number: 39645

Binary: 1001101011011101

.
SOLUTION:

import java.io.*; //calling java.io package


class program_28 //creating class
{
int n,i; //declaring variables
int a[] = new int[100]; //declaring array
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public program_28(int nn) //parameterized contructor
{
n=nn;
}
public void dectobin(int no)
//function converting decimal lto binary number
{
int c = 0;
int temp = no;
while(temp != 0)
{
a[c++] = temp % 2;
temp = temp / 2;
}
System.out.println("Binary eq. of "+no+" = ");
for( i = c-1 ; i>=0 ; i--) //Displaying binary number
System.out.print( a[ i ] );
}
public static void main( ) // main function
{
program_28 obj = new program_28(30);
System.out.println(" enter decimal no -");
int no = Integer.parseInt(br.readLine());
obj.dectobin(no); //invoking function
}
}
Output:
enter decimal no -
2569
Binary eq. of 2569 =
101000001001
Program – 29
Write a program to accept the number of Day and the year;
and print the date, month and year falling on that day.

Example 1:

Number: 129 Year: 2011

Date:9th May 2011

Example 2:

Number:300 Year: 2019

Date:27th Oct 2019


SOLUTION:

import java.io.*; //callimg java.io package


class program_29 //creating class
{
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public void calc(int n, int yr) //function to calculate date
{
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = { "Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","D
ec" } ;
if ( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t < n) //loop calculating date
{
t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{
System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{
System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{
System.out.println( d + "rd" + m[s] + " , "+yr);
}
else {System.out.println( d + "th" + m[s] + " , "+yr);
}
}
public static void main( )//main function
{
program_29 obj = new program_29();
System.out.println( "Enter day no = "); //accepting day no.
int n = Integer.parseInt(br.readLine());
System.out.println( "Enter year = "); //accepting year
int yr = Integer.parseInt(br.readLine());
obj.calc(n,yr); //invoking function
}
}
Output:
Enter day no =
199
Enter year =
2019
18thJul , 2019
Program – 30
write a program in java to print the mirror image of a matrix.
Sample input:
1 2 3
4 5 6

Sample output:
3 2 1
6 5 4
ALGORITHM:

Step-1:Start
Step-2:Create class and the main()function.
Step-3:Create Scanner object.
Step-4:Input size m and n
Step-5:Create an array a[] of size m and n.
Step-6:Run a for loop to enter array elements.
Step-7:Run a for loop starting with i=1.
Step-8:Run a for loop starting with j=n-1.
Step-9:Print array element a[i][j].
Step-10:Close class bracket.
Step-11:Stop
SOLUTION:

import java.util.*;
class mirror //creating class
{
public static void main () //creating main
{
Scanner sc=new Scanner (System.in); //creating scanner
object
int m=sc.nextInt();
int n=sc.nextInt();
int a [][]=new int[m][n]; //creating an array
inti,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt(); //entering the elements
}
}
for(i=);i<m;i++)
{
for(j=n-1;j>=0;j--)
{
System.out.print(a[i][j]); //printing the mirror image
}
System.out.println();
}
}
}
Output:

enter the size


3
3
1
2
3
4
5
6
7
8
9
Original matrix
123
456
789
Mirror matrix
321
654
987

You might also like