Difference between revisions of "Manuals/calci/Examples"

 
(78 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
*z3 codes with z3 notations are shown below the java code.<br><br>
 
*z3 codes with z3 notations are shown below the java code.<br><br>
  
==EXAMPLES==
+
==Examples==
*[[#1.Check Odd/Even|Check Odd/Even]]
+
'''ExampleR1: Check if number is Odd/Even<br>'''
*[[#3.Linear Search|Linear Search]]
+
* Program to check if the number is Odd/Even.
 +
* z3 notations for if-else loop is used.<br><br>
  
==Codes for Example==
+
<div id="z3codes1a" style="font-size:16px">'''Java Code:'''</div>
'''1.''' '''Check Odd/Even<br>'''
 
'''Java Code:'''<br>
 
 
<span class="plainlinks">[https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number/ https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number]</span>
 
<span class="plainlinks">[https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number/ https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number]</span>
  
Line 32: Line 31:
 
         System.out.println("Entered number is odd");
 
         System.out.println("Entered number is odd");
 
   }
 
   }
}</source>
+
}</source><br>
  
 
+
<div id="z3codes1b" style="font-size:16px">'''z3 code:'''</div>
'''z3 code:'''<br>
 
 
<source lang="cpp">
 
<source lang="cpp">
 
var readlineSync = require('readline-sync');
 
var readlineSync = require('readline-sync');
Line 46: Line 44:
  
  
'''2. Sum of Array'''<br>
+
 
'''Java Code:'''<br>
+
 
 +
'''ExampleR2: Sum of a given Array'''<br>
 +
* Program to calculate the sum of a given array.
 +
* z3 function for SUM is used.<br><br>
 +
 
 +
<div id="z3codes2a" style="font-size:16px">'''Java Code:'''</div>
 
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-sum-the-elements-of-an-array/ https://beginnersbook.com/2014/01/java-program-to-sum-the-elements-of-an-array]</span>
 
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-sum-the-elements-of-an-array/ https://beginnersbook.com/2014/01/java-program-to-sum-the-elements-of-an-array]</span>
 
<source lang="cpp">
 
<source lang="cpp">
Line 66: Line 69:
 
       System.out.println("Sum of array elements is:"+sum);
 
       System.out.println("Sum of array elements is:"+sum);
 
   }
 
   }
}</source>
+
}</source><br>
  
 
+
<div id="z3codes2b" style="font-size:16px">'''z3 Code:'''</div>
'''z3 code:'''<br>
 
 
<source lang="cpp">
 
<source lang="cpp">
 
var readlineSync = require('readline-sync');
 
var readlineSync = require('readline-sync');
Line 79: Line 81:
 
       var num = readlineSync.question("Enter the elements: ");
 
       var num = readlineSync.question("Enter the elements: ");
 
       var temp = num.split(",");
 
       var temp = num.split(",");
        for (var i=0; i<k; i++)
+
      for (var i=0; i<k; i++)
        {
+
      {
      numbers[i] = temp[i];
+
    numbers[i] = temp[i];
        }
+
      }
 
       var sumofarray = SUM(numbers);
 
       var sumofarray = SUM(numbers);
 
       console.log("Sum:" +sumofarray);</source>
 
       console.log("Sum:" +sumofarray);</source>
  
  
<a id=”Linear Search”name=”Linear Search”></a>
+
 
'''3. Linear Search'''<br>
+
 
'''Java Code:'''<br>
+
'''ExampleR3: Linear Search'''<br>
 +
* Program to search the location of a given number in an array.
 +
* z3 notations for if-else used.<br><br>
 +
 
 +
<div id="z3codes3a" style="font-size:16px">'''Java Code:'''</div>
 
<span class="plainlinks">[https://beginnersbook.com/2014/04/java-program-for-linear-search-example/ https://beginnersbook.com/2014/04/java-program-for-linear-search-example]</span>
 
<span class="plainlinks">[https://beginnersbook.com/2014/04/java-program-for-linear-search-example/ https://beginnersbook.com/2014/04/java-program-for-linear-search-example]</span>
 
<source lang="cpp">
 
<source lang="cpp">
Line 125: Line 131:
 
         System.out.println(item + " doesn't exist in array.");
 
         System.out.println(item + " doesn't exist in array.");
 
       }
 
       }
}</source>
+
}</source><br>
 
 
  
