954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Factorial

Write a C program to find and print the product of factorial of numbers from N to M.

Can anyone please give me an idea, how to solve this Question ?

Thank you.

farhanafzal
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Start by learning how to calculate a factorial. Then translate that to code. Finally, do that on every value from N to M.

Seriously, have you tried anything yet?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This is what i tried, but seems wrong to me.

#include <stdio.h>
#include <conio.h>
void main ()
{
 int i;
 int n;
 int m;
 int j;
 int mult=1;

 printf("Enter N : ");
 scanf("%d",&n);
 printf("Enter M :");
 scanf("%d",&m);

 for(i=n;i<=m;i++) //loop till the m
  {
    for(j=1;j<=i;j++)// for factorial
     {
      mult=mult*j;
     }
      mult=mult*j;
  }
printf("Mult : %d",&mult);
getche();

}
farhanafzal
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

There is a logical error in your code. Check to see what code I have written and you will spot the error.

mult1=1;
for(i=n;i<=m;i++) //loop till the m
{
     mult=1; 
     for(j=1;j<=i;j++)// for factorial
     {
        mult=mult*j;
     }
     mult1=mult1*mult;
}


PS: Avoid using scanf. It has lots of hidden problems. Shift to fgets instead

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

abhimanipal

Thanks, i would love to use that fgets, the problem is that, we haven't reached till there yet in the lectures.

Farhan

farhanafzal
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Google for fgets.. Its best if you start using it from an earlier stage as opposed to changing your habit when you have already been using scanf for a while

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 
PS: Avoid using scanf. It has lots of hidden problems. Shift to fgets instead


But fgets is only for inputting strings while the OP requires integers and scanf() is pretty much suffice for the poster at this point. scanf() may not have nice error-checking techniques but it sure is good as sscanf() & ssprintf()

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

fgets can be used to input integers as well. We convert the string to integer using atoi.

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

Also i would suggest you to take long int type instead of simple int type...

vineet malhotra
Newbie Poster
3 posts since Feb 2010
Reputation Points: 8
Solved Threads: 0
 

#include
int main()
{
int n,i,f=1;
printf("enter n\n");
scanf("%d",&n);
for(i=1;i

dipesh shrestha
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
#include<stdio.h>
int factorial(int);
int main()
{
int n,f;
printf("enter n\n");
scanf("%d",&n);
f=factorial(int n);
printf("the factorial of %d is %d\n",n,f);
return 0;
}
int factorial(int n);
{
f=n*factorial(n-1);
return f;
}
dipesh shrestha
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
int n,f; 
f=1; 
printf("Enter the number:\n"); 
scanf("%d",&n); 

while(n>0) 
{ 
printf("%d",n); 
f=f*n; 
n--; 
} 
printf("The factorial of the integer is:%d",f); 
getch(); 
}
sannidhikumar99
Newbie Poster
12 posts since Mar 2008
Reputation Points: 6
Solved Threads: 0
 

Yes, we all know that you all can Code factorial programs, but please don't give away your precious, one-of-a-kind, coveted, subjected to plagiarism and not to mention unformatted code to others. :-/
If someone asks for problem is a elementary "Hello World" program, We PROVIDE help, We DON'T post "Hello World" programs using different coding techniques (STL, C99, BOOST, MFC..) just to show what we know.
Next time please stay on topic. Thank You.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

I tried running this code for m it gives me as
h=10 and m=4

i got that h part, but not the m, how m is 4.
as i have red in my lectures, that the following code

m-=m*3-h++*2;

can also be written as

m=m-m*3-h++*2;


i tried it but the answer comes way different.

The Actual Code

#include<stdio.h>
#include<conio.h>
void main()
{
 int m=7,h=8;
 h++;
 h=++h+4;
 m-=m*3-h++*2;

printf("h=%d and m=%d",h,m);
getche();
}
farhanafzal
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

i have red in my lectures, that the following code

m-=m*3-h++*2;

can also be written as

m=m-m*3-h++*2;

No m-=m*3-h++*2;

can be written as m=m-(m*3-h++*2);

Those parentheses make all the difference because when you factor them out it becomes m=m-m*3+h++*2;

Which is different to m=m-m*3-h++*2;

Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
 

Thanks a lot for the help
now it seems to be a big different

much appreciated

farhanafzal
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

m-=m*3-h++*2; = m=m-(m*3-h++*2);

1. m=m- m*3-h++*2;
2. m=m-(m*3-h++*2);

As stated by Banfa both are diff. Since h++ is a post-increment so the code is evaluated in foll. steps:
1st one:

m=m- m*3 - h*2;   | h++

And 2nd one:

->m=m- ((m*3)-(h*2))  | h++
->m= m - m*3 + h*2 ;  | h++

See the sign of (h*2) changes.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: