█ Need Help

Hello,
I wanted to output all even nos. between 1 and 20 inclusive,
but what should i do ?
i have no idea about this one
Danish
Last edited on
Do you know what a natural number is?

Do you know what a for loop is?

If you know that it shouldn't be hard to do.
Sorry,
did a mistake, I want to output all even nos. between 1 and 20 inclusive
If you know how to print all numbers between 1 and 20, you just need to do a small change to make it print only even numbers.
yes,
and im not getting over that change
Use the mod, %. example 8%2=0, so 8 is even. 2 go into 8 evenly, with nothing left over, but 9%2 would equal 1. 9 is odd. Hope this gets you closer to the a solution.
i wrote a code for you.
i think it isnt difficult.
so if i understood good you want this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main ()
{

  cout<<"these are the evens bettwen of 0 and 20:"<<endl;
  for(int i=0;i<=20;i++)
  {

      if(i%2!=0)
      {
          continue;
          cout<<i<<endl;
      }


        cout<<"even:"<<i<<endl;

  }
}


Run the program and tell me if it helped you,because if it isnt helped you,i will code your program again if wants.

(i love help other people):P
@skarla
I have learned for myself that you ought to compile and run your program before pressing 'submit'.

Also, it does not help people to do their assignments for them. (Particularly if the answer is wrong -- it confuses them more than anything.)

@ATMOSF3AR
The mathematical property of even numbers is: they are evenly divisible by 2.
In a computer program, you can check whether a number has any remainder by using the 'modulo' (or 'remainder') operator; in C++ that's the percent sign: %.

1
2
3
4
5
for (int i = 1; i <= 5; i++)
  cout << i << " / 2 has a remainder of " << (i % 2) << "\n";
1 / 2 has a remainder of 1
2 / 2 has a remainder of 0
3 / 2 has a remainder of 1
4 / 2 has a remainder of 0
5 / 2 has a remainder of 1

The trick is to make yourself a loop that goes 1 to 20, and inside that loop check if a number / 2 has a remainder of... zero! If it does, you can print the number.

Hope this helps.
my program it run good,and my program has only even numbers form 1-20.
so where is the problem?
Your program produces correct output, but I think that is more from luck than anything else. It is malformed:

You are testing for and acting on odd numbers, not even.
You use an unnecessary continue.
You have a line of code that can never be executed.

Maybe you are trying to confuse the OP?

[edit] I don't really mean to be harsh. I'm having a bad day. But I stand by the facts that (1) your code is not clear and (2) you are endeavoring to hand a solution to the OP.
Last edited on
our programs are the same.
but you tell if it is true and false,while me i am telling which numbers are even without odd,so my program is better.

Also a program isnt from luck.
may you know better because i am learning 15 days without experience but my program is great ,so why are you curious?
Why are you so intent on derailing this thread?
For a 15-day old program, it isn't bad.
For help to another user, it reeks.
Take a hint and learn something yourself.
It's somewhat refreshing to see a different approach to solve the same problem. There are, of course, many ways to get the "right" output. However, I agree with Duoas. It's logically different than the problem statement for no good reason. Numbers between 1 and 20, but 0 is being tested...
Topic archived. No new replies allowed.