Write a java program to find Sum of cubes and squares of elements in an array.

Write a program to get an int array as input and identify even and odd numbers. Use the following rules:

  • If the number is odd to get the cube of it if the number even gets square of it. Finally, add all cubes and squares together and return it as output.
  • The return type of the output is an integer which is the sum of cubes and squares of elements in the array.

Assume the maximum size of the array is 20.

Input and Output Format

  • Input an integer for the number of elements in the array.
  • Input consists of the integer array.
  • The output is an integer sum.

Refer sample output for formatting specifications

Sample Input 1:

5

2

6

3

4

5

Sample Output 1:

208

Find Sum of cubes and squares of elements in an array in Java

Following are the steps to find Sum of cubes and squares of elements in an array:

  • Input the size of an array.
  • Create an array of integer types.
  • Add elements to an array.
  • Pass the array to summationPattern() method.
  • Inside the method, declare and initialize variables n1 and n2 to 0.
  • Now, iterate over the array using for loop and check for the below-mentioned condition:
    • If the number is even, then multiply that number by itself. (square it) and store it in the n1 variable
    • If the number is odd, then take the cube of that number and store it in the n2 variable.
  • At last, return the sum of values of n1 and n2.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a[] = new int[20];
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
      a[i] = sc.nextInt();
    }

    System.out.println(summationPattern(a));
  }

  public static int summationPattern(int[] a) {
    int n1 = 0, n2 = 0;
    for (int i = 0; i < a.length; i++)
      if (a[i] % 2 == 0)
        n1 += (a[i] * a[i]);
      else
        n2 += (a[i] * a[i] * a[i]);
    return n1 + n2;
  }
}

Output

Java program to find Square, Cube, and Square Root of an integer number

Create a program to find square, cube, and square roots using the Math function:

  1. Math.sqrt(): This will return the square root of the specified number.
  2. Math.pow(): It returns the power of the specified number.
package com.demo3;

import java.util.Scanner;

public class Power {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println("Square of "+a+" is "+Math.pow(a,2));
        System.out.println("Cube of "+a+" is "+Math.pow(a,3));
        System.out.println("Square root of "+a+ " is "+Math.sqrt(a));
    }
}

Output

Sum of the Square of two Arrays in Java

Write a program that allows the entry of an integer value n, followed by two sets of n integer values into arrays A and B. The program should use a function to calculate the sum of the square of corresponding values of A and B. These values should then be displayed.

Input and Output Format

  • Input an integer for the number of elements in the array.
  • Input consists of the integer array.
  • The output is an integer sum of the square of two arrays.

Refer sample output for formatting specifications

Sample Input:

3
1
2
3
1
2
3

Sample Output:

Sum of the Square of two arrays 28.0

Program to find Sum of the Square of two Arrays in Java

  • Input the size of the array.
  • Create two empty arrays of size n.
  • Add elements to both the array.
  • Now, pass both the arrays to sumOfSqaure() method.
  • Inside the method, declare and initialize sumA, sumB, and total variable to 0.
  • Now, iterate over both the arrays using for loop and multiply each element by itself. and add them to sumA and SumB variables.
  • At last, just add values present in the sum variable and print the total i.e sum of squares of both the arrays.
package com.demo3;

import java.util.Scanner;

public class SquareAndCube {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int A[] = new int[n];
        int B[] = new int[n];
        for(int i = 0;i<A.length;i++){
            A[i] = sc.nextInt();
        }
        for (int i = 0;i<B.length;i++){
            B[i] = sc.nextInt();
        }
        sumOfSquare(A,B);
    }

    private static void sumOfSquare(int[] a, int[] b) {
        double n1,sumA = 0, sumB=0, total = 0;
        for(int i = 0 ;i < a.length;i++){
            sumA = sumA + (a[i]*a[i]);
        }
        for(int i = 0 ;i < b.length;i++){
            sumB = sumB + (b[i]*b[i]);
        }
        total = sumA + sumB;
        System.out.println("Sum of the Square of two arrays "+total);
    }
}

Output

Thus, in this, way, we learned how to find Sum of cubes and squares of elements in an array.