473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

finding divisors of a all numbers less than a given num

Hi,
I have to find the divisors of all numbers less than a number inputed by the user and state if a number is prime if no divisors. I have this code so far, but I'm getting the wrong output and not sure how to list all the numbers less than the input number. Please help.

#include <iostream>
using namespace std;

// function declarations
// -------------------
bool isPrime (int);



// main function
// -------------------

int main ()
{
int userNum, divisor;
bool prime;
cout << "Please enter an integer between 10 and 100:";
cin >> userNum;
if(userNum < 10 || userNum > 100)
{
cout << "Please enter an integer between 10 and 100:";
}
else

for (int counter = 2; userNum > counter; counter ++)
{
if (userNum % counter == 0)
{
cout << userNum << "'s divisors are:" << counter;
}
}




isPrime(userNum);
prime = isPrime(userNum);
if (prime == 1)
{
cout << userNum << " is a prime number" << endl;
}


return 0;
}

// function definitions
// -------------------


bool isPrime(int userNum)
{
for(int counter = 2; counter < userNum; counter++)
{
if(userNum % counter ==0)
return false;
}
return true;
}
Feb 9 '07 #1
2 2001
ok I have the programming right, I just need to fix the output to look pretty like
16's divisors are: x, y z
15's divisors are:....
and so on.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // function declarations
  5. // ------------------- 
  6. bool  isPrime (int);
  7.  
  8.  
  9.  
  10. // main function
  11. // -------------------
  12.  
  13. int main ()
  14. {
  15.     int userNum;
  16.     bool prime;
  17.     cout << "Please enter an integer between 10 and 100:";
  18.     cin >> userNum;
  19.     if(userNum < 10 || userNum > 100)
  20.     {
  21.         cout << "Please enter an integer between 10 and 100:";
  22.     }
  23.     else
  24.     while (userNum >= 1)
  25.     {
  26.      userNum--;
  27.      for (int counter = 2; userNum > counter; counter++)
  28.         {
  29.             if (userNum % counter == 0)
  30.                 {
  31.                     cout << userNum << "'s divisors are:" << counter << endl;
  32.                 }
  33.             else
  34.             isPrime(userNum);
  35.             prime = isPrime(userNum);
  36.             if (prime == 1)
  37.             {
  38.                 cout << userNum << " is a prime number" << endl;
  39.             }
  40.         }
  41.     }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.     return 0;
  50.     }
  51.  
  52. // function definitions
  53. // -------------------
  54.  
  55.  
  56. bool isPrime(int userNum)
  57. {
  58.     for(int counter = 2; counter < userNum; counter++)
  59.     {
  60.         if(userNum % counter ==0)
  61.             return false;
  62.     }
  63.     return true;
  64. }
Feb 9 '07 #2
Ganon11
3,652 Expert 2GB
At the beginning of the for... loop, outpu8t a statement like "The number xxx ", but without an endl or newline character. Next, check if it is prime. If it is prime, print out "is prime." and endl - then move to the next number. If the number isn't prime, then proceed with your calculations to find divisors. Every time you find one, print that divisor out - but without an endl. When you have found every divisor, print a newline, and move on to the next number.
Feb 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Philp Smith | last post by:
Hi Does anyone have suggested code for a compact, efficient, elegant, most of all pythonic routine to produce a list of all the proper divisors of an integer (given a list of prime...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
23
by: Arnaud Delobelle | last post by:
Hi all, I want to know the precision (number of significant digits) of a float in a platform-independent manner. I have scoured through the docs but I can't find anything about it! At the...
25
by: Subra | last post by:
Hi, What is the best way to find the 1000 largest numbers from the file having hell lot of entries ? Can you please help me to find out the way ? Do I need to go for B+ trees ?? Please help,...
1
by: Clint Boaz | last post by:
I am just trying to learn C++ and having a problem trying to write a program that finds the sum of the proper divisors of a given integer n and returns whether n is deficient, perfect or abundant.
2
by: arnuld | last post by:
Again, my friend, who does not much access to internet, has given me a working program and I need your views on improvement and design. I put some error checking but my experience with C has made...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.