Computer Journal Programs
Computer Journal Programs
Q1)
import java.util.*;
class Matrix
{
// sorting non boundary elements problem
public static int[] bubbleSort( int a[] )
{
int temp;
for(int i=0; i < a.length-1;i++){
for(int x=0;x < a.length-i-1;x++)
{
if(a[x+1] < a[x]){
temp=a[x];
a[x] = a[x+1];
a[x+1] = temp;
}
}
}
return a;
}
}
}
System.out.println( "ORIGINAL MATRIX : " );
displayMatrix( A );
//sort the array n
n = bubbleSort( n );
//display(n);
i=0;
for( int row = 1 ; row < M - 1 ; row++ )
{
for( int col = 1 ; col < M - 1 ; col++ )
{
A[ row ][ col ] = n[ i++ ];
}
}
Q2)
import java.util.Scanner;
public class GoldbachNumber
{
public static void main(String args[])
{
//Taking the number as input from the user using
scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();
int temp;
boolean flag = false;
// Checks if the number is above 4 then goes into the
loop
if(num>4)
{
// Runs a loop from 3 to the num until the break
condition is met
for(int i = 3; i<num; i+=2)
{
// Checks whether the current number is prime else
goes out
if(isPrime(i))
{
// Finds the other number and checks if it is prime
number
temp = num - i;
if(isPrime(temp))
{
flag = true;
break;
}
}
}
}
if(flag)
{
System.out.println(num+" is a Goldbach number");
}
else
{
System.out.println(num+" is Not a Goldbach
number");
}
}
// Function to check for prime
static boolean isPrime(int num)
{
int iter = 2;
boolean flag = true;
while (num > iter)
{
if (num % iter == 0)
{
flag = false;
break;
}
iter++;
}
return flag;
}
}
Q3)
import java.util.Scanner;
public class cellularphone
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Phone number: ");
String phone = sc.next();
//Regular expression to accept valid phone number
String regex = "\\d{10}";
//Matching the given phone number with regular
expression
boolean result = phone.matches(regex);
if(result)
{
System.out.println("Given phone number is valid");
} else
{
System.out.println("Given phone number is not valid");
}
}
}
Q4)
public class RomanNumberToDecimal
{
public static int toDecimal(String str) {
int len = str.length();
/**
* adding an random char just to be used by the next
char to eliminated
* unnecessary out of index checks
*/
str = str + " ";
int result = 0;
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
/** the next char is needed in case the number is
using subtractive pattern */
char nextChar = str.charAt(i + 1);
Q6)_
import java.util.Scanner;
public class pseudoArithmeticOperation
{
Q7)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ReadTextFileWriteToAnother
{
try {
//Files objects
File inputFile = new File("D:\\examples\\input.txt");
File outFile = new File("D:\\examples\\out.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Q8)
import java.util.Scanner;
public class CircularPrime
{
public static boolean isPrime(int num)
{
int c = 0;
return c == 2;
}
return c;
}
if (n <= 0)
{
System.out.println("INVALID INPUT");
return;
}
boolean isCircularPrime = true;
if (isPrime(n))
{
System.out.println(n);
int digitCount = getDigitCount(n);
int divisor = (int)(Math.pow(10, digitCount - 1));
int n2 = n;
for (int i = 1; i < digitCount; i++)
{
int t1 = n2 / divisor;
int t2 = n2 % divisor;
n2 = t2 * 10 + t1;
System.out.println(n2);
if (!isPrime(n2))
{
isCircularPrime = false;
break;
}
}
}
else
{
isCircularPrime = false;
}
if (isCircularPrime)
{
System.out.println(n + " IS A CIRCULAR PRIME.");
}
else
{
System.out.println(n + " IS NOT A CIRCULAR PRIME.");
}
}
}
Q9)
class Merge2SortedArrays
{
public static void main(String[] args)
{
int arr1[]={4,6, 9, 20, 56};
int arr2[]={1, 7, 25, 45, 70};
int[] result=merge(arr1, arr2);
for (int j=0; j<result.length;j++)
System.out.print(result[j]+ " ");
}
Q10)
import java.io.*;
class Pendulum_Array
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\nEnter number of elements: ");
// Inputting the number of elements
int n = Integer.parseInt(br.readLine());
int A[]=new int[n]; //original array
int B[]=new int[n]; //array for storing the result
/*Inputting the Array*/
for(int i=0; i<n; i++)
{
System.out.print("Enter Element "+(i+1)+": ");
A[i] = Integer.parseInt(br.readLine());
}
/*Sorting the Inputted Array in Ascending Order*/
int t=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
}
/*Printing the Sorted Array*/
System.out.println("\nThe Sorted Array Is");
for(int i=0; i<n; i++)
{
System.out.print(A[i]+" ");
}
int mid = (n-1)/2;
//finding index of middle cell
int x = 1, lim = n-1-mid;
/*'x' is for accessing elements of array A[] and 'lim' is
* for the number of times we have to make this to-and-
fro movement*/
/* Pendulum Arrangement Starts Here */
B[mid]=A[0]; //putting the minimum element in the
middle cell
for(int i=1; i<=lim; i++)
{
if((mid+i)<n) //going to the right side
{
B[mid+i]=A[x++];
}
if((mid-i)>=0) //going to the left side
{
B[mid-i]=A[x++];
}
}
/*Printing the Result*/
System.out.println("\n\nThe Result Is");
for(int i=0; i<n; i++)
{
System.out.print(B[i]+" ");
}
}
}