Scientific Programming II

Programming in Java

Unit 2 - Example Programs


Example 1

The following program computes the maximum in an input integer array.

// Computing the maximum in an integer array
import java.util.Scanner;
 
public class MaxArray {
    public static void main(String[] args) {
        System.out.println("Find Maximum - Enter numbers >= 0");
        Scanner input = new Scanner(System.in);
        
        System.out.println("Input the size of the array");
        int size = input.nextInt();
        int arr[] = new int[size];
        int max = -100;
        for (int i = 0; i < arr.length; i++) {
            System.out.println("Input Number #" + (i+1));
            arr[i] = input.nextInt();
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("The maximum is " + max);
    }
}

Example 2

Using arrays, write a program to print the Fibonacci numbers as specified by the input.

import java.util.Scanner;
 
public class Fibonacci_Array {
    public static void main(String[] args) {
        int z;
        System.out.println("How many Fibonacci Numbers do you need?");
        Scanner input = new Scanner(System.in);
        z = m.nextInt();
        int a[] = new int [z];
        a[0] = 0;
        a[1] = 1;
        for (int i = 2; i< z; i++) {
            a[i] = a[i-1] + a[i-2];
        }
        for (int i=0; i<z; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

Example 3

Let us define equality of two arrays as follows: Two arrays, A and B are said to be equal if they are of the same size and have the same values. The order of values in A and B do not matter. For this problem, the size of the arrays will be the same and the values within both the arrays will be distinct. For example, if A and B are of size = 5 and A = {1,2,3,4,5} and B = {5,4,2,3,1}, they are equal. Otherwise, they are unequal. Your program should ask the user for the size of the arrays and then proceed to take input values for both the arrays. Finally, the program should state whether the two arrays are equal or unequal.

import java.util.Scanner;
 
public class Equal_Arrays {
    public static void main (String[] args) {
        System.out.println("Input two arrays. Both arrays will have the same size");
        Scanner m = new Scanner(System.in);
     
        int count = 0;
        System.out.println("Input Array Size");
        int size = m.nextInt();
       
        int a[] = new int[size];
        int b[] = new int[size];
       
        System.out.println("Input Values for Array_1");
        for (int i = 0; i<size; i++) {
            System.out.println("Input Value " + (i+1));
            a[i] = m.nextInt();
        }
      
        System.out.println("\n");   
        System.out.println("Input Values for Array_2");
        for (int i = 0; i<size; i++) {
            System.out.println("Input Value " + (i+1));
            b[i] = m.nextInt();
        }
        System.out.println("\n");
             
        for(int i = 0; i< size; i++) {
            for(int j = 0; j <size; j++) {
                if (a[i] == b[j]) {
                        count++;
                }
            }
        }
         
        if (count == size) {
            System.out.print("Arrays A and B are Equal");
        } else {
            System.out.print("Arrays A and B are Unequal");
        }
    }
}

Example 4

Declare an integer array of input size. Take integer inputs. Store only those input values in the array which are not duplicates of values already stored. Output your array after each input. Your program will quit when all values in the array have been filled up with unique integer input values.

import java.util.Scanner;
 
public class Array_Distinct_Insert {
    public static void main (String[] args) {
        System.out.println("Input Array Size");
        Scanner m = new Scanner (System.in);
        
        int SIZE = m.nextInt();
        int length = 0;
       
        int a[] = new int[SIZE]; //array for input values
  
        while (length < SIZE) {
            System.out.println("Input Value ");
            int input = m.nextInt();
         
            int count = 0;
           
            for (int i = 0; i < SIZE; i++) { // Check for duplicates
                if (a[i] == input) {
                    count++;
                }
            }
               
            if (count == 0) {
                a[length] = input;      // Insert unique value
                length++;
                System.out.println("Array: ");
                for (int i=0; i<length; i++) {
                    System.out.print(a[i] + "  ");
                }
                System.out.println();   // new line
            } else {
                System.out.println (input + " is already a value in the array");
            }
        }
    }
}

Example 5

Write a program that checks if an input integer is prime or not.

import java.util.Scanner;
 
public class Prime_Check {
    public static boolean check_If_Prime(int p) {
        int i = 2;
        boolean m = true;
       
        if(p == 1 || p == 0) {
            return false;
        } else { 
            while (i < p && m == true) {
                if (p % i == 0) {
                    m = false;
                } else {
                    i++;
                }
            }
   
            if (m == true) {
                return true;
            } else {
                return false;
            }
        }
    }
 
    public static void main (String[] args) {
        int z;
        System.out.println("Input A Number Greater Than Zero:");
        Scanner m = new Scanner (System.in);
        z = m.nextInt();
        if(check_If_Prime (z) == true) {
            System.out.println(z + " is prime ");
        } else {
            System.out.println(z + " is not prime");
        }
    }
}