Create the following Methods Secret Formula create a functi
Create the following Methods
Secret Formula - create a function to calculate the following formula but instead of calculating only from 1 to 4 you should calculate from 1 to n where n is a user input value.
Solution
You just asked for the function above. which ll be
public static int getTotalValue(int choice) {
int total = 0;
for (int i = 1; i<= choice; i++){
total += 2+ (i * i);
}
return total;
}
In case you want the complete code, here it is with the main method as well. It works for any input.
Complete code
import java.util.Scanner;
public class SecretFormula {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(\"Enter the value of n\");
int choice = scanner.nextInt();
int totalValue = SecretFormula.getTotalValue(choice);
System.out.println(\"The total value is :\"+totalValue);
}
public static int getTotalValue(int choice) {
int total = 0;
for (int i = 1; i<= choice; i++){
total += 2+ (i * i);
}
return total;
}
}