0
Why my code is not working? I want to print first five even numbers
#include <stdio.h> int *get_evens(); int main() { int *a; int k; a = get_evens ; for (k=0;k<5;k++) printf ("%d\n",a[k]); return 0; } int *get_evens (){ static int nums [5]; int k ; int even=0; for(k=0;k<5;k++){ nums[k] = even+=2; } return (nums); }
2 Réponses
+ 2
* In main()
a = get_evens(); // function call uses parentheses
Parentheses means function call.
* In get_evens()
You are incrementing <even> inside the for...loop body as you fill up array <nums> elements. As it currently is, the result started from 2 instead of zero. It's fine if it was intentionally though ...
0
for(int i = 1; i<=5; i++) printf("%d\n", 2*i);