i dont know how to do this, can anyone help?
Write a C++ program that finds the smallest divisor of a number or determines if that number is a prime number. The program reads a number from the user via the keyboard and invokes a function named find_div() that returns the smallest divisor of n that is greater than 1. Otherwise, if n is a prime number, find_div() will simply return n as prime number. Do you want to search more (y/n) => y
#include<iostream>
using namespace std;
int main()
{
int num=0;
int find_div;
int count=0;
int prime=1;
char ans=' ';
do
{
cout <<"Enter a number that you think is a prime number ";
cin >> num;
for (int i=(num)/2; i>1; i--)
{
if (num%i==0)
{
count=count+1;
if (count==1)
cout <<"divisors are: ";
prime=0;
cout <<i<<" ";
}
}
if (prime==0)
{
cout<<"\nNumber is not prime."<<endl;
}
if (prime==1)
{
cout <<"Number is prime."<<endl;
}
cout<<"Do you want to search more (y/n) ";
cin>>ans;
cin.ignore(200,'\n');
count=0;
prime=1;
system("cls");
}while(ans =='y' || ans=='Y');
system("pause");
return 0;
}
But it showed the factors instead of the smallest divisor.. can anyone revise or give help?
You're getting all the factors of the number, right? Wouldn't the number you are looking for be the largest of those that is greater than 1 and less than the number itself?
@Zhuge, nope i understood wrong, only the smallest divisor
this should be the sample output
Sample output:
Enter a number that you think is a prime number => 1000
2 is the smallest divisor of 1000
Do you want to search more (y/n) => y
Enter a number that you think is a prime number =>997
997 is a PRIME NUMBER.
Do you want to search more (y/n) => n
Press any key to continue…….
what i really dont know right now is how to get the smallest divisor of a number.
Isn't it just the first number that comes as divisble into the number enetered? If you can find prime numbers, you should know that the first number that proves it not prime is the smallest divisor.