# How to display numbers that input number is divisible with?

Page 1 of 1

## 8 Replies - 525 Views - Last Post: 20 February 2013 - 06:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312520&amp;s=a66dca3ae1e2683ad908b41f67aff671&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 needhelpest

Reputation: 0
• Posts: 40
• Joined: 12-January 13

# How to display numbers that input number is divisible with?

Posted 17 February 2013 - 12:37 PM

Hello, for the first time I have a concrete question about my program.I need to write a program which after inputting a number will provide the answer whether it is prime number and if it isnt, the numbers that the input number is divisible with.
Example:
4 is divisible with 1,2 and 4.
My code so far:
```#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int P,count=0;
cout<<"Sisestage arv:\n"; /*Asks for user input*/
cin>>P;                    /* User input P*/
for(int a=1;a<=P;a++)
{
if(P%a==0)
{
count++;
}
if(count==2)
{
cout<<"Algarv.\n";  /* Provided option when the number is prime number*/
}
else
{
cout<<" Ei ole algarv \n"; /* This is where I am supposed to provide the numbers input is divisible with*/
}
getch();
}
}
```

Is This A Good Question/Topic? 0

## Replies To: How to display numbers that input number is divisible with?

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3459
• Posts: 12,299
• Joined: 08-August 08

## Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 01:52 PM

if(P%a==0) and a is not equal to 1 or P then it's not prime. Why are you increasing count?

### #3 needhelpest

Reputation: 0
• Posts: 40
• Joined: 12-January 13

## Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 02:15 PM

CTphpnwb, on 17 February 2013 - 01:52 PM, said:

if(P%a==0) and a is not equal to 1 or P then it's not prime. Why are you increasing count?

You need it for it to be mathematically correct,try to run it without count.

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3459
• Posts: 12,299
• Joined: 08-August 08

## Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 02:21 PM

Huh? What do you mean by mathematically correct?

A brute force method of determining if a number is prime is to check if it is devisable by any number smaller than it but greater than 1.

### #5 needhelpest

Reputation: 0
• Posts: 40
• Joined: 12-January 13

## Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 04:21 PM

CTphpnwb, on 17 February 2013 - 02:21 PM, said:

Huh? What do you mean by mathematically correct?

A brute force method of determining if a number is prime is to check if it is devisable by any number smaller than it but greater than 1.

Could you provide an example code to me , because when I tried it, it just made the program to give me out prime number result only.Also, this has not been connected to the question in the title in anyway to my understanding.

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3459
• Posts: 12,299
• Joined: 08-August 08

## Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 04:37 PM

Here's an example:
15 % 2 == 1, so it could be prime. Keep going.
15 % 3 == 0, so it cannot b prime. Output 3 and 15/3 as numbers it is divisible by and stop.

and another:

7 % 2 == 1, so it could be prime. Keep going.
7 % 3 == 1, so it could be prime. Keep going.
7 % 4 == 3, so it could be prime. Keep going.
7 % 5 == 2, so it could be prime. Keep going.
7 % 6 == 1, but there are no more numbers less than 7, so it must be prime.

### #7 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: How to display numbers that input number is divisible with?

Posted 18 February 2013 - 09:28 PM

Write a function which will get the factors of the input number and print them. If there are no factors the number is prime... so print it.

Reputation: 3
• Posts: 89
• Joined: 08-February 13

## Re: How to display numbers that input number is divisible with?

Posted 19 February 2013 - 09:35 PM

Something to double the speed of your program, no number has a factor greater than half of it's value. Therefore, you can cut off your if loop much earlier. (esp for large numbers)

### #9 Skydiver

• Code herder

Reputation: 4768
• Posts: 15,750
• Joined: 05-May 12

## Re: How to display numbers that input number is divisible with?

Posted 20 February 2013 - 06:35 AM

If you are only looking for the prime factors, then "square root of the value" is sufficient.

If you are looking for all the factors, then "half the value" is the correct limit.

Remember, though to always list the number itself, otherwise you'll be missing a factor.