# Manuals/calci/Examples

Examples of Java and z3 Programs

## DESCRIPTION

• Synchronous Readline for interactively running to have a conversation with the user via a console.
• Original codes in java as examples.
• z3 codes with z3 notations are shown below the java code.

## Examples

ExampleR1: Check if number is Odd/Even

• Program to check if the number is Odd/Even.
• z3 notations for if-else loop is used.

Java Code:
```import java.util.Scanner;
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
//The input provided by user is stored in num
Scanner input = new Scanner(System.in);
num = input.nextInt();
// If number is divisible by 2 then it's an even number
// else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
```

z3 code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var temp;
var num = readlineSync.question("Enter any number: ");
( num % 2 == 0 )::
{console.log(num + " is an even number")},
{console.log(num + " is an odd number")};
```

ExampleR2: Sum of a given Array

• Program to calculate the sum of a given array.
• z3 function for SUM is used.

Java Code:
```import java.util.Scanner;
class SumDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int[] array = new int;
int sum = 0;
System.out.println("Enter the elements:");
for (int i=0; i<10; i++)
{
array[i] = scanner.nextInt();
}
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var k;
k = readlineSync.question("Number of elements: ");
var numbers = new Array(k);
//Enter numbers using comma
var num = readlineSync.question("Enter the elements: ");
var temp = num.split(",");
for (var i=0; i<k; i++)
{
numbers[i] = temp[i];
}
var sumofarray = SUM(numbers);
console.log("Sum:" +sumofarray);
```

ExampleR3: Linear Search

• Program to search the location of a given number in an array.
• z3 notations for if-else used.

Java Code:
```import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int counter, num, item, array[];
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
//Creating array to store the all the numbers
array = new int[num];
System.out.println("Enter " + num + " integers");
//Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();

System.out.println("Enter the search value:");
item = input.nextInt();

