Prime Number Program-Best Guide for Prime Number Program with output - Programmer's Mind



What is a prime number??
A Prime number is a natural number, which is evenly divisible only by 1 and itself.
Click here more about Prime Number
Armstrong Number
Strong Number
Perfect Number

The  logic used to check whether the given number is prime or not is as follows

  • Take the Number from the user
  • Count the factors of the Number
  • If the count is 2,then it is a Prime Number
  • Otherwise it is not a Prime Number

Program to check whether the given number is prime or not:

#include<stdio.h>
void main()
 {
   int num,i,count=0;
   printf("Enter Number\n");
   scanf("%d",&num);
   for(i=1;i<=num;i++)
   {
    if(num%i==0)
     {
      count++;
     }
   }
   if(count==2)
   {
    printf("%d is a Prime Number",num);
   }
   else
   {
    printf("%d is not a Prime Number",num);
   }
 }

Output:


Prime Number Program Output
Another fact about prime numbers is that except 2 and 3,every prime number leaves remainder 1 or 5 when it is divided by 6.But Every number which leaves remainder 1 or 5 when divided by 6 is not a Prime.Let us construct a program to check whether prime numbers leaves remainders (1 or 5) or not.For proof click here

Logic for this program

  • Take the number
  • Divide the number by 6
  • Print the Remainder

Program:

#include<stdio.h>

void main()

 {
   int num,rem;
   printf("Enter Number\n");
   scanf("%d",&num);
   rem=num%6;
   printf("Remainder is %d",rem);
 }

Output:

Prime Number Remainder













Check for different prime numbers and observe the remainders.As i mentioned earlier,some other numbers also leaves remainders 1 or 5.In above output,55 and 65 are not primes but leaves remainders 1 and 5 respectively.

Related Posts:



Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment