Prime Number Program in C (or) Prime Number in C.

Prime Number Program in C (or) Prime Number in C

Prime Number: A number that is divisible only by itself and 1 is called a Prime number.

Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.


Prime Number Program in C (or) Prime Number in C.


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

}
Output:
Enter a number: 7

Entered number is Prime Number.

Explanation of the above program: 

  1. If the while loop condition (j <= i/2) is true then loop enters into while loop and check the if condition (i%j == 0). 
  2. If the condition is true the Entered number is not the prime number. 
  3. If the condition is False, then the number entered is the Prime number.

Prime Number Program in C (or) Prime Number in C.

Prime number using for loop:


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

}
Output:
Enter a number: 7
Prime Number

Explanation of the above program: 

  1. If the For loop condition (j <= i/2) is true then loop enters into for loop and check the if condition (i%j == 0). 
  2. If the condition is true the Entered number is not the prime number.
  3.  If the condition is False, then the number entered is the Prime number.

Prime Number Program in C (or) Prime Number in C.

Prime number program to print from 1 to 100.

C program to print numbers from 1 to 100:
#include<stdio.h>
int main()
{
  int limit = 100;
    printf("Prime numbers between 1 and 100 are: ");
    for(int i = 2; i < limit; i++)
    { 
     int flag = 0;
     for(int j=2;j < i;j++)
      {
             if(i%j==0)
              {
                 flag = 1;
                 break;
             }
        }
         if(flag ==0)
             printf("%d ",i);
    }
   return 0;
}
Output:
Prime numbers between 1 and 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.

Explanation of the above program: 

  1. If the For loop condition (i <= limit) is true then loop enters into for loop and check the if condition (i%j == 0). 
  2. If the condition is true the Entered number is not the prime number.
  3.  If the condition is False, then the Prime number is printed up to the given limit. When for loop condition (101 <=100) then the program terminates.

Prime Number Program in C (or) Prime Number in C.

Prime number program to print from 1 to N.

C program to print numbers from 1 to N:
#include<stdio.h>
int main(){
  int i,j,count,k;
    printf("Enter max range: ");
    scanf("%d",&k);
    for(i = 1;i<=k;i++)
     {
         count = 0;
         for(j=2;j<=i/2;j++){
             if(i%j==0){
                 count++;
                 break;
             }
        }
         if(count==0 && i!= 1)
             printf("%d ",i);
    }
   return 0;

}
Output:
Enter max range: 70

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67.

Explanation of the above program: 

  1. First, the user should give the range value from the keyboard. the value is stored in the variable "k". 
  2. If the For loop condition (i <= limit) is true then loop enters into for loop and check the if condition (i%j == 0). 
  3. If the condition is true the Entered number is not the prime number.
  4.  If the condition is False, then the Prime number is printed up to the given limit. When for loop condition (101 <=100) then the program terminates.

Read also: 

Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive