Multiples of 2 numbers in C

Asked

Viewed 699 times

-1

I am doing a program in C that asks for the value of n and two positive integers i and j. With these values I have to calculate n natural numbers that are multiples of i or j and or both. But my code for some reason the program crashes after reading the values, does not calculate any multiple and closes.

int n, i, j, contador, multiplosI, multiplosJ;

printf("Digite o valor de N : ");
scanf("%d",&n);

printf("Digite o valor de I e J : ");
scanf("%d %d",&i,&j);

multiplosI = 0;
multiplosJ = 0;

for (int contador = 0; contador < n; contador ++){
    if (i % contador == 0 && j % contador == 0){
        multiplosI = contador;
        printf("Multiplos de I e J : %d \n",multiplosI);
    } else if (i % contador == 0){
        multiplosI = contador;
        printf("Multiplos apenas de I : %d \n",multiplosI);
    } else {
        multiplosJ = contador;
        printf("Multiplos apenas de J : %d \n",multiplosJ);
    }
}

2 answers

2

There are several problems in the code and I hope to have understood the problem. Some things I changed because it gets better just.

I’m not sure but I doubt I should start the 0 so I started the 1.

The count should only be displayed at the end of the loop when it has finished making it and not in the middle of the count.

It missed counting when it is multiple of both, mixed with the count of I. And I simplified (probably made it more performatic) to find both).

I considered that it should be exclusively both, and that it should not be calculated there and the correct result would be only the sum of the two other accumulators, but I doubt that.

I wasn’t increasing the multiples counter, I was taking the current value of the counter, which doesn’t make any sense. The count is conditional, so each should be independently incremented.

Also did the bill of the rest on the contrary, the correct thing is to take the rest of the division of the counter by the established divisor, was using the counter as divisor, also does not make sense.

I preferred not to touch the names of the variables, but normally I would use other.

#include <stdio.h>

int main(void) {
    int n, i, j, multiplosI = 0, multiplosJ = 0, multiplos = 0;
    printf("Digite o valor de N: ");
    scanf("%d", &n);
    printf("Digite o valor de I e J: ");
    scanf("%d %d", &i, &j);
    for (int contador = 1; contador < n; contador++) {
        if (contador % i == 0) multiplosI++;
        if (contador % j == 0) multiplosJ++;
        if (contador % (i * j) == 0) multiplos++;
    }
    printf("\nMultiplos apenas de I: %d", multiplosI);
    printf("\nMultiplos apenas de J: %d", multiplosJ);
    printf("\nMultiplos de ambos: %d", multiplos);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future referenceto.

1


From what I understand of the problem you want to calculate the n and not limit each of the multiples to n.

An idea is:

#include <stdio.h>
int main() {
    int n, i, j, k=0, cont=0, mult_i=0, mult_j=0, mult_ij=0;
    printf("Digite o valor de N : ");
    scanf("%d", &n);
    printf("Digite o valor de I e J : ");
    scanf("%d %d", &i, &j);
    while (cont <= n) {
        k++;
        if ((k%i == 0) && (k%j == 0)) {
            mult_ij++;
            cont++;
        }
        else {
            if (k%i == 0) {
                mult_i++;
                cont++;
            }
            else {
                if (k%j == 0) {
                    mult_j++;
                    cont++;
                }
            }
        }
        k++;
    }
    printf("Multiplos de %d e %d : %d \n", i, j, mult_ij);
    printf("Multiplos apenas de %d : %d \n", i, mult_i);
    printf("Multiplos apenas de %d : %d \n", j, mult_j);
    return 0;
}
  • Just not able to understand why there are 2 counters in the program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.