Please explain..suppose m=6 then p=2 returns 0 ..p=3 returns 0..p=4 returns 1..what does it return and why?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please explain..suppose m=6 then p=2 returns 0 ..p=3 returns 0..p=4 returns 1..what does it return and why??

int prime(int m){ for (int p=2;p<m;p++){ if (m%p==0){ return 0; } } return 1;}

4th Jun 2018, 1:26 PM
Vikram
Vikram - avatar
4 Answers
0
share your code link
4th Jun 2018, 1:27 PM
‎ ‏‏‎Anonymous Guy
0
if your input is 6 it returns all prime no. from 2 to 6 in your function, if p is divisible by any number, it returns 0 otherwise, it returns 1 if function returns 1, the number i.e prime no. is displayed
4th Jun 2018, 1:37 PM
‎ ‏‏‎Anonymous Guy
0
You need to understand that "%" operator return the rest of the division of a number: 20%2=0 because the division of any even number by 2 results in 0 as rest. The "prime" function in your code checks if "m" is divisible by the numbers, except 0 and 1 ("p" is inialized as 2), up to m-1 (p<m), returning 1 for the ones that aren't (prime numbers). Later, you print all these prime numbers.
4th Jun 2018, 2:05 PM
Pedro dos Santos
Pedro dos Santos - avatar