Respuesta :
Answer:
#include <iostream>
using namespace std;
int main() {
int k;
cin>>k;
cout<<"2 "; //printing the first prime number.
for(int i=3;i<=k;i++)
{
int divisor=2;//divisor to check the prime numbers.
while(divisor<i)
{
if(i%divisor==0)//if i is divisible by divisor then it is not prime.
{
break;
}
divisor++;
}
if(divisor==i)
{
cout<<divisor<<" ";
}
}
return 0;
}
Input:-
24
Output:-
2 3 5 7 11 13 17 19 23
Explanation:
The above-written code is in c++.It prints all the prime numbers upto the integer entered k.To achieve this I have nested loops for and while.For loop runs over 3 to n because 2 is a prime number so we are printing it beforehand.For every value of the for loop.It will be checked it is prime or not.If it is prime then the integer is printed else it is not printed.
Answer:
#section 1
def is_prime_v1(n):
for d in range(2, n):
if n % d == 0:
return False
return True
#section 2
while True:
try:
num = int(input('Enter a number greater than 2: '))
if num > 2:
break
else:
raise
except:
print('{} is not greater than 2'.format(num))
#section 3
for n in range(3, num):
is_prime_v1(n)
a=is_prime_v1(n)
if a==True:
print (n)
Explanation:
The programming language used is Python 3.
#section 1
In this section we create a function that will check if a number is a prime number. The function returns True for prime numbers and False for normal numbers.
#section 2
In this section we make use of a WHILE loop and a try and except block to ensure that the input by the user is greater than two, if the input is less than 2, an exception is raised and the except block prompts the user to enter the appropriate value.
when the appropriate value is entered the loop breaks and the value is carried to the last section.
#section 3
In this final section, the value collected from the user in #section 2, is inserted as the upper-limit in a range function with the lower limit set at 3.
The function created in #section 1 is called and each value in the range is passed to it and an IF statement checks if the output is True or False. If the IF block evaluated to be True, it implies that the number is a prime number and the result is printed to the screen.