0

Here is what I have so far. I need to specifically add a loop and a counter for the program to print out the first 50 prime numbers. I know I need to get rid of the initial input but I am confused on how to add in the loop and a counter to check and see how many primes I have. Any info would help greatly, I am totally new to programming.

num = int(input("enter a number: "))

primeFlag = True
n = 2
for n in range(2,num):
    if (num % n == 0):
        primeFlag = False

if primeFlag:
    print(" %d is prime" % num)

ombk
  • 2,036
  • 1
  • 4
  • 16
  • 4
    Does this answer your question? [To find first N prime numbers in python](https://stackoverflow.com/questions/1628949/to-find-first-n-prime-numbers-in-python) Specifically [this answer](https://stackoverflow.com/a/1629447/4518341) – wjandrea Nov 27 '20 at 02:47

2 Answers2

0
print("Prime numbers: ")
total = 0
i = 2
while True:
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        total += 1
        print(i)
        if total == 50:
            break
    i += 1

print("Total :", total)

try this code.

Output:

Prime numbers: 
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
Total : 50
wjandrea
  • 28,235
  • 9
  • 60
  • 81
sourab maity
  • 1,025
  • 2
  • 8
  • 16
  • I made a few improvements. You can check the [revision history](https://stackoverflow.com/posts/65031425/revisions) to see what changed. – wjandrea Nov 27 '20 at 03:04
  • yes, i am also edited in pycharm same code,when i come there to edit my post i saw that you already edited. thanks for help – sourab maity Nov 27 '20 at 03:07
-1

You can create a counter i and increase it by one (i += 1) when primeFlag is true, when you print.

Karla
  • 1
  • 1