• Any number which is divisible by 1 and itself is known as prime number.
  • To check prime number or not in c programming we need to use for loop and iterate from 2 to half of the number.
  • If any number is divisible then it is non prime number, we can exit the loop.
  • Now we need to check prime numbers from 1 to 100 or 1 to n.
  • Logic is same and we are adding one more for loop for 1 to N numbers.
  • Now we will iterate for loop from 1 to n and check prime or nor for each number.
  • Lets see an example program on c to check prime numbers from 1 to N. 


 Program #1 : Write a c program to check prime numbers from 1 to N by using for loop.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i,j, count = 0;
  5.  
  6.     printf("Enter a Number");
  7.     scanf("%d",&n);
  8. for(i=2;i<=n;i++)
  9.  {
  10.   for(j=2;j<i;j++)
  11.   {
  12.    if(i%j==0)
  13.    break;
  14.    else if(i==j+1)
  15.    printf("%d\n",i);
  16. }
  17. }
  18.  
  19.     getch();
  20.     
  21. }

 Output:


prime numbers from 1 to n in c

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu