heyy all..I am really confused on the MOD (%) operator and how it is processed..

int n;
	cout << "Enter n: ";
	cin >> n;
	for (int i=1; i<=n; i=i+1)
		if ( (i % 2) == 0) //even
			cout << "-";
		else  //odd
			cout << "^";

What exactly is going on with the %..I want to understand the way that the computer processes that..I have no idea how to use it.

thankssss

The modulo operator is the remainder operator. it says what is left after I divide x by y. So if you do 10 % 2 you get zero because 10 / 2 = 5 with nothing remaining. If you do 10 % 4 you get 2 because 10 / 4 = 2 so 4 goes into 10 two times and there will be 2 remaining. The way to calculate % is

let x = number to get the mod from
let y = number to mod against

mod = x - ((x / y) * y)

2 = 10 - ((10 / 4) * 4)

2 = 10 - (2 * 4)

2 = 10 - 8

2 = 2
this calculation only works when doing integer division
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.