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;
}
}
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
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
Oh, what a lovely request! In FoxPro, you can create a program to print all prime numbers from 1 to 100 by using a loop to check each number for divisibility only by 1 and itself. If it meets this criteria, you can print it out on the screen. Remember, every number is unique and special, just like a happy little tree in a vast forest.
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
#include
This is a homework question and does not deserve an answer because you will learn nothing other than being lazy.