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

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
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();
}
 

killivolt

Joined Jan 10, 2010
836
Rich (BB code):
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:

5 == 5; is true
5 == 4; is false

int x = 5;
x == 34; is false

x == 35 - 30; is true

The reason it is == and not just = is because = is the assignment operator.

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:

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
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????
 

thatoneguy

Joined Feb 19, 2009
6,359
% 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.

More info
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 

spinnaker

Joined Oct 29, 2009
7,830
% 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.
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");
 

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
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.
 

killivolt

Joined Jan 10, 2010
836
% 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.
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!
 

thatoneguy

Joined Feb 19, 2009
6,359
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");

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.
 

spinnaker

Joined Oct 29, 2009
7,830
Maybe easier to see had the op used code tags.

Rich (BB code):
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();
}
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
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?
 

spinnaker

Joined Oct 29, 2009
7,830
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.
The break statement causes the while loop to terminate "prematurely".
 

WBahn

Joined Mar 31, 2012
33,119
First, let's get the code in CODE tags to make it readable:

Rich (BB code):
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();
}
Now, here's a novel idea, let's comment the code!

Rich (BB code):
int main()
{
   int i,num;
   printf("enter a number:");
   scanf("%d",&num);

   // Check each value, i, between 2 and num-1 to 
   // see if any of them evenly divide num.
   // If none of them do, then i will end up being equal to num.

   i=2;
   while(i<=num-1)
   {   
      if(num%i==0) // if True, then i divides num
      {
         printf("\nNot a prime number");
         break; // leave i equal to value that divides num
      }
      i++;
   }
   
   // if i is equal to num, then prior loop finished normally
   // meaning that no value between 2 and num-1 evenly
   // divided num, meaning that num is prime.

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

   // Stall program to prevent window from closing too quick.
   getch();
}
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.
 

spinnaker

Joined Oct 29, 2009
7,830
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.


Rich (BB code):
#include<stdio.h>

int isPrime(int,int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num,num/2);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num,int i){

    if(i==1){
        return 1;
    }else{
       if(num%i==0)
         return 0;
       else
         isPrime(num,i-1);
    }
}
 
Top