Answer to Question #306783 in C for Cubes and Squares

Question #306783

2. 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-03-06T04:49:35-0500




#include <stdio.h>
#include <stdlib.h>


int main(){
	int nums[] = { -2, 1, 1, 2, 3, 
        9, 10, -3, 100, 2,
        4, -5, 1, 10, -100,
        5, 2, 3, -1, 1,
        -3, -4, -5, -6, -7,
        9, 9, 9, 9, 9,
        1, 2, 3, 4, 5,
        100, 50, 20, -51, -99};
	int i;


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


	
	getchar();
	getchar();
	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