0% found this document useful (0 votes)
125 views24 pages

Name: Aditya Krishna Das Class: XI (B) Roll No: 2

The document describes a program to sort the non-boundary elements of a square matrix in ascending order. It includes: 1) Taking user input for the matrix size and elements 2) Storing the non-boundary elements in a separate array 3) Sorting the separate array using a standard sorting technique 4) Rearranging the sorted elements back into the original matrix

Uploaded by

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

Name: Aditya Krishna Das Class: XI (B) Roll No: 2

The document describes a program to sort the non-boundary elements of a square matrix in ascending order. It includes: 1) Taking user input for the matrix size and elements 2) Storing the non-boundary elements in a separate array 3) Sorting the separate array using a standard sorting technique 4) Rearranging the sorted elements back into the original matrix

Uploaded by

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

Name: Aditya Krishna Das

Class : XI (B)
Roll No: 2
Q1 ) Write a program to check whether a number is Disarium or not:

Algorithm:
Step 1:Start

Step 2:Create a function which returns integer type and pass formal parameters a and p.

Step 3:If a==0, return 0

Step 4:Else returnthe sum of the position powered integers.

Step 5:inside the main method create an object ob.

Step 6: Take n which stores the user input of the number.

Step 7:Take c=0 and k=n

Step 8:While n is not equal to 0, store n/10 and increment c by 1

Step 9:Take s which stores the returned value of the function after it calls k and c to the function.

Step 10:If s=k, print k is a disarium number.

Step 11: If step 10 is false , print k is anot a dsiarium number.

Step 12:Stop

Source Code:

// Java program to check whether a number is Disarium

// or not
class Disarium_test

// Method to check whether a number is disarium or not

static boolean check(int n)

// Count digits in n.

int count_digits = Integer.toString(n).length();

// Compute sum of terms like digit multiplied by

// power of position

int sum = 0; // Initialize sum of terms

int x = n;

while (x!=0)

// Get the rightmost digit

int r = x%10;

// Sum the digits by powering according to

// the positions

sum = (int) (sum + Math.pow(r, count_digits--));

x = x/10;

// If sum is same as number, then number is

return (sum == n);

}
// Driver method

public static void main()

Scanner in=new Scanner(System.in);

Int n=in.nextInt();

System.out.println(check(n) ? "Disarium Number" : "Not a Disarium Number");

Variable Description :
Serial no Variable name Data type Description
1. N Int Stores the integer as
input.
2. countdigits Int To count the digits.
3. sum Int To store the sum of
digits of the number.
4. R Int To store the last digit of
the remaining number.

Screenshot:

Q2 ) Write a program to

i) Create a file
ii) Write a file
iii) Read a file

i) Create a file

import java.io.File; // Import the File class

import java.io.IOException; // Import the IOException class to handle errors

public class CreateFile {

public static void main(String[] args) {

try {

File myObj = new File("filename.txt");

if (myObj.createNewFile()) {

System.out.println("File created: " + myObj.getName());

} else {

System.out.println("File already exists.");

} catch (IOException e) {

System.out.println("An error occurred.");

e.printStackTrace();

ii) Write to a file

import java.io.FileWriter; // Import the FileWriter class

import java.io.IOException; // Import the IOException class to handle errors

public class WriteToFile {


public static void main(String[] args) {

try {

FileWriter myWriter = new FileWriter("filename.txt");

myWriter.write("Files in Java might be tricky, but it is fun enough!");

myWriter.close();

System.out.println("Successfully wrote to the file.");

} catch (IOException e) {

System.out.println("An error occurred.");

iii) Read a file

import java.io.File; // Import the File class

import java.io.FileNotFoundException; // Import this class to handle errors

import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {

public static void main(String[] args) {

try {

File myObj = new File("filename.txt");

Scanner myReader = new Scanner(myObj);

while (myReader.hasNextLine()) {

String data = myReader.nextLine();

System.out.println(data);

myReader.close();
} catch (FileNotFoundException e) {

System.out.println("An error occurred.");

Q3 ) Write a program to show String Tokenizer class

/* A Java program to illustrate working of StringTokenizer */

import java.util.*;

public class NewClass

public static void main(String args[])

System.out.println("Using Constructor 1 - ");

StringTokenizer st1 =

new StringTokenizer("Hello Geeks How are you", " ");

while (st1.hasMoreTokens())

System.out.println(st1.nextToken());

System.out.println("Using Constructor 2 - ");

StringTokenizer st2 =

new StringTokenizer("JAVA : Code : String", " :");

while (st2.hasMoreTokens())

System.out.println(st2.nextToken());

System.out.println("Using Constructor 3 - ");

StringTokenizer st3 =
new StringTokenizer("JAVA : Code : String", " :", true);

while (st3.hasMoreTokens())

System.out.println(st3.nextToken());

Q4) Write a program to declare a square matrix A[][] of order (M x 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:
Sort the non-boundary elements in ascending order using any standard sorting technique and re-
arrange them in the matrix.

Algorithm:
Step 1:Start

Step 2:Take nwhich stores the size of the square matrix.

Step 3:If n>=3 and n<=10 take k which is the square of (n-2).

Step 4:Take c=k nad temp.

Step 5:Declare a[] and size k.

Step 6:Declare square matrix A[][] of size n*n

Step 7:Take i=0 and j=0

Step 8:Take the input of A[][].

Step 9:Increment I and j by 1

Sterp 10:Repeat the loop till i<n nad j<n

Step 11:Take i=0 and j=0

Step 12:Print A[i][j] with a space after it.

Step 13:Increment I and j and repeat step 12 and 13 till j<n.

Step 14:Use print statement to go the next line.

Step 15:Increment i by 1 and jrepeat steps 11-14 till j<n.

Step 16:Take i=1 and j=1


Step 17:Decrement k by 1 and assign A[i][j] to a[k]

Step 18:Increment I and j by1and repeat the loops till i<n and j<n

Step 19:Take y=0 and z=0

Step 20:If a[z]>a[z+1] then assign a[z] to temp.

Step 21:Assign a[z+1] to a[z] and temp to a[z+1]

Step 22:Increment y and z by 1 and repeat step 20-22 till y<c and z<c-1

Step 23:take x=0

Step 24:Take i=1 and j=1 and a[x] to A[i][j]

Step 25:Increment x,I and j by 1 and repeat step 25 till I<n-1 and j<n-1

Step 26:Repeat steps 11-15

Step 27:If step 3 is false then print that the input is invalid.

Step 28:Stop

Source Code:
import java.util.*;

class SortNonBoundary

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();

/* The below function stores Non-Boundary elements

* from array A[][] to array B[] if s = 1

* else stores the Non-Boundary elements in array A[][] from array B[]
*/

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();

public static void main(String args[])

{
SortNonBoundary ob = new SortNonBoundary();

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

Variable Description:
Serial no. Variable name Data type Description
1. M Int Stores one of the
dimension of the
array.
2. N Int Stores one of the
dimension of the
array.
3. I Int Used as a variable in
for loop.
4. J Int Used as a variable in
for loop.
Screenshot:

Q5) A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the
leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is
still prime. The process is repeated until the original number is reached again.
A number is said to be prime if it has only two factors I and itself.

Algorithm:
Step 1:Start

Step 2: Set c=0

Step 3: If n%i==0 then increment c by 1.

Step 4: Set String s = Integer.toString(n)


Step 5:Set String p = s.substring(1)+s.charAt(0)

Step 6: Set int a = Integer.parseInt(p)

Step 7:Return a

Step 8: Check if a is prime and if it is not then increment f by 1 and break.

Step 9:Create object ob of the class.

Step 10:Set n as the value which is given as input by the user.

Step 11: Stop

Source Code:
import java.util.*;

class CircularPrime

boolean isPrime(int n) // Function for checking whether a number is prime or not

int c = 0;

for(int i = 1; i<=n; i++)

if(n%i == 0)

c++;

if(c == 2)

return true;

else

return false;

}
int circulate(int n) //Function for circulating the digits to form new number

String s = Integer.toString(n);

String p = s.substring(1)+s.charAt(0);

int a = Integer.parseInt(p);

return a;

void isCircularPrime(int n) //Function to check for circular prime

int f = 0,a = n;

do

System.out.println(a);

if(isPrime(a)==false)

f = 1;

break;

a = circulate(a);

}while(a!=n);

if(f==1)

System.out.println(n+" IS NOT A CIRCULAR PRIME");

else

System.out.println(n+" IS A CIRCULAR PRIME");


}

public static void main(String args[])

CircularPrime ob = new CircularPrime ();

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number : ");

int n = sc.nextInt();

ob.isCircularPrime(n);

Variable Description:
Serial no. Variable name Data type Description
1. c Int Used as a counter.
2. n Int Used to store the
number as input by the
user.
3. i Int Used as a variable in for
loop.
4. s String Used to store the
number as a string.
5. p String Used to store the
permutated string as an
integer.
6. a Int Used to store p as an
integer.
7. f Int Used as a counter.

Screenshot:
Q6) A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.

Note: All even integer numbers greater than 4 are Goldbach numbers. Example: 6 = 3 + 3
10 = 3 + 7
10 = 5 + 5

Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7, 5 and 5.

Write a program to accept an even integer ‘N’. Find all the odd prime pairs whose sum is equal to the
number ‘N’.

Algorithm:
Step 1:Start

Step 2: Set c=0

Step 3: If n%i==0 then increment c by 1.

Step 4: Store in n the number given as input by the user.

Step 5: Run a loop from j=2 till j<=n/2 and if obj.prime(j)==1&&obj.prime(n-j)==1 ,then print the
required pair of integers.

Step 6:Stop

Source Code:
// Java program to implement Goldbach's conjecture

import java.util.*;
class Goldbach

int prime(int x)

int c=0;

for(int i=2;i<=(x/2);i++)

if(x%i==0)

c++;

if(c==0)

return 1;

else

return 0;

public static void main()

Scanner in=new Scanner(System.in);

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

int n=in.nextInt();

Goldbach obj=new Goldbach();

for(int j=2;j<=(n/2);j++)

if(obj.prime(j)==1&&obj.prime(n-j)==1)

System.out.println(n+"=" + (j) + "+" +(n-j));

}
}

Variable Description:
Serial no. Variable name Data type Description
1. I Int Used as a variable in for
loop.
2. C Int Used as a counter in a
for loop.
3. N Int Stores the number given
as input by the user.
4. J Int Used as a variable in for
loop.

Screenshot:

Q 7) Write a program to input a string and print the words in ascending order of sum of their ASCII
value:

For Example: “ HOW ARE YOU”

H+O+W = 73+80+88 = 241

A+R+E =66+83+70 = 219

Y+O+U =90+80+86 = 256

Output:

YOU

HOW

ARE

Algorithm:
Step 1:Start

Step 2:Run the loop till i<l

Step 3:Set ascii_sum =ascii_sum+(int)(charAt(i))

Step 4:Return ascii_sum.

Step 5:Store the ascii_sum of each word as elements of an array.

Step 6:Sort the array using bubble sort technique.

Step 7:Display the required array.

Step 8:Stop

Source Code:
import java.io.*;

import java.util.*;

import java.util.Arrays;

public class string_ascii_sort

static int return_ascii_sum(String s)

int ascii_sum=0;

for (int i=0;i<s.length();i++) {

ascii_sum+=(int)(s.charAt(i));

return ascii_sum;

}
public static void main(String args[])

Scanner sc = new Scanner(System.in);

String input;

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

input=sc.nextLine();

String[] input_array=input.split(" ");

int[] ascii_value=new int[input_array.length];

int counter=0;

for (int i=0;i<input_array.length;i++) {

ascii_value[counter]=return_ascii_sum(input_array[i]);

counter++;

for (int i = 0; i < ascii_value.length; i++)

for (int j = i + 1; j < ascii_value.length; j++)

if (ascii_value[i] > ascii_value[j])

{
int temp = ascii_value[i];

ascii_value[i] = ascii_value[j];

ascii_value[j] = temp;

String str_temp=input_array[i];

input_array[i]=input_array[j];

input_array[j]=str_temp;

for (int i=0;i<input_array.length;i++) {

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

Variable Description:
Serial no. Variable name Data type Description
1. I Int Used as a variable in for
loop.
2. Ascii_sum Int Used to store the sum of
the ascii values of the
characters.
3. J Int Used as a variable in the
for loop.

Screenshot:

You might also like