how if(i==num) works in this program

Discussion in 'Programmer's Corner' started by engrrehmat, Feb 10, 2013.

1. engrrehmat Thread Starter New Member

Dec 17, 2010
26
0
int main()
{
int i,num;
printf("enter a number:");
scanf("%d",&num);

i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf("\nNot a prime number");

break;
}
i++;
}
if(i==num)
printf("prime number");

getch();
}

2. jaygatsby New Member

Nov 23, 2011
185
23
It's simply a test of equality between the variables i and num...

3. killivolt Active Member

Jan 10, 2010
432
351
Code ( (Unknown Language)):
1. It tests whether what is on the left side is equal to what is on the right side. It's the equality operator. So for example:
2.
3. 5 == 5; is true
4. 5 == 4; is false
5.
6. int x = 5;
7. x == 34; is false
8.
9. x == 35 - 30; is true
10.
11. The reason it is == and not just = is because = is the assignment operator.
12.
13. So x = 5; assigns the value 5 to x. It does not test whether x is equal to 5. You need to use == for that.

Last edited: Feb 10, 2013
4. killivolt Active Member

Jan 10, 2010
432
351
You beat me. Mine is to wordy.

5. engrrehmat Thread Starter New Member

Dec 17, 2010
26
0
i know this that if 5==5 than it works.
i am confused how it works when i give an input 5 and num=5, then how it becomes true that i==num, while i is initialized as i=2.
how it works, 2==5????

6. thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
% is the "Modulus" operator. It gives the remainder after division.

so if i=2, any number mod 2 will result in 0 (even) or 1 (odd)

The mod operator is also useful for scaling clocks/counters in a microcontroller. Something like count=time%10 will cause count to be a number between 0 and 9 as time becomes a larger and larger number.

7. spinnaker AAC Fanatic!

Oct 29, 2009
5,076
1,176
2==5 could not possibly evaluate to true. There has to be something wrong with your assumption.

That is the purpose of a debugger to help you find things that are wrong that you assume to be correct.

Learn to use your debugger and you will be able to quickly determine where you went wrong.

8. spinnaker AAC Fanatic!

Oct 29, 2009
5,076
1,176
OP does several comparisons in the code. I would assume with the 2==5 example OP was referring to the last comparison of:

if(i==num)
printf("prime number");

9. engrrehmat Thread Starter New Member

Dec 17, 2010
26
0
i am confused, how program works after break???
i know how "%" works, but brother i am confused after break statement, how i is incremented and how it works in the if statement, if(i==num), this statement has confused me and i am stuck.

10. killivolt Active Member

Jan 10, 2010
432
351
So, I thought, i++; after break; incremented to swap to if(i==num) from i=2 as a Postfix.
Edit: Easy on me I'm just a beginner too!

11. thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718

The program simply runs i from 2 to the number entered, and does a brute force attempt to find a number that divides into the number entered, if one does, num%i=0, and it is not prime. If it goes through the entire list of numbers up to i, when i==num, the program says the number is prime since it would have stopped earlier when a divisor with mod 0 was found.

The speed can be doubled on the program, as only num/2 numbers need to be tested, rather than 1 to num, there are other optimizations to be made, but it suffices for finding primes.

killivolt likes this.
12. spinnaker AAC Fanatic!

Oct 29, 2009
5,076
1,176
Maybe easier to see had the op used code tags.

Code ( (Unknown Language)):
1.
2. int main()
3. {
4.        int i,num;
5.       printf("enter a number:");
6.       scanf("%d",&num);
7.
8.       i=2;
9.      while(i<=num-1)
10.      {
11.          if(num%i==0)
12.        {
13.           printf("\nNot a prime number");
14.
15.          break;
16.        }
17.        i++;
18.      }
19.      if(i==num)
20.         printf("prime number");
21.      getch();
22. }
23.

13. thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
The program continually tests num mod i for 0, meaning it is divisible, thus, not prime.

