Answers for "calculate values of array"

0

calculate values of array

import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        Sum arr = new Sum();
        setArraysize(arr,5);
        enterValues(arr);
        System.out.println("Sigma ==> ");
        int sigma = 0;
        sigma = scanner.nextInt();
        calculateSigma(sigma);
        arr.calculateValuesOfArr();
        arr.printSumOfValues();


    }
    public static void setArraysize(Sum arr,int size){
        arr.setArrAndSize(size);
    }

    public static void  calculateSigma(int n){
        Sum sigma = new Sum();
        sigma.sigma(n);
        sigma.printSigma();
    }

    public static void enterValues(Sum arr){
        arr.enterValues();
    }
}
Posted by: Guest on July-30-2021
0

calculate values of array

import java.util.Scanner;

public class Sum {
   private int [] arr;
   private int size ;
   private int arrSum ;
   private int n;
   private int sigma = 0 ;

   private Scanner scanner = new Scanner(System.in);

    public Sum(){

    }
    public Sum(int size) {
        this.size = size;
        this.arr = new int [size];
    }

    public void setArrAndSize(int size){
        this.arr = new int[this.size = size];
        System.out.println("\tArray has been initialized\n");
    }

    public void enterValues(){
        if(this.arr == null){
            System.out.println("\tEnter size of the array\n");
            int size = scanner.nextInt();
            setArrAndSize(size);
        }
        System.out.println("\t(Enter "+this.arr.length+" numbers)\n");
        for(int i = 0; i < this.arr.length ; ++i){
            this.arr[i] = scanner.nextInt();
        }
    }

    public void calculateValuesOfArr(){
        this.arrSum = recursiveSigma(this.size-1);
    }



    private int recursiveSigma( int l){
        if(l == 0 )
            return arr[l];

        return this.arr[l] + recursiveSigma(l-1);

    }

    public  void sigma(int n){
        this.n = n ;
        calculatSigma(n);

    }
    private int  calculatSigma(int n){
        if(n == 0)
             return this.sigma ;
        this.sigma = n + calculatSigma(n-1);
        return this.sigma;
    }

    public void printSumOfValues(){
        System.out.println("Array values = "+this.arrSum+"\n");
    }

    public void printSigma(){
        System.out.println("Sigma "+ this.n+" = "+this.sigma+"\n");
    }
}
Posted by: Guest on July-30-2021

Code answers related to "calculate values of array"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language