Manuals/calci/Examples

Revision as of 07:02, 13 October 2017 by Sahiti (talk | contribs) (→‎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

1. Check Odd/Even
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")};


2. Sum of Array
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);


3. Linear Search
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.")};



4. Floyd's Triangle
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 number = 1;
	var counter;
    var	j;
	
       //To get the user's input
       rows = readlineSync.question("Enter the number of rows for floyd's triangle:");
	   
       //Copying user input into an integer variable named rows
       console.log("Floyd's triangle");
       console.log("****************");
       for ( counter = 1 ; counter <= rows ; counter++ ){
           for ( j = 1 ; j <= counter ; j++ ){
                console.log(number+"");
                //Incrementing the number value
                number++;
            }
           //For new line
           console.log();
        };


5.Reverse Number
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);


6.Factorial
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);



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



8.Display Prime Numbers
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 i =0;
    var num =0;
    var counter;
    var primeNumbers = "";
    var n = readlineSync.question("Enter the value of n:");
      for (i = 1; i <= n; i++)  	   
      { 		 		  
        counter=0; 		  
        for(num =i; num>=1; num--)
        {
	    (i%num==0)::
       	    {
	    counter = counter + 1;
            }
	}
        (counter ==2)::
	{
	  //Appended the Prime number to the String
	  primeNumbers = primeNumbers + i + " ";
	}	
      }	
      console.log("Prime numbers from 1 to n are :");
      console.log(primeNumbers);




9.Ascending Order
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);