They are: 2 3 5 and 7
Chat with our AI personalities
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.