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
Sarah is having a hard time finding a template for her advertising buisness that she mah be able to use at a later date and also
irinina [24]

Answer: create a custom template

Explanation:

Since Sarah is having a hard time finding a template for her advertising business that she may be able to use at a later date and also make it available to her colleagues, her best option will be to create a custom template.

Creating a custom template will ensure that she makes the template based on her requirements and can tailor it specifically to her needs which then makes it unique.

6 0
3 years ago
Not a subject question but please help
Doss [256]

Answer:

You go to account settings, edit your profile and click preferences and then go to choose level and put which grade u are in the options r middle school, high school, and college

Explanation:

8 0
3 years ago
Read 2 more answers
In the welding operations of a bicycle manufacturer, a bike frame has a long flow time. The set up for the bike frame is a 7 hou
mestny [16]

Answer:

Bike Frame Flow Time

The value-added percentage of the flow time for this bike frame is:

= 46.

Explanation:

a) Data and Calculations:

Bike Frame Flow Time:

Setup time = 7 hours

Processing time = 6 hours

Storage time = 7 hours

Flow time of the bike frame = 13 hours (7 + 6)

Value-added percentage of the flow time for this bike frame = 6/13 * 100

= 46%

b) Flow time represents the amount of time a bicycle frame spends in the manufacturing process from setup to end.  It is called the total processing time. Unless there is one path through the process, the flow time equals the length of the longest process path.  The storage time is not included in the flow time since it is not a manufacturing process.

8 0
3 years ago
How to find out where an item was purchased by the upc code?
diamong [38]
There should be a search engine
3 0
3 years ago
Write a function max_magnitude() with two integer input parameters that returns the largest magnitude value. Use the function in
JulsSmile [24]

Answer:

def max_magnitude(user_val1, user_val2):

if abs(user_val1) > abs(user_val2):

return user_val1

else:

return user_val2

if __name__ == '__main__':

n1 = int(input("Enter the first integer number: "))

n2 = int(input("Enter the second integer number: "))

print("The largest magnitude value of", n1, "and", n2, "is", max_magnitude(n1, n2))

Explanation:

5 0
3 years ago
Other questions:
  • When u look at a green object through red glass the object will appear
    11·2 answers
  • You are attempting to open a file containing a list of integer numbers, each on a separate line, and read them into Python as in
    6·1 answer
  • The class shown below called is called CSVReader. Use this class exactly as it is written (specifically don’t add any instance v
    8·1 answer
  • Name the main technology used to connect computers to devices
    6·1 answer
  • Sometimes, fourth-generation languages (4GLs) are called procedural languages
    9·1 answer
  • Which career field has grown most as a result of technological changes?
    13·1 answer
  • To update a bibliography field that is not contained in a ____, right-click the bibliography, and then click Update Field on the
    10·1 answer
  • Type in computer code for Sonic the Hedgehog, does it look weird yes or no
    15·1 answer
  • Need answer ASAP!!!!!
    6·1 answer
  • Write a static method that takes a String and returns an integer. Return the number of characters in the passed in, String param
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!