dimanche 8 mars 2015

C++ What does return function do after if statements in loop?-Prime Test

#include <iostream>


#include <cmath>


using std::cin;


using std::cout;


using std::endl;


int prime(int n){//primetest function



`int i;`
`if(n == 1) return 0;//zero- not prime`
`if(n == 2) return 1;//one- prime`
`for(i= 2; i <= ceil(sqrt(n)); i++){`
`if(n % i == 0) return 0; //remainder is zero}`
`return 1;}`


int main(){



`unsigned long n;`
`while(cin >> n){`
`cout << prime(n) << endl;}`

`return 0;}`


// I know that 0 means not prime and 1 means prime. Can someone please explain how are all the return functions used to accomplish this? Why not use cout << "0" for not prime and cout << "1" for prime?


Thank you. I am new to C++


Aucun commentaire:

Enregistrer un commentaire