1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Lorico [155]
4 years ago
12

Write a program to find and print the sum of the first n cubes in the following four ways.

Computers and Technology
1 answer:
klio [65]4 years ago
3 0

Answer:

The solution code is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.           System.out.print("Please enter a number: ");
  4.           Scanner console = new Scanner(System.in);
  5.           int n = console.nextInt();
  6.           calculateSum1(n);
  7.           calculateSum2(n);
  8.           calculateSum3(n);
  9.           calculateSum4(n);
  10.    }
  11.    public static void calculateSum1 (int num){
  12.        int sum1 = 0;
  13.        for(int i=1; i <= num; i++){
  14.            sum1 += i*i*i;
  15.        }
  16.        System.out.println("Sum 1 : " + sum1);
  17.    }
  18.    public static void calculateSum2 (int num){
  19.        int sum2 = num * num * (num + 1) * (num + 1) / 4;
  20.        System.out.println("Sum 2 : " + sum2);
  21.    }
  22.    public static void calculateSum3 (int num){
  23.        int sum3 = 0;
  24.        for(int i=1; i <=num; i++){
  25.            sum3 += i;
  26.        }
  27.        sum3 = sum3 * sum3;
  28.        System.out.println("Sum 3 : " + sum3);
  29.    }
  30.    public static void calculateSum4 (int num){
  31.        int sum4 = (num * (num + 1) * (2*num + 1)) / (4+2);
  32.        System.out.println("Sum 4 : " + sum4);
  33.    }
  34. }

Explanation:

Firstly we create four different methods that calculate the sum of the first n cubes in different ways (Line 13 - 43).

This is important to understand the concept of  operator precedence to work out the different ways of summation.

For example, The operator * and / will always have higher precedence than +.

On another hand, any expression enclosed within the parenthesis will have highest precedence and therefore the value will be evaluated prior to others.  

This is also important to understand the expression evaluation will always start from left to right.

You might be interested in
In addition to MLA, what are some other widely used style guides? Check all that apply.
Lostsunrise [7]
APA and Chicago manual of style :D
6 0
3 years ago
Read 2 more answers
What is a Slide Master?
mafiozo [28]

The correct answer is c. the default design template in a presentation program for Plato. Hope this helps :)

8 0
4 years ago
Read 2 more answers
hi Let's say you graduate from school and you are unemployed or take a low-paying job. What are your debt repayment options if y
maks197457 [2]

Income-Based Repayment would be one of your repayment options.

IBR is an option similar to Pay As You Earn but offers more flexibility. To qualify for an IBR, your prospective payments must be lower than they would be on the Standard Repayment Plan. You can still sign up for an IBR even if you are still unemployed. These plans are solely based on your income. So if you are unemployed, this translates to zero income. As a result, your monthly payment will be $0

4 0
4 years ago
Read 2 more answers
Dir, attrib, cd, and rem are all examples of:
konstantin123 [22]
Dir, attrib, cd, and rem are all examples of DOS Commands. dir command is used to display a list of files and folders contained inside your current folder. Attrib changes or views the attributes of one or more files. CD id for changing directory.  rem<span> command is used to record comments in a script file.</span>
3 0
3 years ago
Read 2 more answers
Which statement best describes antivirus software?
valentina_108 [34]
It identifies and removes viruses in computers
7 0
4 years ago
Other questions:
  • A company that hires only American Indians is practicing
    5·2 answers
  • Text, numbers,graphics, sounds entered into a computer's memory during input operations are referred to as
    11·1 answer
  • Is brainly down? Cant search anything
    8·1 answer
  • What is the output of the following code fragment? int i = 1; int sum = 0; while (i &lt;= 15) { sum = sum + i; i++; } System.out
    5·1 answer
  • What are 5 ways we can reduce our energy?
    5·1 answer
  • Which of the following is most likely to require use of a server, desktop, and industry-specific handheld computer, and is most
    8·1 answer
  • Based on what you know about the Sort and Find functions, return to the database file to determine the answers to the following
    7·2 answers
  • Why is computer called and information processing machine​
    6·1 answer
  • Assume you're using a three button mouse.To access shortcut menus,you would
    10·1 answer
  • It would be at least two decades before some of
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!