since few minutes I tried practice "using FOR loop with IF statement" to test user "Even Numbers"(divided / 2) in console screen, I wrote this trivial program to print the statement "IS even number" if the number divided by 2, and print "NOT even number" when not divided by 2, when I tried input a number like "1313" prints "IS even number" although it isn't even number..by calculater 1313/2 = 656.5 ... !
p.s. i want do that without using IF .. ELSE

This is my code:

// write some even numbers | loops and if statement
#include <iostream>
#include <process.h>
using namespace std;
int main()
	unsigned int n,j;

	cout << "Enter a number: ";
	cin  >> n;

	for(j=2; j<=n/2; j++)
		cout << "IS even number" << endl;
	cout << "NOT even number" << endl;
	return 0;
8 Years
Discussion Span
Last Post by WaltP

Remove the exit(0); call.

It kills the entire program, not just the if (or the while).

Look up 'break' and 'continue' for more refined loop controls.


Try it with 9.

You will find it is also even because in the loop when j=3, n%j = 0

This loop has nothing to do with odd/even. Where did you get this code?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.