0
I’m having trouble showing if the number is perfect, when I put in 6, it says it’s not perfect, and I can only use pointer and dynamic allocation. Someone would know how to help me?
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int perfectNumber(int fNumber) {
int sum = 0;
for(int i = 1; i < fNumber; ++i) {
sum += i;
if(fNumber % sum == 0) {
return 0; // It's perfect
} else {
return -1; // it isn't perfect
}
}
}
int perfectVerify(int fSecondNumber) {
if(fSecondNumber == 1) {
return 1;
}
if(perfectNumber(fSecondNumber) == 0) {
return 1;
} else {
return 0;
}
}
int main() {
setlocale(LC_ALL, "Portuguese");
int *number;
number = malloc(sizeof(int));
printf("Digite um número inteiro: ");
scanf("%d", number);
if(perfectNumber(*number) == 1) {
printf("%d Um número perfeito\n", *number);
} else {
printf("%d Não é um número perfeito", *number);
}
return 0;
}
Your algorithm for checking if the number is perfect is wrong. You should only add up if it is a divisor and not add up all the numbers.
– anonimo
int sum = 0;
andint i = 1;
break the rule "I can only use pointer and dynamic allocation".– pmg
I recommend you, my dear, to take a table test. Try to solve for yourself, instead of looking for a solution on the internet, it is not so complicated. This will help you improve as a programmer...
– Leonardo Alves Machado