C
C
churchhela2020-12-06 22:16:26
C++ / C#
churchhela, 2020-12-06 22:16:26

Why doesn't my C code work?

you need to make a program that decomposes any number into prime factors

#include <stdio.h>
int main(void){
    int nUm,d;
    scanf("%d", &d);
    d=2;
    while (nUm >= 1){ //цикл вывода простых делителей
        if(nUm % d == 0){
            printf("%d", d);//распечатываем простой делитель
            nUm = nUm % d;// делим на него
                d=2; //обнуляем переменную d
        }else{
            d++;
        }
    }
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-12-06
@churchhela

Judging by the code, where it is written scanf("%d", &d);meant scanf("%d", &nUm);.

nUm = nUm % d;// делим на него

It should be written here. nUm = nUm / d;// делим на него
Also, d can divide nUm more than 1 time, this can be checked right away, and divide nUm by d as many times as possible. Then you don't have to do this:
d=2; //обнуляем переменную d

G
galaxy, 2020-12-06
@galaxy

nUm where is it initialized? (this is the main problem)

d=2; //set variable d to zero

why?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question