i=2 rem=1 echo "Enter a number" read num if [ $num -lt 2 ] then echo "$num is not prime" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ] do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ] then echo "$num is prime" else echo "$num is not prime" fi
You need two utility functions. The first determines if a given number is prime or not. The second finds the next prime after a given number. The following function can be used to determine if a given integer is prime: bool is_prime (const unsigned num) { if (num<2) return false; if (0==(num%2)) return num==2; unsigned max_factor = (unsigned) sqrt ((double) num) + 1; unsigned factor; for (factor=3; factor<max_factor; ++factor) if (0==(num%factor)) return false; return true; } The following function can be used to determine the next prime after the given integer: unsigned next_prime (unsigned num) { while (!is_prime (++num)); return num; } Now you can print a series of primes using the following: int main (void) { unsigned num=1; while (num<10000) { num = next_prime (num); printf ("%d is prime\n", num); } return 0; }
7 is one prime number between 2 and 10.
#include <iostream> bool isPrime(int p) { if( p<=1 ( p>2 && p%2==0 ))return( false );int max = (int)sqrt(( double ) p ) + 1;for( int i=3; i<=max; i+=2 )if( p%i==0 )return( false );return( true ); } int main() { int primes[10]; int count=0, num=0; while( count!= 10 ) {num+=num>2?2:1;if( isPrime(num) )primes[count++]=num;}for( int i=0; i<10; ++i )std::cout << primes[i] << std::endl;return( 0 ); }
The prime numbers between 1 and 10 are 2, 3, 5, and 7.
/*Determine prime numbers between 1 to 50*/ #include<stdio.h> #include<conio.h> main() { int num, i ,n; clrscr(); printf ( "\nEnter a number limit" ) ; scanf ( "%d", &n ) ; for ( num=2; num <= 50; num++ ) { for ( i=2; i <= num - 1; i++ ) { if ( num % i == 0 ) { Break; continue; } } if (num==i) printf(\n The prime number=%d", num); continue; } getch() }
i=2 rem=1 echo "Enter a number" read num if [ $num -lt 2 ] then echo "$num is not prime" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ] do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ] then echo "$num is prime" else echo "$num is not prime" fi
You do not need OOP for this because the first 100 prime numbers are all integral types. All you need is a function to determine if a given integral is prime or not, and another to find the next prime after a given integral: bool is_prime (const unsigned int num) { unsigned factor, max_factor; /* anything less than 2 is non-prime */ if (num<2) return false; /* 2 is the only even prime */ if (!(num%2) return num==2; /* determine largest potential factor of num (square root of num)*/ max_factor = sqrt (num); /* test each odd factor from 3 onwards */ for (factor=3; factor<=max_factor; factor+=2) { if (!(num%factor)) return false; /* num is non-prime */ } return true; /* the number is prime */ } unsigned next_prime (unsigned int num) { /* increment num until num is prime */ while (!is_prime (++num)); return num; } /* test the functions */ int main (void) { unsigned num, count; count=0; num=0; printf ("Printing the first 100 prime numbers:\n"); while (count<100) { num=next_prime (num); printf ("%d is prime\n", num); ++count; } return 0; }
You need two utility functions. The first determines if a given number is prime or not. The second finds the next prime after a given number. The following function can be used to determine if a given integer is prime: bool is_prime (const unsigned num) { if (num<2) return false; if (0==(num%2)) return num==2; unsigned max_factor = (unsigned) sqrt ((double) num) + 1; unsigned factor; for (factor=3; factor<max_factor; ++factor) if (0==(num%factor)) return false; return true; } The following function can be used to determine the next prime after the given integer: unsigned next_prime (unsigned num) { while (!is_prime (++num)); return num; } Now you can print a series of primes using the following: int main (void) { unsigned num=1; while (num<10000) { num = next_prime (num); printf ("%d is prime\n", num); } return 0; }
If you just want a hint: One way to check whether a number is prime is by dividing it by any number between 2 and the square root of your number. If the number divides by any of these, it is not prime. If you want the code: import math for num in range(1,101): if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)): print num
i=2 rem=1 echo -e "Enter a number: \c" read num if [ $num -lt 2 ]; then echo -e "$num is not prime\n" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ]; then echo -e "$num is prime\n" else echo -e "$num is not prime\n" fi
int number; int i=2; while (i<number) { if(number%i==0) { printf("Not a prime no."); break; } else printf("number entered is prime"); getch(); }
(defun prime (num) (if (< 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (<= chk-to dividend) (return t))) t))
#include<conio.h> #include<stdio.h> main() { int num,n,i,prime; printf("Enter any number: "); scanf("%d", &num); for(n=2; n<=num; n++) { for(int i=2; i<n; i++) { if(n%i==0) { prime=0; break; } prime=1; } if(prime) printf("\t%d",n); } getch(); }
#include<iosys.h> #include<math.h> // for sqrt() bool is_prime (unsigned num) { if (num<2) return false; // 2 is the first prime if (!(num%2)) return num==2; // 2 is the only even prime // largest potential factor is square root of num unsigned max = (unsigned) sqrt ((double) num) + 1; // test all odd factors for (unsigned factor=3; factor<max; factor+=2) if (!(num%factor)) return false; return true; // if we get this far, num has no factors and is therefore prime } int main (void) { // test all nums from 0 to 100 inclusive for (unsigned num=0; num<=100; ++num) { if (is_prime (num)) printf ("%d is prime\n", num); else if (num>0) printf ("%d is composite\n", num); else printf ("%d is neither prime nor composite\n", %d); } return 0; }
#include#includebool is_prime(unsigned num) {unsigned max, factor;if (num
7 is one prime number between 2 and 10.