How to test the factorial in Main ()?

2

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

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");
    
asked by anonymous 21.02.2017 / 14:58

2 answers

5
___ erkimt ___ How to test the factorial in Main ()? ______ qstntxt ___

How would %code% of this function be? The user types a number and the factorial of the number appears on the screen.

#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
}
    
______ ___ azszpr185630

This code has no logic at all.

To get well, except for the fact that create unnecessary variable again.

To call the function needs to be with brackets and pass as argument the number entered in order 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.

Still missing some syntax basics and concepts of variables , parameters, arguments , scope , functions, 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
}

See Running on ideone . And No Coding Ground . Also put it on GitHub for future reference .

    
______ azszpr185644 ___

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

%pre%

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

PS2: Oh, and if you're just starting out, I'll tell you to study clean code

    
___
21.02.2017 / 15:15
1

Here is a recursive function and an iterative function, where both calculate 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 stage because I was the only one who used recursive functions in the selection test

PS2: Oh, and if you're just starting out, I'll tell you to study clean code

    
21.02.2017 / 15:36