In order to print the first 10 prime numbers you need a function to determine if a given value is prime or not. The following algorithm is the standard method of doing so:
1. Let n be the value.
2. If n < 2 then return false.
3. If n is even then return true if n is 2, otherwise return false.
4. Let divisor = 3.
5. If divisor is greater than the square root of n then return true.
6. If divisor is a factor of value then return false.
7. Let divisor = divisor + 2.
8. Go to step 5.
This algorithm can be efficiently implemented in C++ as follows:
bool is_prime (const unsigned n)
{
if (n<2) return false;
if (!(n&1)) return n==2;
const unsigned m = static_cast<unsigned>(std::sqrt (static_cast<double>(n)));
for (unsigned d=3; d<=m; d+=2)
if (!(n%d)) return false;
return true;
}
With this function defined, we can now go ahead and write the complete program:
#include<iostream>
bool is_prime (const unsigned n) {/*as above*/}
int main()
{
std::cout << "First 10 primes:\n";
unsigned primes = 0;
unsigned num = 0;
while (primes < 10)
{
if (is_prime (num))
{
std::cout << num << std::endl;
++primes;
}
++num;
}
}
#include<stdio.h>
#include<math.h>
bool is_prime (const unsigned);
unsigned next_prime (unsigned);
int main (void) {
unsigned count=0, num=0;
printf ("The first 100 prime numbers\n");
while (count<100) {
num = next_prime (num);
printf ("%d\t", num);
++count;
}
printf ("\n");
return 0;
}
unsigned next_prime (unsigned num) {
while (!is_prime (++num));
return num;
}
bool is_prime (const unsigned num) {
if (num<2) return false;
if (!(num%2)) return num==2;
const unsigned max = (unsigned) sqrt (num) + 1;
for (unsigned div=3; div<max; div+=2) if (!(num%div)) return false;
return true;
}
First, you need a function that can determine if a given number is prime or not:
#include<math.h>
bool is_prime (unsigned num) {
if (num < 2) return false;
if (!(num % 2)) return num == 2; // 2 is the only even prime
unsigned max = (unsigned) sqrt ((double) num) + 1;
for (unsigned div = 3; div < max; div += 2) if (!(num % div)) return false;
return true;
}
Next, a function that will return the next prime after a given number:
unsigned next_prime (unsigned num) {
while (!is_prime (++num));
return num;
}
Finally, a function that counts the number of primes in a given range:
unsigned count_primes (unsigned min, unsigned max) {
unsigned count = is_prime (num) ? 1 : 0;
while ((min = next_prime (min)) <= max) ++count;
return count;
}
To count the number of primes in the range 1 to 1,000,000:
#include<stdio.h>
int main (void) {
unsigned count = count_primes (1, 1000000);
printf ("There are %d primes in the range [1:1,000,000]\n", count);
return 0;
}
#include<iostream>
#include<cmath>
bool is_prime (unsigned n) {
if (n<2) return false;
if (!(n%2)) return n==2;
unsigned max {sqrt (n)};
for (unsigned factor {3}; factor<=max; factor+=2)
{
if (!(num%factor)) return false;
}
return true;
}
int main (void)
{
for (unsigned x=100; x<=200; ++x) if (is_prime (x)) std::cout<<x<<std::endl;
}
PRINT 2,3,5,7,11,13,17,19,23,29,31,37
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
#include
find even number in array
you do this 10 print "0112358132134" use the whole of the thing
Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.
This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.
PRINT 2,3,5,7,11,13,17,19,23,29,31,37
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
First, create a for loop from a,1 to 50. Inside of that create another for loop b,2 to a-1. If a/b=int(a/b) then you know it is not prime
You can use int i; for (i = 10; i <= 50; i += 2) {//print i} as a program to print even numbers between 10 and 50.
// simple program to generate first ten prime numbers #include<stdio.h> #include<conio.h> void main() { int c,i,j,n; clrscr(); for(i=2;i<30;i++) { c=0; for(j=2;j<i;j++) { if(i%j==0) {c=c+1; } } if(c==0) printf("%d",i); } getch(); }
first sort the ten numbers in descending order and print the first number. That will be the largest no
/*the program to print prime no from 1 to 300*/ #include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); printf("The prime numbers from 1 to 300 are\n"); for(j=2;j<=300;j++) { for(i=2;i<=j/2;i++) if(j%i==0) break; if(i>j/2) { printf("%d ",j); } } }
10 CLS 20 FOR n = 1 to 10 30 PRINT n, n^2, n^3 40 NEXT n 50 PRINT: PRINT: PRINT "Touch 'x' to go again, any other key to end." 60 INPUT a$ 70 IF a$ = "X" or a$ = "x" THEN 10 80 END
This is a homework question and does not deserve an answer because you will learn nothing other than being lazy.
#include