+ 2

How to print all the prime numbers from 1 to 10 and from 3 to 5 or n to m which will be input "testcase" times?

2 3 5 7 If input 1 to 10. 3 5 If input 3 to 5

21st Nov 2018, 8:24 AM
The Coder(Abeer)
The Coder(Abeer) - avatar
7 odpowiedzi
+ 11
Have you tried it doing by yourself? If so, can you show us your attempt?
21st Nov 2018, 9:52 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 5
How to? Run a for loop "testcase" times, and in that for loop get user input for 'n' and 'm' in each iteration. Then you can have a nested for loop iterating from 'n' to 'm' in which you can test each number whether it is prime or not and print it to the screen if it is a prime number. This is how you can theoretically do it. If you need further help with a practical code, please show us your own attempt on the task first.
21st Nov 2018, 9:01 AM
Shadow
Shadow - avatar
+ 3
It's not about understanding the task for us. I presented you a way how to solve it, a structure you can follow. But we don't want to write the entire code for you. That is not our job. We will gratefully help you if you are stuck while writing the code, but that requires you at least trying to solve it before asking for help. So please show us an attempt of yours that we should help you with. Or make your question more specific on what exactly you don't understand.
21st Nov 2018, 10:02 AM
Shadow
Shadow - avatar
+ 2
Well, go to this website to understand this problem better: spoj.com/problems/PRIME1
21st Nov 2018, 9:56 AM
The Coder(Abeer)
The Coder(Abeer) - avatar
+ 1
Here's is my first attempt, but it didn't work: #include <bits/stdc++.h> using namespace std; int main() { int t,testcase; cin>>testcase; for(t = 1; t <= testcase; t++) { int n,m; cin>>n>>m; for(;int a = 2; n <= m && a <= m; n++,a++) { if(n%1 == 0 && n%n == 0 && n%a != 0) { cout << n << endl; } } } }
21st Nov 2018, 1:22 PM
The Coder(Abeer)
The Coder(Abeer) - avatar
+ 1
You had some minor issues in the code that I pointed out in the refactored code, but your main problem is your prime checking. I reworked your code for you and explained it there in detail in the comments you should read through. If you have any more questions, for example regarding the logic, please feel free to ask. Here is the rewritten code: https://code.sololearn.com/crFbp42pEWRM/?ref=app
21st Nov 2018, 10:24 PM
Shadow
Shadow - avatar
0
Thank you.
22nd Nov 2018, 12:29 PM
The Coder(Abeer)
The Coder(Abeer) - avatar