How to create a code in c that if I insert any number "n" it displays all the "even" numbers that are smaller or equal to "n"
I dont have much knowledge in C so I would appreciate the help
CodePudding user response:
This is what I tried and think it's good enough
int n;
cout << "\nEnter number= ";
cin >> n;
cout << "\nEven numbers are ";
for(int i = 0; i <= n; i )
{
if ( i % 2 == 0 )
{
cout << i <<" ";
}
}
CodePudding user response:
You can use a repetition loop to check for all the numbers until N and the look if each number is divisible by 2 (when, in that division, the remainder is 0).
@john ideia of starting counting at 2 and going up 2 at a time is a easier one.