You Have To Be More Specific
It's hard to tell you what's wrong when one's not even sure what your code's supposed to do in the first place.
You should explain the following when you're posting:
- What is your program supposed to do?
- What do you think is not working right?
Let's See Some Issues
Without knowing what exactly you're trying to do here, let me run some analysis on your code.
16 (x <= i);//as it will repeatedly executed until the value is less than or equal to i
this line does nothing. This is an empty instruction. You ask the compiler to compare if x is less or equal to i, but then that's it, you don't do anything with the result, just throw it away.
Consequently, i is an unused variable and for this, the following lines could be removed:
5 int i;
10 i = n * n * n;
The value of i is never read, so there's no point in writing the value of i and no point in creating this variable.
21 printf("%d\n", x);
has a wrong indentation. It should have the same indentation as the line above. This is not a problem to the compiler, but to the reader, making humans confused.
10 x = (n * (n - 1)) + 1; //formula to get the first addend
12 y = 2; //since the first addend is an odd number, then by adding 2 to the first addend will result into another odd addned
18 x += y; //the first addend will repeatedly add 2 to itself
20 while (x <= n);//I thought that this loop will help us to print the same count of the inputted number
It's easy to see that x is always greater than any non-negative n, so the while statement in line 20 will be always false (except for when an overflow happens). So you probably wanted to do something differently.
UPDATE
Problem specified
Thank you for specifying the problem in more detail. Now it is clear what your program is supposed to do. Now I can give you advice on how to fix it.
More Problems
Apart from the problems mentioned above, I've found that the formula you use (n * (n - 1) + 1) does not really work. So I had to create a new formula. Actually two: one for even one for odd numbers.
A Possible Solution
I came up with the solution below for the given problem. I used your code as a start and fixed it up.
I needed an array to store the actual addends that the program is supposed to print and called it addends for simplicity.
In the next step we apply the formula (depending on even or odd) to calculate the middle addend then iterate toward the beginning of the array and set every element being 2 less than the previous one.
Then do this toward the other direction: starting from the middle iterate through every element toward the last in the addends array and set every element 2 more than the previous one.
After this, all is left is to print the elements of addends.
I grouped the relevant code into a single function called Pow3().
I've also created a small function TestPow3() to test if the Pow3() works correctly. This utilizes a macro called TEST that checks if the function's return value (the n3) matches an expected value.
The Code
#include <stdio.h>
// Checks if `testToRun` equals `expected`
#define TEST(testToRun, expected) \
if ((testToRun) == (expected)) \
printf(" <<< SUCCESS\n"); \
else \
printf(" <<< FAILURE\n")
// Calculates n ^ 3 based on addition and returns it. Additionally, it prints the addends on the standard output
int Pow3(int n)
{
//
// Validating the input parameter
//
if (n < 1 || n > 30)
{
printf("The integer input is out of range!");
return -1;
}
//
// Calculating the addends
//
int addends[n];
int middle = (n - 1) / 2; // Start index of the addends
addends[middle] = n % 2 == 0 ? n * n - 1 : n * n;
for (int i = middle - 1; i >= 0; i--)
addends[i] = addends[i + 1] - 2;
for (int i = middle + 1; i < n; i++)
addends[i] = addends[i - 1] + 2;
//
// Printing the addends and the sum
//
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += addends[i];
printf("%d", addends[i]);
if (i != n - 1)
printf(" + ");
}
printf(" = %d", sum);
return sum;
}
// Unit tests for Pow3()
void TestPow3()
{
//
// Testing all the positive test cases
//
for (int i = 1; i <= 30; i++)
TEST(Pow3(i), i * i * i);
//
// Testing some negative test cases
//
TEST(Pow3(0), -1);
TEST(Pow3(31), -1);
}
int main()
{
//TestPow3(); // Uncomment to run unit tests for Pow3()
//
// User input
//
int n = 0;
printf("Input integer value in range [1, 30]: ");
scanf("%d", &n);
return Pow3(n);
}
Note #1
The above code works only on Linux/Unix based systems as the Microsoft C compiler does not support variable-length arrays:
int addends[n];
In this case, the array needs to be dynamically allocated and deallocated, once it's not needed anymore (see malloc() and free()). This involves some performance hit, but probably this is not a concern at all for this task.
Note #2
Feel free to modify my code to your needs. It is just one example of how it could be done. Feel free to enhance it. Make it better! Experiment!
Have fun! :)