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++)
		if(n%j==0)
		{
		cout << "IS even number" << endl;
		exit(0);
		}
	cout << "NOT even number" << endl;
	return 0;
}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.