Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Can someone explain why is *f = 1?

+1
−6

Regarding a code I got from the net on calculating factorial, why is 1 assigned to *f here?

#include <stdio.h>
void findFact(int,int*);
int main()
{
         int fact;
         int num1;
		printf("\n\n Pointer : Find the factorial of a given number :\n"); 
		printf("------------------------------------------------------\n");	
		printf(" Input a number : ");
		scanf("%d",&num1);		 

         findFact(num1,&fact);
         printf(" The Factorial of %d is : %d \n\n",num1,fact);
         return 0;
        }

void findFact(int n,int *f){
        int i;

       *f = 1; //here
       for(i=1;i<=n;i++)
       *f=*f*i;
       }
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

5 comment threads

Do you know how factorial work? If yes, it should be fairly obvious. If no, read https://en.wikipedia... (2 comments)
As a side note, the loop could start as `for(i=2`, because multiplying by 1 is redundant and unnecess... (1 comment)
What if the line wasn't there? (1 comment)
Calculate it on pen and paper (1 comment)
I know, responding to a question with another question is bad style, but anyhow (1 comment)

0 answers

Sign up to answer this question »