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.

Recommended Answers

All 16 Replies

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?

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();

}

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

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

Farhan

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

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()

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

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

#include<stdio.h>
int main()
{
int n,i,f=1;
printf("enter n\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
f*=n*i;
}
printf("the factorial of %d is %d",n,f);
return 0;
}

commented: better first u test it +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;
}
#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(); 
}
commented: Non Standard code +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.

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();
}

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;

commented: Thanx +1

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

much appreciated

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.

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.