Say the number entered was 15. The first loop, 15%2=1, so i would increment, then 15%3=0

As soon as 15%3==0 is found, the loop breaks, jumping to the end curly braces of that function.

then i++ adds 1 to 3 (the number i was at when the break occured)
leaving i=4

The final test test to see if number entered is the same number the index reached, in this case 4 != 15, so "prime number" is not printed, and that function is skipped.

Does that make sense?

killivolt likes this.
14. spinnaker AAC Fanatic!

Oct 29, 2009
5,076
1,176
The break statement causes the while loop to terminate "prematurely".

15. WBahn Moderator

Mar 31, 2012
18,087
4,917
First, let's get the code in CODE tags to make it readable:

Code ( (Unknown Language)):
1.
2. int main()
3. {
4.    int i,num;
5.    printf("enter a number:");
6.    scanf("%d",&num);
7.
8.    i=2;
9.    while(i<=num-1)
10.    {
11.       if(num%i==0)
12.       {
13.          printf("\nNot a prime number");
14.          break;
15.       }
16.       i++;
17.    }
18.
19.    if(i==num)
20.       printf("prime number");
21.
22.    getch();
23. }
24.
Now, here's a novel idea, let's comment the code!

Code ( (Unknown Language)):
1.
2. int main()
3. {
4.    int i,num;
5.    printf("enter a number:");
6.    scanf("%d",&num);
7.
8.    // Check each value, i, between 2 and num-1 to
9.    // see if any of them evenly divide num.
10.    // If none of them do, then i will end up being equal to num.
11.
12.    i=2;
13.    while(i<=num-1)
14.    {
15.       if(num%i==0) // if True, then i divides num
16.       {
17.          printf("\nNot a prime number");
18.          break; // leave i equal to value that divides num
19.       }
20.       i++;
21.    }
22.
23.    // if i is equal to num, then prior loop finished normally
24.    // meaning that no value between 2 and num-1 evenly
25.    // divided num, meaning that num is prime.
26.
27.    if(i==num)
28.       printf("prime number");
29.
30.    // Stall program to prevent window from closing too quick.
31.    getch();
32. }
33.
This is a case that screams for a for() loop, but that isn't germaine to the discussion.

The OP asked what the break statement did. It breaks out of the innermost loop in which the statement is contained and continues execution with the first statement following the loop. The continue statement simple skips the rest of the statements within the loop body but stays within the loop. This is where a subtle distinction between a for() loop and a while() loop takes place. In a while() loop, the increment statement that is commonly found at the bottom of the loop body will be skipped since it is part of the loop body, but the increment statement in a for() loop will be executed prior to the test being re-evaluated.

killivolt likes this.
16. spinnaker AAC Fanatic!

Oct 29, 2009
5,076
1,176
Determining prime numbers was always used as a perfect way to demonstrate recursion. A little harder to understand but much cleaner than a for loop.

Code ( (Unknown Language)):
1.
2.
3. #include<stdio.h>
4.
5. int isPrime(int,int);
6.
7. int main(){
8.
9.     int num,prime;
10.
11.     printf("Enter a positive number: ");
12.     scanf("%d",&num);
13.
14.     prime = isPrime(num,num/2);
15.
16.    if(prime==1)
17.         printf("%d is a prime number",num);
18.    else
19.       printf("%d is not a prime number",num);
20.
21.    return 0;
22. }
23.
24. int isPrime(int num,int i){
25.
26.     if(i==1){
27.         return 1;
28.     }else{
29.        if(num%i==0)
30.          return 0;
31.        else
32.          isPrime(num,i-1);
33.     }
34. }
35.
36.

17. Jim Hunter New Member

Jan 27, 2013
9
4
Should be:
Code ( (Unknown Language)):
1.
2.        else
3.          [B]return[/B] isPrime(num,i-1);
4.     }
5. }
6.
7.

killivolt likes this.