How to test the factorial in Main()?

Asked

Viewed 325 times

1

How would the main() of this function below? The user enters a number and appears on the screen the factorial of the same.

int fatorial(int num) {
if (num >= 0) {
    int fat = 1;
    while (num > 0) {
        fat *= num;
        num--;
    }
    return fat;
} else {
    return -1;
}}

int main(){
int n,num,fat;
printf("ESTE POGRAMA CALCULA N FATORIAL = N!\n");
printf("Digite um valor para ser calculado: ");
scanf("%d",&num);

printf("\n\tO fatorial de %d = %d\n\n",fatorial);
//printf("%d!=%d\n",num,fat);

//else printf("O calculo do fatorial se aplica somene aos numeros naturais.\n");

2 answers

5


This code has no logic whatsoever.

It even starts well, except by creating unnecessary variable again.

To call the function you need to be parentheses and pass as argument the number typed to be able to perform the calculation.

You have to check the return of this function if it is negative. If it is, you have to give the error message. If you see a natural number you can print.

Try to keep the code organized that helps you understand it.

You’re still missing some basic syntax and concepts variables, parameters, arguments, scope, function, etc..

#include <stdio.h>

int fatorial(int num) {
    if (num >= 0) {
        int fat = 1;
        while (num > 0) {
            fat *= num;
            num--;
        }
        return fat;
    } else {
        return -1;
    }
}

int main() {
    int num;
    printf("ESTE POGRAMA CALCULA N FATORIAL = N!\n");
    printf("Digite um valor para ser calculado: ");
    scanf("%d", &num);
    int fat = fatorial(num); //chama a função passado o que foi digitado e guarda em fat
    if (fat < 1) { //verifica se o retorno é menor que 1, se for indica erro
        printf("O calculo do fatorial se aplica somene aos numeros naturais.\n");
    } else { //se fat não for menor que um, então pode imprimir o resultado
        printf("\n\tO fatorial de %d = %d\n\n", num, fat);
    } //encerrou o bloco do if, só executa um dos dois blocos, nunca ambos
}

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

3

Here is a recursive function and an iterative function, where both compute the factorial

#include <stdio.h>
#include <stdlib.h>

int fatorialRecursivo(int n) {
    if(n < 0) {
        return -1;
    } else if(n == 0) {
        return 1;
    } else {
        return n * fatorialRecursivo(n - 1);
    }
}

int fatorialIterativo(int n) {
    if(n >= 0) {
        int fatorial = 1;
        for(; n > 0; n--) fatorial *= n;
        return fatorial;
    } else {
        return -1;
    }
}

int main() {

    int n = 10;

    if(n < 0) {
        printf("Nao e possivel calcular o fatorial de %d\n", n);
    } else {
        printf("O fatorial de %d e %d\n", n, fatorialRecursivo(n));
    }

    return EXIT_SUCCESS;
}

PS1: Learn recursive functions, once I got an IT internship because I was the only one who used recursive functions in the selection test

PS2: Oh, and if you’re starting out, I recommend you study clean code (clean code)

  • Ah this one has on the internet, it is much leaner, but the first one from the top helps to clarify better for those who are beginner, with for, while... but anyway thank you.

Browser other questions tagged

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