prime number in another angle

hello everybody,
here a newbie i have this home work about prime number. i should check if a gin“veb number is a prime or not and print the given number is prime and if the given number is not prime it should print "is not a prime is divisible by " and print the smallest number is with.

here is my code:

#include <iostream>

using namespace std;

int prime(int num); // fonction to check a prime number
int i= 0; // an interger uses for division
bool isPrime=true; //this boolean is to check if a number is a prime.

int main( )
{
bool print= true; // this boolean is to use for printing

// this loop ask the user input it takes a boolean if
// the boolean is false it shouldn't print anything

while(print )
{


cout << " insert a natural number (1-100000): ";
int num = 0;
cin >> num;

prime( num);// fonction invocation

if (isPrime && num>0) // here we print the prime number
{
cout <<num <<" is a prime" << endl;
print = false;

}

else if(!isPrime) // here we print non prime number
{

if (num>0 && num < 100000)
{
cout << num <<" is not a prime.is divisible by "<< i << endl;
print=true;
}
}

else //here we print error message
{
cout<<"prime numbers are non-negative "<< endl;
print= true;

}
isPrime= true;
}

the program is working by the problem is it isnot printing the small number which divisible by the user input
please i open to anyone advice
Last edited on
Ok, the structure of this program makes it a bit difficult to follow. Clearly prime() must be setting the global variables "isPrime" and "i" in order for the code to have any chance of working. However global variables should be avoided.

Why don't you write the function int lowest_factor( int num ) which returns the smallest factor of num, or 1 if num is prime? Then all main() has to do is read the number from the user, call lowest_factor on it, and output "prime" if lowest_factor returned 1 or "not prime" if lowest_factor returned anything else.

Your main function should not need any loops.
thank you for reply jsmith , the progrom is compling good without error.
i like your solution but it is a bit different from the fonctionality of the program. the program should work like this if the user enter 3 it should print 3 is a prime, alright and if the user insert a non prime number for example 9 the program should print is divisible by 3.

the problem is if the number is not a prime i cannot find the smallest number that is divisible by the user input

for example if the user insert 22. it should "22 is not a prime." it is divisible by 2 but my program print "22 is not a prime. it is divible by 11" this is my problem
I can't see the code to prime() to see how it is arriving at 11. It could be as simple as looping in the reverse direction or, if it always outputs the highest factor, simply dividing the user number by the highest factor to arrive at the lowest factor.
i m sorry i m so confused myself with this code and i m close to the death line so i have to be able to find what is wrong
here is the function prime ():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int prime(int num)
                         { 
                           int j= 2;
			   while (j <= num)
				{    
                                   
				   if ( num!=j && num % j == 0 )
				         {
                                           isPrime=false;	  
                                            i= j;
			                 } 
                                     j++;
                                 }
                            return i;
			  } 


it is silly some times when someone can sit infront is computer for hour and cannot find the the problem. thank you jsmith i solve it here is the code and it working good :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int prime(int num)
                         { 
                           int j= 2;
			   while (j <= num)
				{    
                                   
				   if ( num!=j && num % j == 0 )
				         {
                                           isPrime=false;	  
                                            i= num/ j;  // i store the smallest number which is divisible by the user input 
			                 } 
                                     j++;
                                 }
                            return i;
			  }

now i trying to avoid the global veriable
Topic archived. No new replies allowed.