for (counter = 0; counter < num; counter++)
{
if (array[counter] == item)
{
System.out.println(item+" is present at location "+(counter+1));
/*Item is found so to stop the search and to come out of the
* loop use break statement.*/
break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var k;
k = readlineSync.question("Number of elements: ");
var numbers = new Array(k);
//Enter numbers using comma
var num = readlineSync.question("Enter the elements: ");
var temp = num.split(",");
var item = readlineSync.question("Enter the search value: ");

for (i = 0; i < k; i++)
{
(temp[i]-item== 0)::
{
console.log(item+" is present at location "+(i+1));
break;
};
}
(i-k ==0)::
{console.log(item + " doesn't exist in array.")};
```

ExampleR4: Floyd's Triangle

• Printing the Floyd's triangle for a given number of rows.
• z3 function FLOYDSTRIANGLE is used.

Java Code:
```import java.util.Scanner;
class FloydTriangleExample
{
public static void main(String args[])
{
int rows, number = 1, counter, j;
//To get the user's input
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows for floyd's triangle:");
//Copying user input into an integer variable named rows
rows = input.nextInt();
System.out.println("Floyd's triangle");
System.out.println("****************");
for ( counter = 1 ; counter <= rows ; counter++ )
{
for ( j = 1 ; j <= counter ; j++ )
{
System.out.print(number+" ");
//Incrementing the number value
number++;
}
//For new line
System.out.println();
}
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var rows;
var	tri;

rows = readlineSync.question("Enter the number of rows for floyd's triangle: ");
console.log("Floyd's triangle");
console.log("****************");
tri = FLOYDSTRIANGLE(rows)
console.log(tri);
```

ExampleR5: Reverse Number

• Program to find the reverse of a number.
• For floats you do not do a comparison with zero. It should be absolute value to be less than a small number.
• z3 functions FLOOR and SIGN are used.

Java Code:
```import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}

System.out.println("Reverse of input number is: "+reversenum);
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var num = 0;
var reversenum = 0;
var temp = num;
while( abs(num) > 0.00001 )
{
reversenum = reversenum * 10;
reversenum = reversenum + abs(num%10);
num = FLOOR(abs(num/10));
}
console.log(SIGN(temp)*reversenum);
```

ExampleR6: Calculate the Factorial of a number

• Program to calculate the factorial of a user input number.
• In the code num is multiplied by 1, to convert it from a string to number.
• z3 funtion FACTORIAL is uded.

Java Code:
```import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is: "+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var num;
var fact;
num = readlineSync.question("Enter the number: ");
console.log(typeof num)
fact = FACTORIAL(num*1);
console.log(num,fact);
```

ExampleR7: Area of Rectangle

• Program to find the area of rectangle.

Java Code:
```import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var length;
var width;
var area;
length = readlineSync.question("Enter the length of Rectangle:");
width = readlineSync.question("Enter the width of Rectangle:");
//Area = length*width;
area = length*width;
console.log("Area of Rectangle is:"+area);
```

ExampleR8: Display Prime Numbers

• Program to print all the prime numbers till n.
• z3 function PRIMES is used.

Java Code:
```import java.util.Scanner;
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
scanner.close();
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
}
}
System.out.println("Prime numbers from 1 to n are :");
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var n;
n = readlineSync.question("Enter the value of n: ");
console.log("Prime numbers from 1 to n are : "+ primenumbers);
```

ExampleR9: Ascending Order

• Program to find ascending order of the input numbers.
• z3 function ASCENDING is used.

Java Code:
```import java.util.Scanner;

class BubbleSortExample {
public static void main(String []args) {
int num, i, j, temp;
Scanner input = new Scanner(System.in);

System.out.println("Enter the number of integers to sort:");
num = input.nextInt();

int array[] = new int[num];

System.out.println("Enter " + num + " integers: ");

for (i = 0; i < num; i++)
array[i] = input.nextInt();

for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

System.out.println("Sorted list of integers:");

for (i = 0; i < num; i++)
System.out.println(array[i]);
}
}
```

z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var num;
var k;
var temp1;
num = readlineSync.question("Enter the number of integers to sort: ");
var array = new Array(num);
k = readlineSync.question("Enter " + num + " integers: ");
temp1 = k.split(",");
var increase = ASCENDING(temp1);
console.log("Sorted list of integers: " +increase);
```

ExampleR10: Inputting Matrix and Calculating it's Inverse

• Program to input mxn matrix.
• Finding Inverse of the matrix using z3 function MATRIXINVERSE.
• we have used regexp to handle both space tab and ,
• [\,\s] means any set of characters including , and space characters.
• + means more than one space assumed as 1. And regexp should be within / and /.
• .\$(NUM) is used to convert all numbers in array which are strings to numbers.

Java Code:
z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");
var numbers = new Array();
var i=0;
do
{
rowstring = readlineSync.question("Enter elements of matrix for row " + i + " (comma separated): ");
if(rowstring=="")
{
break;
}
else
{
rowstring.split(/[\,\s]+/);
numbers.push(rowstring.split(/[\,\s]+/));
i++
}
}while(true)
var inverse = MATRIXINVERSE(numbers.\$(NUM));
console.log(inverse);
```

ExampleR11: Inputting 2 Matrices and Calculating Sum,Difference and Product

• Program to input 'k', mxn matrices.
• Note: Enter only 2 matrices for calculation purposes.
• Finding Sum,Difference and Product using z3 functions.
• Functions are MATRIXADD,MATRIXSUBTRACT, MATRIXMULTIPLY respectively.

Java Code:
z3 Code:
```var readlineSync = require('readline-sync');
console.log("Starting");

var rows;
var columns;
var array1;
var temp1;
var temp;
var m = 0;
var numbers2 = new Array();
do
{
if(rowstring=="")
{
break;
}
else
{
var k = 0;
var numbers = new Array(rows);
rows = readlineSync.question("Enter number of rows of matrix: ");
columns = readlineSync.question("Enter number of columns of matrix: ")

//Enter all elements row-wise with comma(like 1,2,3)
array1 = readlineSync.question("Enter the elements of matrix: ");
temp1 = array1.split(/[\,\s]+/);
temp = temp1.\$(NUM);

for (var i=0; i<rows; i++){
var numbers1 = new Array(columns);
for (var j = 0; j<columns; j++){
numbers1[j] = temp[k];
k++;
}
numbers[i] = numbers1;
}
numbers2.push(numbers.\$(NUM));
m++;
}
}while(true)

for (var i = 0;i<1;i++){
//Compute for multiply,add,divide for any 2 given matrices