Factorial in C, beginning of programming

Asked

Viewed 869 times

-2

include stdio.h
include stdlib.h
include math.h
int main()

{
    int n, prod=1,x;
    printf ("Digite um numero:");
    scanf ("%D", &n);
    for (=1; x<=n; x++)
{
Prod*= x;
}
printf ("Fat= %D\n", prod);
return 0;
}
  • 4

    Welcome back, @Edgardo. When asking a question, try to get the details of the problem as clear as possible: what error occurs, at what point, etc. Have a look at tour :)

  • In the scanf must be %d (with a small "d"), no for must have x=1 (instead of only =1) and within the for, the variable prod must be with a tiny "p"

3 answers

0


#include <stdio.h>
int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Entre com um numero inteiro: ");
    scanf("%d",&n);

    // Exibi mensagem de erro caso valor de entrada for negativo
    if (n < 0)
        printf("Erro! Fatorial de numero negativo nao existe.");

    else
    {
        for(i=1; i<=n; ++i)
        {
            factorial *= i;              // fatorial = fatorial*i;
        }
        printf("Fatorial de %d = %llu", n, factorial);
    }

    return 0;
}

0

#include <stdio.h>

int main (){
    int i,n,resultado=1;
    printf ("Digite um numero: ");
    scanf ("%d",&n);
    for(i=n;i>=1;i--){
        resultado = resultado * i;
    }
    printf ("%d! = %d\n",n,resultado);
    return 0;
}

-1

int main(){
  int fat, n;

  printf("Insira um valor inteiro positivo para o qual deseja calcular seu fatorial: ");
  scanf("%d", &n);

  if(n==0){
    fat=1;
  }else{
    for(fat = 1; n > 1; n = n - 1)
      fat = fat * n;
  }

  printf("\nFatorial calculado: %d", fat);
  return 0;
}

Browser other questions tagged

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