+ 5

Getting the Output Multiple times..

In this program , the String [ Its Prime ] is shown 111 times . How can i get the output only once if every condition Here is true. https://code.sololearn.com/cCC39TcgxCyi/?ref=app

20th Sep 2018, 6:18 PM
OM KASHYAP
OM KASHYAP - avatar
6 Antworten
+ 7
for i in range(2,a): if a%i==0: print("Its composite") break else: print('Prime')
20th Sep 2018, 6:22 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 5
Thanks Uzair Saeed for your help....
20th Sep 2018, 7:18 PM
OM KASHYAP
OM KASHYAP - avatar
+ 3
you could write it inside a function and make the function return False if the first condition is True. The second condition should be outside of the loop. def primes(num): for i in range(...): if num%i ==0: return False print("prime")
20th Sep 2018, 6:27 PM
davy hermans
davy hermans - avatar
+ 3
Mert Yazıcı ,davy hermans Davy thanks a lot for your help... 👍👍👍💐💐
20th Sep 2018, 6:39 PM
OM KASHYAP
OM KASHYAP - avatar
+ 1
int composite; for (int number=2;number<=113;number++){ composite=0; for (int reps=2;reps<=sqrt(number);reps++){ if (number%reps==0){ composite++; break; }} if (composite==0){ cout<<"Its prime"<<endl; }}
20th Sep 2018, 6:50 PM
Uzair Saeed
Uzair Saeed - avatar
+ 1
Sorry, I can't code in python therefore I did it in c++
20th Sep 2018, 6:55 PM
Uzair Saeed
Uzair Saeed - avatar