Your code compiles (which is quite surprising, to be honest).
What you have right now is simply code that is executed inside the main function. int n
is an uninitialized variable and you cannot know its value (apparently, it seems to be >=0 when you tried). In other words: the program currently has undefined behavior.
What you need to do: you need to declare and define a function which writes the required output. Once you have that, you need to call that function from your main
function.
A function's signature follows the form: return type + function name + function parameters, e.g. int fun(int num)
. A function needs a body too, which defines what this function does when called.
A very simple example:
/* define the function "square", which takes a single parameter and returns the squared value */
int square(int num) {
return num * num;
}
int main(void) {
/* call the function: */
int squared = square(5);
/* squared now contains the value 25. you can use it in other calculations or function calls, such as `printf` */
return 0;
}
With that, you should have the necessary information to rewrite your program to define a function, call the function and then write the function's output.