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); }

27th Apr 2022, 7:16 AM
Rushikesh Jogdand
Rushikesh Jogdand - avatar
2 Answers
+ 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 ...
27th Apr 2022, 7:51 AM
Ipang
0
for(int i = 1; i<=5; i++) printf("%d\n", 2*i);
27th Apr 2022, 1:06 PM
Brian
Brian - avatar