Tuesday, 12 March 2019

Program for primality test in JAVA

Problem:
=============
Program for primality test in JAVA

What is primality test?

A primality test is an algorithm for determining whether an input number is prime.

Solution:
===========
import java.util.*;
public class PrimalityTest{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int i = scanner.nextInt();
if(i<0){
System.out.println("Please enter valid number ");
}
else {
System.out.println("is " + i + " prime number?" + isPrimeNumber(i));
}
}

public static boolean isPrimeNumber(int i){
boolean flag = false;
int counter = 0;
for(int j = 1; j <= i; j++){
if(i%j == 0 ){
counter++;
}
if(counter > 2){
break;
}
}
if(counter == 2){
flag = true;
}
return flag;
}
}

Second approach:
================

public static boolean isPrime(int N)
{
if (N < 2) return false;
for (int i = 2; i*i <= N; i++)
if (N % i == 0) return false;
return true;
}


Output:
=======

Enter a number
100
is 100 prime number?false

Enter a number
97
is 97 prime number?true

Enter a number
255
is 255 prime number?false

Find the absolute value of a double value using JAVA

Problem:

Find the absolute value of a double value using JAVA

Algorithm:
==========
1. Read the double value from the console.
2. verify the number is negative or not.
3. if it is negative value the apply again negative symbol.
4. if not return the given value as absolute value.

Solution:
============
import java.util.*;
public class AbsoluteDoubleValueFinder{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a double value");
double i = scanner.nextDouble();
if(i<0.0){
System.out.println("Absolute value for the given number is: " + -(i));
}
else {
System.out.println("Absolute value for the given number is: " + i);
}
}

}

Output:
=========

Enter a double value
-2.5
Absolute value for the given number is: 2.5

Enter a double value
2.3
Absolute value for the given number is: 2.3


Enter a double value
0.9

Absolute value for the given number is: 0.9

Monday, 11 March 2019

Find the absolute value of an int value using JAVA

Problem:
==============

Find the absolute value of an int value using JAVA

What is absolute value??

The absolute value of an integer is the numerical value without regard to whether the sign is negative or positive.

Algorithm:
==========
1. Read the integer value from the console.
2. verify the number is negative or not.
3. if it is negative value the apply again negative symbol.
4. if not return the given value as absolute value.

Solution:
========
import java.util.*;
public class AbsoluteValueFinder{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int i = scanner.nextInt();
if(i<0){
System.out.println("Absolute value for the given number is: " + -(i));
}
else {
System.out.println("Absolute value for the given number is: " + i);
}
}

}

Output:
========

Enter a number
12
Absolute value for the given number is: 12


Enter a number
-12
Absolute value for the given number is: 12


Enter a number
-89

Absolute value for the given number is: 89

Sunday, 10 March 2019

matrix-matrix multiplication (square matrices) using Java

Problem:
===========
matrix-matrix multiplication (square matrices)

Algorithm:
===========
1. Initialize the two square matrices using SecureRandom.
2. Declare another square matrices to hold the result.
3. Iterate the two arrays and calculate the product of two array values and assign the result to resultant array.
4. Print the result.

Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class MatrixMultiplicationUtility{
public static void main(String[] args){
int[][] a = new int[3][3];
int[][] b = new int[3][3];
int[][] c = new int[3][3];
initializeArray(a);
System.out.println("Printing array one: ");
printArrayLikeMatrix(a);

initializeArray(b);
System.out.println("Printing array two: ");
printArrayLikeMatrix(b);
multiplyArrays(a, b, c);

System.out.println("Printing result of matrix multiplication ");
printArrayLikeMatrix(c);
}

public static void initializeArray(int[][] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] =  random.nextInt(10);
}
}
}

public static void printArrayLikeMatrix(int[][] array){
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}

public static void multiplyArrays(int[][] arrayOne, int[][] arrayTwo, int[][] arrayThree){
for(int i=0;i < arrayOne.length; i++){
for(int j = 0; j < arrayTwo.length; j++){
arrayThree[i][j] = arrayOne[i][j] * arrayTwo[i][j];
}
}
}
}


Output:
=======
Printing array one:
5 8 4
2 2 4
6 5 6
Printing array two:
8 1 6
2 0 8
7 5 5
Printing result of matrix multiplication
40 8 24
4 0 32
42 25 30

Reverse the elements within an array using JAVA

Problem:
=======
Reverse the elements within an array using JAVA

Algorithm:
==========
1. Initialize the array with random values.
2. Declare a variable called temp  and initialize the value with 0.
3. Iterate the array and swap the first element of the array and last element of the array.
4. Print out the result

Solution:
=========

import java.util.*;
import java.security.SecureRandom;
class ReverseTheArrayElementsUtility{
public static void main(String[] args){
int[] a= new int[3];
initializeArray(a);
System.out.println("Printing initial array values");
printArray(a);
reverseArrayValues(a);
System.out.println("Printing after reversal of an array");
printArray(a);
}

public static void initializeArray(int[] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
array[i] =  random.nextInt(100);
}
}

public static void printArray(int[] array){
Arrays.stream(array).forEach(System.out::println);
}

public static void reverseArrayValues(int[] array){
int temp = 0;
for(int i=1; i < array.length-1; i++){
temp = array[i-1];
array[i-1] = array[array.length - i];
array[array.length-i] = temp;
}
}
}

Output:
==========

Printing initial array values
81
59
96
Printing after reversal of an array
96
59
81

Printing initial array values
95
47
89
Printing after reversal of an array
89
47
95


Copy data from one array to another array using JAVA

Problem:
=======
Copy data from one array to another array using JAVA

Algorithm:
===========
1. Initialize the array with random values.
2. Create an another array with the size of previous array or source array.
3. Iterate the source array and assign the value of source array to destination array.
4. Print out both source array and destination array.

Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class CopyArrayUtility{
public static void main(String[] args){
int[] a= new int[3];
int[] b= new int[a.length];
initializeArray(a);
System.out.println("Printing source array");
printArray(a);
copyArray(a, b);
System.out.println("Printing destination/copeid array");
printArray(b);
}

public static void initializeArray(int[] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
array[i] =  random.nextInt(100);
}
}

public static void printArray(int[] array){
Arrays.stream(array).forEach(System.out::println);
}

public static void copyArray(int[] sourceArray, int[] destinationArray){

for(int i=0; i < sourceArray.length; i++){
destinationArray[i] = sourceArray[i];
}
}
}

output:
==========

Printing source array
46
4
56
Printing destination/copied array
46
4
56




Friday, 8 March 2019

Compute the average of the given array values in JAVA

Problem:

Compute the average of the given array values in JAVA

Algorithm:

1. Initialize the array with random values.
2. Declare a variable called sum and initialize the value with 0.
3. Iterate the array and calculate the sum.
4.  Divide the sum with the array length
4. Print out the array and average value of the array

Solution:

import java.util.*;
import java.security.SecureRandom;
class FindAverageOfArrayUtility{
public static void main(String[] args){
int[] a= new int[3];
initializeArray(a);
printArray(a);
System.out.println("Average of given array values is : " + findAverageValue(a));
}

public static void initializeArray(int[] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
array[i] =  random.nextInt(100);
}
}

public static void printArray(int[] array){
Arrays.stream(array).forEach(System.out::println);
}

public static double findAverageValue(int[] array){
int sum = 0;
for(int i=0; i < array.length; i++){
sum = sum + array[i];
}
return sum/array.length;
}
}

Output:

24
80
45

Average of given array values is : 49.0

38
75
89

Average of given array values is : 67.0


Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...