Prime numbers upto 100

What is a prime number ?
Integer Greater than 1
Its only factor is 1 and itself.

It should not be divisible ( with reminder equal to 0 ) by any number less than it and greater than 1.
We will be using fmod() or % operator to get reminder of any division.

First we will use while loop to display all the number between 2 and our given number ( 100 ).
while (i<=my_num){
// statements 
}
We will use one flag and set the value to 0 at the beginning of each loop.
while (i<=my_num){
 flag=0;
}
By using a for loop we will divide each number by all number starting from 2 and ending one less than the number.
 for(j=2;j<=i-1;j++){
    if(i % j == 0 ){
        flag=1;
        break;
    }
 }
At any point inside the for loop if the reminder of division became 0 then we will exit the for loop and set the flag to 1 . That will indicate that the number is not a prime number.
Before starting the next loop we will reset the flag to 0 again as shown above. We used one if condition to check the flag value and display the message if it is equal to 0;
Here is the complete code
#include<stdio.h>
#include<string.h>
int main()
{
 int my_num=100;
 int i=2,j;
 int flag=0;
while (i<=my_num){
 flag=0;

 for(j=2;j<=i-1;j++){
    if(i % j == 0 ){
        flag=1;
        break;
    }
 } // end of for loop
 if(flag==0){
    printf( " %d is a prime number  \n",i);
 }
 i++;
} // end of while loop 
return 0;
}
Numbers other than Prime numbers will have factors , we can list the factors of a given number.

Factors of a input number

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer