-4

When I input a negative integer the output will not result to it's cube value. I'm confused what should I do? here is my code:

#include <stdio.h>
int main()
{
    
    int n, i, cube;
    printf("Enter an integer: ");
    scanf("%d",&n); 
        
    while (i<=n)
    {   
        cube = i*i*i;   
        i++;

    }
    printf("The cube of %d = %d\n ", n, cube);  

    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
gigibot
  • 3
  • 1
  • I'm confused as to what this is trying to do. – Scott Hunter Nov 08 '21 at 23:25
  • 5
    You never initialized `i`. – Barmar Nov 08 '21 at 23:27
  • `i` is uninitialized, as is `cube`. Therefore there's no guarantee `cube` will have a determinate value after the loop (because the loop body may never be executed). There's absolutely no point in having this loop in the first place. Explain why `cube = n * n * n;` is not sufficient. – paddy Nov 08 '21 at 23:28
  • You just need `cube = n * n * n;` – Barmar Nov 08 '21 at 23:29
  • Maybe he wants you to print the cubes of all numbers from 1 to n. – Barmar Nov 08 '21 at 23:29
  • Please post the exact text of the assignment. – Barmar Nov 08 '21 at 23:30
  • This is the reason most courses have lab sessions with tutors on hand to assist with understanding. Does your institution not have such a learning structure? – paddy Nov 08 '21 at 23:31
  • 1
    If it is a requirement that you use a loop, you will probably want to initialize `cube=1` then loop 3 times multiplying it by n (`cube *= n`). But as observed above, there is no technical need for a loop and this is equivalent to `cube=n*n*n`. – EdmCoff Nov 08 '21 at 23:32
  • You could try something like `i=3; cube=1; while (i) { cube *= n; i--; }`. In your example, you should initialize `i`. And notice that, if you initialize it to zero, for example, and you keep incrementing it, it will take a while for it to be smaller than a negative `n` (it has to overflow first). – rturrado Nov 08 '21 at 23:32

3 Answers3

1

I would assume you'd want to compute the cube of a number by using a loop that iterates 3 times.

int n, cube, i;
printf("Enter an integer: ");
scanf("%d",&n); 

cube = 1;
i = 0;
while (i < 3)
{
    cube = cube * n;
    i++;
}

printf("%d\n", cube);
selbie
  • 100,020
  • 15
  • 103
  • 173
0
  1. Loop is wrong.
  2. Your loop invokes undefined behaviour as i is not initialized.

You simply need:

long long cube(int n)
{
    return n * n * n;
}

int main(void)
{
    int x = -21;
    while(x++ < 40)
        printf("n=%d n^3 = %lld\n", x, cube(x));

}

Or if you need to use while loop:

long long cube(int n)
{
    long long result = 1;
    int power = 3;
    while(power--) result *= n;
    return result;
    
}

Or full of whiles and no multiplication.

long long cube(int n)
{
    long long result = 0;
    unsigned tempval = abs(n);
    while(tempval--) result += n;
    tempval = abs(n);
    n = result;
    while(--tempval) result += n;
    return result;
    
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

You just want the cube of 1 number?

i = 0;
cube = 1;
while (i<3)
{ 
    cube *= n;
    i++;
}
stark
  • 12,615
  • 3
  • 33
  • 50