Can someone tell me what's wrong with my syntax.. THanks

#include<stdio.h>
#include<conio.h>
void FACTORIAL(int factorial=1, int N)
{int y;
{for(y==1;y<=N;y++)
factorial=factorial*y;
printf("The factorial of %d is %d", N,factorial);}
}
void main()
{clrscr();
printf("Please enter a no.");
scanf("%d",&N);
FACTORIAL(factorial=1,N);
getch();
}

Recommended Answers

All 3 Replies

>void FACTORIAL(int factorial=1, int N)
Defaulted parameters must follow non-defaulted parameters. Though in this case I don't see any need for the first parameter.

On a stylistic note, names in all caps are typically reserved for macros. Your function name would likely cause some raised eyebrows.

>for(y==1; y<=N; y++)
== is for comparison, = is for assignment.

>printf("The factorial of %d is %d", N,factorial);
Design note: The factorial() function should only calculate a factorial and return it. It's the caller's job to decide what is done with the resulting factorial. This gives you the most flexibility in reusing the function.

>void main()
main() returns int. In C++ you have no excuse for void main() because standard C++ even allows omission of the return statement. This void main() requires one more keystroke than int main() and kills portability. There are no benefits.

>clrscr();
Unnecessary and antisocial, please do not clear the screen when your program starts.

>printf("Please enter a no.");
If you don't end the prompt with a newline, the stream should be flushed manually to ensure that the prompt is visible before your input request blocks.

>scanf("%d",&N);
N does not exist as an entity here. You need to create a variable in main(). It's also a good idea to check user input for failure. scanf() returns the number of successfully executed format specifiers, which means that a successful call would return 1 in this case.

>FACTORIAL(factorial=1,N);
Neither C nor C++ support named arguments.

>getch();
The only use for this would be if you're running in an IDE that doesn't stop the window from closing, or by double clicking on the program's icon. The combination of clrscr() at the beginning and getch() at the end is stupid. I'll explain in more detail if you'd like.

Compare and contrast with my version:

#include <stdio.h>

int factorial(int n)
{
    int result = 1;
    
    for (int i = 2; i <= n; i++)
        result *= i;
    
    return result;
}

int main(void)
{
    int n;
    
    printf("Please enter a number: ");
    fflush(stdout);
    
    if (scanf("%d", &n) == 1)
        printf("The factorial of %d is %d\n", n, factorial(n));
    
    return 0;
}

this is even helping me alot thanks a lot Narue. But i am still confused in between ( endl , flush and end ) . I got understnad about endl and end but flush is quite unknown. Please explain.. :)

this is even helping me alot thanks a lot Narue. But i am still confused in between ( endl , flush and end ) . I got understnad about endl and end but flush is quite unknown. Please explain.. :)

Well, for starters there's no such thing as end [1]. endl is defined as printing a newline and then flushing the stream, so these two statements are equivalent:

cout << "Hello, world!" << endl;
cout << "Hello, world!" << '\n' << flush;

[1] There's an ends , but it's largely useless.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.