Answer to Question #287405 in C for gel

Question #287405

Cubes and Squares

by CodeChum Admin

You can also make a list of numbers, ey? Let's go and have some fun in tweaking the values of each element, but let's give it some twist to add on some fun to the code.


Let's have a go at it!


Instructions:

  1. An array containing 40 integer elements is already provided for you in the code editor below.
  2. Using loops and conditions, print out the cube of the array element if it is a positive number, and print out the square of the number if it is negative. Each result must be printed out separately by each line.

Output

The squares and cubes of the elements in the array.

4
1
1
8
27
.
.
.
1
Expert's answer
2022-01-13T16:11:44-0500
#include <stdio.h>

int main() {
    int array[40] = {-2, -1, 1, 2, 3, 4, 3, -2, 1, 3,
                     -1, 2, 3, -3, 5, 2, -2, 3, 1, -2,
                     2, 3, -1, -3, 2, 4, 5, 7, -5, -3,
                     4, 3, -2, -1, -4, -5, 3, 2, -2, 3 };

    for (int i=0; i<40; i++) {
        if (array[i] < 0)
            printf("%d\n", array[i]*array[i]);
        else
            printf("%d\n", array[i]*array[i]*array[i]);
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS