Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I kept running over this error. But I can't find it in the code.

#include<stdio.h>
long factorial(int num); //Function declaration
int main()
{
int num;
long fact;
printf("Enter a number to find factorial: ");
scanf("%d", num);
if(num<0)
printf("Factorial of negative no. is not defined. \n") ;
else
{
fact = factorial(num);
printf("%d!=%d \n", num, fact);
}
return 0;
}
//Function definition
long factorial(int num);
{
    {

if(num==0) 
return 1;

else


return(num*factorial(num-1);
}
}


What I have tried:

I know it's a syntax problem, but can someone here help me.
Posted
Updated 17-Feb-21 2:45am

Quote:
C++
long factorial(int num);
{
    {

if(num==0) 
return 1;

else


return(num*factorial(num-1);
}
}
Remove the trailing ; from the first line of the function.

You then have a stray unmatched ( on the return line.

And you have some extra braces within your function which aren't needed.

Also, indent your code properly. It will make it much easier to read.
C++
long factorial(int num)
{
    if (num == 0) return 1;
    return num * factorial(num-1);
}
 
Share this answer
 
Comments
Member 15074855 17-Feb-21 6:10am    
This worked, thank you so much. I'll take note of that.
Member 15074855 17-Feb-21 6:24am    
Just want to ask my program was supposed to show the factorial, but this code ain't doing it.
Richard Deeming 17-Feb-21 6:31am    
Well, you have an error in your scanf call - it should be:
scanf("%d", &num);

scanf - C++ Reference[^]

You also need to use %ld on the printf line to output the fact variable:
printf("%d!=%ld \n", num, fact);
Member 15074855 17-Feb-21 6:41am    
Okay thank you, noted.
Quote:
//Function definition
long factorial(int num); // <-- REMOVE THE SEMICOLON
{
{



[update]
Try
C++
#include<iostream>
  
using  namespace std;
long factorial(int num); //Function declaration
int main()
{
  int num;
  long fact;
  printf("Enter a number to find factorial: ");
  scanf("%d", &num);
  if( num < 0 )
    printf("Factorial of negative no. is not defined. \n") ;
  else
  {
    fact = factorial(num);
    printf("%d!=%ld \n", num, fact);
  }
  return 0;
}

//Function definition
long factorial(int num)
{
  if(num==0)
    return 1;
  else
    return(num*factorial(num-1));
}

[/update]
 
Share this answer
 
v2
Comments
Member 15074855 17-Feb-21 6:25am    
I did remove it, but it still has a lot of error.
CPallini 17-Feb-21 6:32am    
See my updated soluiton.
read the error message carefully because also the line number is mentioned.

I guess:
C
//Function definition
long factorial(int num);//TODO remove ;
{
 
Share this answer
 
Comments
Member 15074855 17-Feb-21 6:25am    
I did remove it, but it still has a lot of error.
KarstenK 17-Feb-21 7:52am    
read the error message en detail and start with the FIRST. This is very important, because some errors triggering other errors.
Looking at this and your Java question, you appear to be trying to write code in both languages without really understanding the basics of either of them. You would do well to focus on just one, and spend some (a lot of) time actually learning the syntax and other rules.

For Java see The Java™ Tutorials[^].

For C++ see Learn C++[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900