+ 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;}
4 Antworten
0
share your code link
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
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.