Best Prime Example Is Availble in my another post. Kindly check.
http://zakilive.com/2017/05/12/check-the-number-is-prime-or-not-in-c-easy-and-details-explanation/
problem:
Write a program to show prime from 0 to N where N will be taken from user.
code:
#include<stdio.h> int main() { int i,j,count=0,N; printf("Enter N:"); scanf("%d",&N); printf("prime = "); for(i=1; i<=N; i++) { for(j=1; j<=i; j++) { if(i%j==0) count++; } if(count==2) printf("%d\t",i); count=0; } return 0; }
Another Logic:
#include<stdio.h> int main() { int i,j,N,isPrime; while(1){ printf("enter value:\n"); scanf("%d",&N); for(i=2; i<=N; i++) { isPrime=0; for(j=2; j<=i/2; j++) { if(i%j==0){ isPrime=1; } } if(isPrime==0 && N!=1) { printf("%d\t",i); } } printf("\n"); } return 0; }
Another Logic collected from this link:
http://www.techcrashcourse.com/2015/11/c-program-to-print-all-prime-numbers-between-1-to-N.html
Understood the implementation of the code.