Search C Program | nimishsoft@gmail.com

Prime Number using while loop

/* Prime Number using while loop */

#include<stdio.h>
int main()
{
    int a,i,f;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=0;
    i=2;
    while(i <= a/2)
    {
        if(a%i == 0)
        {
            f=1;
            break;
        }
        i++;
    }
    if(f==0)
        printf("Prime Number")
    else
        printf("Not Prime Number");
    return 0;
}

Related Post
Prime number using for loop
http://cbasicprogram.blogspot.in/2012/03/prime-number-using-for-loop.html

27 comments:

  1. Replies
    1. Attention everyone, the file provided above is not working. I have been looking for the working file and finally found it.



      ✔️Click Here To Download http://gestyy.com/e0GAyS
      ✔️Click Here To Download

      ✔️Click Here To Download http://exe.io/XONVsO6l

      ✔️Click Here To Download


      ✔️Click Here To Download
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .

      .

      .
      .

      .

      .
      .
      .
      .

      Mn.
      ..

      .
      .
      .
      ..
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      ..
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .ds
      .
      .
      .
      .sdcccxxx

      Delete
  2. thnks plz tell me another logic of prime number

    ReplyDelete
    Replies
    1. #include
      #include
      void main()
      {
      int i,num;

      printf("Enter a Number-\n");
      scanf("%d",&num);

      i=2;
      while(i<=num-1)
      {
      if(num%i==0)
      {
      printf("Not a Prime Number!\n");
      break;
      }
      i++;
      }
      if(i==num)
      printf("Prime Number!\n");
      getch();
      }

      Delete
    2. if num=2
      then
      while(i<=num-1)
      2<=1..this condition is false
      then how it proceeds to prime number ???

      Delete
  3. Replies
    1. Why there is num-1 in the while loop plz explain

      Delete
    2. bcz prime no. is a no. which is divided by 1 and itself

      Delete
  4. #include
    int main()
    {
    int n,i;
    printf("Type the number:");
    scanf("%d",&n);
    i=2;
    while (i<(n/2))
    {
    if (n%i==0)
    {
    printf("%d is not a Prime Number",n);
    break;
    }
    i=i+1;
    }
    if (i>=(n/2))
    {
    printf("%d is a Prime Number",n);
    }
    return 0;
    }

    ReplyDelete
  5. this is not acceptible using number 3

    ReplyDelete
  6. Why a/2 i cant understand that please tell me

    ReplyDelete
    Replies
    1. bcoz to check if a number is divisible, we just need to check to its half.
      like 11, if you can not divide it from 2-5 it is prime, 6-10 are not going it divide it anyway

      Delete
    2. please anyone give output of this program

      Delete
  7. #include
    using namespace std;

    int main()
    {
    int start=1;
    int num;
    cout<<"Enter the number to check if it's prime or not? "<>num;
    int counter=0;

    while(start<=num)
    {
    if(num%start==0)
    {
    counter++;
    }
    start++;
    }
    if(counter==2) //if number has only 2 divisors( 1 and number itself)
    cout<<num<<" is Prime number"<<endl;
    else
    cout<<"Not a prime number"<<endl;

    system("pause");
    }

    ReplyDelete
    Replies
    1. can anyone tell what is function of counter in this function

      Delete
  8. I want to buy your blog please text me or call me hitesh.xc@gmail.com 8076671483

    ReplyDelete
  9. I have found amazing article on prime number program in C with different examples with outputs like while, for loops, 1-n, nth numbers and between two intervals.

    ReplyDelete