To check if the number is prime or not. Code: /* To check if an integer is prime or not */ #include<iostream.h> #include<conio.h> void main() {[INDENT] int num,i; clrscr(); cout<< "Please enter the number "; cin >> num; i =2 ; while(1){[INDENT] if (num%i==0) {[INDENT] if(num == i){[INDENT] cout << "Prime Number"; break; [/INDENT][/INDENT][INDENT] } else {[INDENT]cout << "Not Prime"; break; [/INDENT]} [/INDENT]} else {[INDENT] i++; [/INDENT]} [/INDENT]} [/INDENT]}
Yes....the actual is the square root of i. You can use the standard function sqrt(); for that purpose.
A simple program is here. to chech a number is prime or not. Code: void main() { int a,b,i; cout<<"\n enter the no: "; cin>>a; for(i=2;i<=a;i++) { rem=a%i; if(rem==0) { cout<<"\nNo is not prime: "; } else { cout<<"\nNo is prime:"; } } }