'''z3 code:'''<br>
+
<div id="z3codes3b" style="font-size:16px">'''z3 Code:'''</div>
 
<source lang="cpp">
 
<source lang="cpp">
 
var readlineSync = require('readline-sync');
 
var readlineSync = require('readline-sync');
 
console.log("Starting");   
 
console.log("Starting");   
 
     var k;
 
     var k;
k = readlineSync.question("Number of elements: ");
+
    k = readlineSync.question("Number of elements: ");
 
     var numbers = new Array(k);
 
     var numbers = new Array(k);
//Enter numbers using comma
+
    //Enter numbers using comma
 
     var num = readlineSync.question("Enter the elements: ");
 
     var num = readlineSync.question("Enter the elements: ");
var temp = num.split(",");
+
    var temp = num.split(",");
var item = readlineSync.question("Enter the search value: ");
+
    var item = readlineSync.question("Enter the search value: ");
 
   
 
   
for (i = 0; i < k; i++)
+
    for (i = 0; i < k; i++)
 
     {
 
     {
 
         (temp[i]-item== 0)::  
 
         (temp[i]-item== 0)::  
Line 148: Line 153:
 
         };
 
         };
 
     }
 
     }
        (i-k ==0)::
+
    (i-k ==0)::
        {console.log(item + " doesn't exist in array.")};</source>
+
    {console.log(item + " doesn't exist in array.")};</source>
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR4: Floyd's Triangle'''<br>
 +
* Printing the Floyd's triangle for a given number of rows.
 +
* z3 function FLOYDSTRIANGLE is used.<br><br>
 +
 
 +
<div id="z3codes4a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/04/java-program-to-print-floyds-triangle-example/ https://beginnersbook.com/2014/04/java-program-to-print-floyds-triangle-example]</span>
 +
<source lang="cpp">
 +
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();
 +
      }
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes4b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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);</source>
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR5: Reverse Number<br>'''
 +
* 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.<br><br>
 +
 
 +
<div id="z3codes5a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-reverse-a-number/ https://beginnersbook.com/2014/01/java-program-to-reverse-a-number]</span>
 +
 
 +
<source lang="cpp">
 +
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);
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes5b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
var readlineSync = require('readline-sync');
 +
console.log("Starting");
 +
    var num = 0;
 +
    var reversenum = 0;
 +
    var num = readlineSync.question("Input your number and press enter: ");
 +
    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);</source>
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR6: Calculate the Factorial of a number<br>'''
 +
* 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.<br><br>
 +
 
 +
<div id="z3codes6a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-find-factorial-of-a-given-number-using-recursion/ https://beginnersbook.com/2014/01/java-program-to-find-factorial-of-a-given-number-using-recursion]</span>
 +
 
 +
<source lang="cpp">
 +
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;
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes6b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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);</source>
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR7: Area of Rectangle<br>'''
 +
* Program to find the area of rectangle.<br><br>
 +
 
 +
<div id="z3codes7a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-rectangle/ https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-rectangle]</span>
 +
<source lang="cpp">
 +
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);
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes7b style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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);</source>
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR8: Display Prime Numbers<br>'''
 +
* Program to print all the prime numbers till n.
 +
* z3 function PRIMES is used.<br><br>
 +
 
 +
<div id="z3codes8a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/01/java-program-to-display-prime-numbers/ https://beginnersbook.com/2014/01/java-program-to-display-prime-numbers]</span>
 +
<source lang="cpp">
 +
import java.util.Scanner;
 +
class PrimeNumbers2
 +
{
 +
  public static void main (String[] args)
 +
  {
 +
      Scanner scanner = new Scanner(System.in);
 +
      int i =0;
 +
      int num =0;
 +
      //Empty String
 +
      String  primeNumbers = "";
 +
      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
 +
    primeNumbers = primeNumbers + i + " ";
 +
}
 +
      }
 +
      System.out.println("Prime numbers from 1 to n are :");
 +
      System.out.println(primeNumbers);
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes8b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
var readlineSync = require('readline-sync');
 +
