C Program to Print Prime Numbers from 1 to N

C Program to Print Prime Numbers from 1 to N

C program to print prime numbers from 1 to N; Through this tutorial, we will learn how to print prime numbers from 1 to N(10, 100, 500, 1000, etc) in the c program using for loop, while loop, and recursion.

Programs to Print Prime Numbers from 1 to N in C

Let’s use the following program to print prime numbers from 1 to N (10, 100, 500, 1000, etc) in c:

  • C Program to Print Prime Numbers from 1 to N Using For Loop
  • C Program to Print Prime Numbers from 1 to N Using While Loop
  • C Program to Print Prime Numbers from 1 to N Using Recursion

C Program to Print Prime Numbers from 1 to N Using For Loop

#include<stdio.h>
void main()
{
    int i,j,n;  
    printf("Enter the number till which you want prime numbers :- ");
    scanf("%d",&n);
    printf("Prime numbers are:- ");    
    for(i=2;i<=n;i++)
    {
        int c=0;
        for(j=1;j<=i;j++)
        {
            if(i%j==0)
            {
                c++;
            }
        }    
       if(c==2)
        {
            printf("%d ",i);
        }
    }
}

The output of the above c program; as follows:

Enter the number till which you want prime numbers :- 20
Prime numbers are:- 2 3 5 7 11 13 17 19 

C Program to Print Prime Numbers from 1 to N Using While Loop

#include <stdio.h>
int main()
{
  int i, Number = 1, count, n; 
  
  printf("Enter the number till which you want prime numbers :- ");
  
  scanf("%d", &n);
  
   printf("Prime numbers are:- ");  
   
  while(Number <= n)
  {
    count = 0;
    i = 2;
    while(i <= Number/2)
    {
      if(Number%i == 0)
      {
        count++;
	break;
      }
      i++;	
    }	
    if(count == 0 && Number != 1 )
    {
       printf(" %d ", Number);
    }
    Number++; 
  }
  return 0;
}

The output of the above c program; as follows:

Enter the number till which you want prime numbers :- 20
Prime numbers are:- 2 3 5 7 11 13 17 19 

C Program to Print Prime Numbers from 1 to N Using Recursion

#include<stdio.h>
#include<math.h>
int CheckPrime(int i,int num)
{
    if(num==i)
        return 0;
    else
        if(num%i==0)
            return 1;
    else{
        return CheckPrime(i+1,num);
    }
}
int main()
{
    int n,i;
    printf("Enter the N Value:");
    scanf("%d",&n);
    printf("Prime Number Between 1 to n are: ");
   for(i=2;i<=n;i++)
    if(CheckPrime(2,i)==0)
        printf("%d ",i);
}

The output of the above c program; as follows:

Enter the N Value:20
Prime Number Between 1 to n are: 2 3 5 7 11 13 17 19 

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *