#include<stdio.h>
int main(void) {
int nums[100] = {
2, 3, 5, 100, 7, 3, 5, 3, 1, 4,
76, 77, 78, 79, 80, 54, 68, 61, 34, 33,
44, 49, 100, 5, 2, 87, 34, 33, 2, 1,
4, 5, 8, 2, 1, 8, 5, 3, 2, 1,
88, 22, 23, 33, 48, 44, 46, 48, 50, 52,
45, 47, 49, 51, 53, 1, 1, 1, 1, 1,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
11, 22, 33, 44, 55, 66, 77, 88, 99, 100,
1000, 2001, 3000, 4001, 5000, 6001, 7000, 8001, 9000, 10001
};
for(int nums=2; nums<=100; nums )
{
if(nums%2==0){
printf("%d - I love Coding\n", nums);
}
}
return 0;
}
My output is:
2 - I love Coding
4 - I love Coding
.
.
100 - I love coding
but it should be
2 - I love Coding
100 - I love Coding
4 - I love Coding
.
.
.
Hopefully this makes sense.
CodePudding user response:
Two issues here. Your loop counter has the same name as the array, so it masks the array from being seen. Also, you're checking if the loop counter is divsible by 2, not the members of the array.
The loop counter should have a name different from the array. It should run from 0 to 99 (since those are the valid array indices for an array of size 100), and you need to check the actual array members.
for(int i=0; i<100; i )
{
if(nums[i]%2==0){
printf("%d - I love Coding\n", nums[i]);
}
}
CodePudding user response:
You are checking and printing the index of the numbers, not the numbers within the array itself.
Change your code into:
for(int index=2; index<100; index )
{
if(nums[index]%2==0){
printf("%d - I love Coding\n", nums[index]);
}
}