0
#include <iostream>
#include <stdlib.h>

using namespace std;

bool primos(int primo);
int divs(int num);

int dato, d2;

int main()
{   
    cout<<divs(5);
    cout<<divs(15);

THe console returns 50, which is weird since it should output 53, the first prime factor it finds for each number.

    return 0;
}

// PRIMOS
bool primos(int primo)
{
    int x;
    for (int i = 1; i <= primo; i++)

    {
        if (primo % i == 0)
        {
            // cout << i << endl;
            x += i;
        }
    }
    if (x == (primo + 1))
    {
        return true;
    }
    return false;
}

// DIVISOR PRIMO
int divs(int num)
{

    /* RETURN PRIME FACTOR */
    for (int i = 2; i <= num; i++)
    {
        if (num % i == 0 && primos(i))
        {
            return i;
        }
    }
    return 0;
}

In the main funciton, after the first iteration of the function, it no longer recognizes numbers as divisors and goes trough all of them and returns 0 (error, in this case.)

Cani77
  • 1
  • 3
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Aug 16 '22 at 12:56
  • 6
    In `primos`, `x` is uninitialized. The value that the function computes is meaningless. – Pete Becker Aug 16 '22 at 12:56
  • 2
    Note that if you find yourself writing something like `if(x) { return true; } return false;` you can instead just write `return x;`. – François Andrieux Aug 16 '22 at 12:57

0 Answers0