0

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.

8
Contributors
16
Replies
18
Views
6 Years
Discussion Span
Last Post by nbaztec
Featured Replies
  • 1

    [QUOTE=farhanafzal]i have red in my lectures, that the following code [CODE]m-=m*3-h++*2;[/CODE] can also be written as [CODE]m=m-m*3-h++*2;[/CODE][/QUOTE]No [ICODE]m-=m*3-h++*2;[/ICODE] can be written as [ICODE]m=m-(m*3-h++*2);[/ICODE] Those parentheses make all the difference because when you factor them out it becomes [ICODE]m=m-m*3+h++*2;[/ICODE] Which is different to [ICODE]m=m-m*3-h++*2;[/ICODE] Read More

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?

0

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

}

Edited by Nick Evan: Removed bold-tags, added code-tags

1

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

Edited by abhimanipal: n/a

0

abhimanipal

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

Farhan

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

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

-5

#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;
}

Votes + Comments
better first u test it
-2
#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;
}

Edited by Nick Evan: added code tags. Learn to use them

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

Edited by Nick Evan: added code-tags. Learn to use them

Votes + Comments
Non Standard code
1

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.

0

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

Edited by farhanafzal: n/a

1

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;

Votes + Comments
Thanx
0

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

much appreciated

1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.