This blog is under construction

Saturday, 22 June 2013

C program to find whether the given number is prime or not

Write a C program to check whether the given number is prime or not.


  #include <stdio.h>
  int main() {
        int data, i, flag = 1;
        printf("Enter ur input:");
        scanf("%d", &data);
        /* prime no is greater than 1 and it is divisible by 1 and itself */
        if (data <= 1) {
                printf("Not a prime number\n");
                return 0;
        }

        for (i = 2; i <= data - 1; i++) {
                if (data % i == 0) {
                        flag = 0;
                        break;
                }
        }

        if (!flag)
                printf("Not a prime number\n");
        else
                printf("Prime Number\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter ur input:89
  Prime Number



No comments:

Post a Comment