Prime Number Program in C using For loop and While loop

Prime Number Program in C

Prime Number: A number that is divisible only by itself and 1 is called Prime number.
Examples: 2, 3, 5, 7, 11, 13, 17, 19 etc.
Prime number programs in c:
Program to display the prime numbers from 1 to 100:
#include<stdio.h>
 int main()
 {
  int limit = 100;
  printf("Prime numbers between 1 and 100");
      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);
  }
}


Output: Prime numbers between 1 and 1002 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Program to display prime numbers from 1 to n:

#include<stdio.h>
int main()
{
 int n;
 printf("Enter the value of N");
 scanf("%d", &n);
    for(int i=2; i < n; i++)
     {
      int flag = 0;
       for(int j=2; j < i ; j++)
        {
         if(i % j == 0)
         {
           flag = 1;
            break;
          }
         }
     if(flag == 0)
    printf("%d ", i);
   }
}

 


output:
Enter the value of N 50                                                                                                               
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Prime number using while loop:
#include<stdio.h>
int main()
 {
 int n,i = 2,flag;
 printf("Enter the value of N");
 scanf("%d", &n);
 flag = 0;
 while(i <= n/2)
 {
  if(n % i == 0)
  {
   flag = 1;
   break;
  }
 i++;
 }
 if(flag == 0)
        printf("Prime Number");
    else
        printf("Not Prime Number");
    return 0;
}


Enter the value of N 10                                                                                                               
Not Prime Number 


Also Read: Java strings interview questions Java collections interview questions Java exception handling interview questions Java multithreading interview questions Java io streams interview questions Core Java interview questions Java constructor interview questions USPS Tracking USPS Hold Mail   Liteblue Liteblue login Earthlink Webmail  RCN Webmail BU Webmail Towson University Webmail Gmail login Shaw Webmail Cox Email GoDaddy Webmail Walmart Credit Card Login TCS Webmail bookzz 192.168.0.1 liteblue usps gov human resources Hotmail login WalmartOne Login upsers outlook webmail Factorial program in Java constructor in java switch case in java java data types java if else Gmail sign in Gmail Gmail sign up AOL Mail login PSU Webmail Juno Webmail Rackspace Webmail Webmail DQFanSurvey Tellpopeyes survey Kroger feedback Disney hub tiktok login Netflix login
Share:

Search This Blog

  • ()

Our Services

Powered by Blogger.

Netflix login and sign in and password change here.

Netflix login  or Login Information Here this Article is about the  Netflix login  and Sign up and login problems. The Easy Way to Logi...