Error when trying to calculate factorial,per passing meter

Asked

Viewed 64 times

0

I am studying functions by passing parameter,the teacher asked to calculate the factorial of a number by passing parameter,the maximum that I got was the code below, yet the code does not execute.

#include <stdio.h>
#include <stdlib.h>
void fatorial(int num, long int*fat); 
int main()
{

  int num;
  printf("Digite um numero:\n");
  scanf("%d", &num);
  fatorial(num);
  return 0;
}
void fatorial(int num, long int *fat)
 {
    for(fat = 1; num > 0; num = num - 1)
    {
       fat *= num;
    }
    printf("%ld\n", fat);
 }
  • 1

    The compile code. I can see it here by just tapping my eye. " Calculate the factorial of a number per parameter passage" is a meaningless phrase, better formulate

  • What do you mean by "calculate ... by passing parameter" ? Is the number to be calculated the only parameter of the function ? The factorial result is supposed to be assigned in a parameter (pointer) ?

1 answer

0

I reread your example by taking the parameter long int *fat of its factorial function and declared the fat as a local variable in the function.

#include <stdio.h>
#include <stdlib.h>
void fatorial(int num); 
int main()
{

  int num;
  printf("Digite um numero:\n");
  scanf("%d", &num);
  fatorial(num);
  return 0;
}

void fatorial(int num)
 {        
    long fat = 0;    
    for(fat = 1; num > 1; num--)
        fat *= num;             
    printf("%ld\n", fat);
 }

Another change would be to make the factorial return a long, so

#include <stdio.h>
#include <stdlib.h>
long fatorial(int num); 
int main()
{

  int num;
  printf("Digite um numero:\n");
  scanf("%d", &num);
  long fat = fatorial(num);
  printf("%ld\n", fat);
  return 0;
}

long fatorial(int num)
 {
    long fat = 0;
    for(fat = 1; num > 1; num--)
        fat *= num;    
    return fat;
 }

Browser other questions tagged

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