console.log("Starting");
 +
    var n;
 +
    var primenumber;
 +
    n = readlineSync.question("Enter the value of n: ");
 +
    primenumbers = PRIMES(n,1);
 +
    console.log("Prime numbers from 1 to n are : "+ primenumbers);</source>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR9: Ascending Order<br>'''
 +
* Program to find ascending order of the input numbers.
 +
* z3 function ASCENDING is used.<br><br>
 +
 
 +
<div id="z3codes9a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[https://beginnersbook.com/2014/07/java-program-for-bubble-sort-in-ascending-descending-order/ https://beginnersbook.com/2014/07/java-program-for-bubble-sort-in-ascending-descending-order]</span>
 +
<source lang="cpp">
 +
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]);
 +
  }
 +
}</source><br>
 +
 
 +
<div id="z3codes9b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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);</source>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR10:  Inputting Matrix and Calculating it's Inverse<br>'''
 +
* 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.
 +
 
 +
 
 +
<div id="z3codes10a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[http://www.sanfoundry.com/java-program-find-inverse-matrix/ http://www.sanfoundry.com/java-program-find-inverse-matrix]</span><br>
 +
 
 +
<div id="z3codes10b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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);</source><br>
 +
 
 +
[[File:Matrix2.JPG]]
 +
 
 +
 
 +
 
 +
 
 +
'''ExampleR11:  Inputting 2 Matrices and Calculating Sum,Difference and Product<br>'''
 +
* 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.<br><br>
 +
 
 +
<div id="z3codes11a" style="font-size:16px">'''Java Code:'''</div>
 +
<span class="plainlinks">[http://www.sanfoundry.com/java-program-perform-matrix-multiplication/ http://www.sanfoundry.com/java-program-perform-matrix-multiplication]</span><br>
 +
 
 +
<div id="z3codes11b" style="font-size:16px">'''z3 Code:'''</div>
 +
<source lang="cpp">
 +
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
 +
{
 +
        rowstring = readlineSync.question("Enter matrix: ");
 +
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
 +
      var Result = MATRIXADD(numbers2[i],numbers2[i+1])
 +
}
 +
console.log(numbers2,Result);</source><br>
 +
 
 +
[[File:Matrix.JPG]]

Latest revision as of 03:47, 15 February 2018

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:

https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number

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:

https://beginnersbook.com/2014/01/java-program-to-sum-the-elements-of-an-array

import java.util.Scanner;
class SumDemo{
   public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      int[] array = new int[10];
      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:

https://beginnersbook.com/2014/04/java-program-for-linear-search-example

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:

https://beginnersbook.com/2014/04/java-program-to-print-floyds-triangle-example

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:

https://beginnersbook.com/2014/01/java-program-to-reverse-a-number

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 num = readlineSync.question("Input your number and press enter: ");
     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:

https://beginnersbook.com/2014/01/java-program-to-find-factorial-of-a-given-number-using-recursion

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:

https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-rectangle

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:

https://beginnersbook.com/2014/01/java-program-to-display-prime-numbers

import java.util.Scanner;
class PrimeNumbers2
{
   public static void main (String[] args)
   {		
      Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      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
	    primeNumbers = primeNumbers + i + " ";
	 }	
      }	
      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
}


z3 Code:
var readlineSync = require('readline-sync');
console.log("Starting"); 
    var n;
    var primenumber;
    n = readlineSync.question("Enter the value of n: ");
    primenumbers = PRIMES(n,1); 
    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:

https://beginnersbook.com/2014/07/java-program-for-bubble-sort-in-ascending-descending-order

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:

http://www.sanfoundry.com/java-program-find-inverse-matrix

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:

http://www.sanfoundry.com/java-program-perform-matrix-multiplication

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
{
        rowstring = readlineSync.question("Enter matrix: ");
	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
      var Result = MATRIXADD(numbers2[i],numbers2[i+1])
}
console.log(numbers2,Result);