here why should we have to write c=1 ??
i couldn't understand the logic of writing c=1 :s... please just explain the logic
void main(void)
{
int a,b,c=1;
a=5;
for ( b=a; b>0; b--)
{
c*=b;
}
printf ("The Factorial of %d is %d\n",a,c);
getch();
}
here why should we have to write c=1 ??
i couldn't understand the logic of writing c=1 :s... please just explain the logic
void main(void)
{
int a,b,c=1;
a=5;
for ( b=a; b>0; b--)
{
c*=b;
}
printf ("The Factorial of %d is %d\n",a,c);
getch();
}
because your assignment statement c *= b;
is a shorthand way of saying c = c * b
by setting c=1, this correctly causes the first execution to be read as "c = 1 * b", and uses the value of c recursively each time afterward.
if you don't initialize c=1 at the very beginning, the first execution will be "c = <some_unknown_value> * b"... and you don't want that.
.
thanks alot :)
but here b is equal to 5 4 3 2 1 , then why ithis program is multiplying all the values of b ???
i mean why the ouput is 120 since we never said to the program to multiply all the values of loop
and why we've used c in a program ?? can't we done this without using airthmaetic assignment operator ?
thanks alot :)
but here b is equal to 5 4 3 2 1 , then why ithis program is multiplying all the values of b ???
i mean why the ouput is 120 since we never said to the program to multiply all the values of loop
i don't know why you're multiplying it. you're the one who wrote the program, right? ;)
change the multiplication operator to an addition operator if you want to recursively add values c += b
.
and why we've used c in a program ?? can't we done this without using airthmaetic assignment operator ?
not sure i understand your question. factorial is recursive multiplication, so you have to do some amount of arithmetic, and you have to assign the value to something, right?
if you want to get rid of a variable, you could get rid of "c" i suppose, and keep the total factorial product in "a". you'd save a variable, and maybe a line of code or two, but the end result should be the same.
.
i don't know why you're muliplying it. you're the one who wrote the program, right? ;)
change the multiplication operator to an addition operator if you want the correct factorial to be computed.
c += b
but why airthmetic assignment operator adding,multiply,subtract etc...the values inside the loop ?
i mean when am writing c += b
the program is adding all the values inside the loop , that is 4+3+2+1 and the answer is 10...just tell me why programm is adding these values ??
what should be the correct answer of my question ??
okay, hang on a sec. lets get the definition of 'factorial' agreed upon.
factorial is a total products, as calculated using recursive multiplication from 1 up to the number that you are getting the factorial of.
factorial of 5, is written as "5!"
and 5! = 1 x 2 x 3 x 4 x 5 = 120.
so you should be multiplying it as originally shown, using the "c *= b" assignment
(there is another operation that does addition, like 1 + 2 + 3 + 4 + 5 = 15, but that's not a factorial, i forget what it is called.)
so the whole point of this program is to be able to vary the number you want to compute the factorial of. in your example 5 is used, but there you should be able to check any number within a reasonable range, such as 6! or 7!, etc....
that's why you use a loop, because the point is not to just hard-code "1x2x3x4x5" ... but to be able to count any number of factorials from 1! through at least 12! depending on how large of an integer you can handle.
Hey...i know what the factorial is :-s and i know the range of int, it will not find the factorial of 8 or number above 8 because this would be outside the int range....
you are not understanding what i'm trying to ask
i just want to know why this program is multiplying all the values inside the loooop ?? why is is happening just answer my this question
for example,
if i write c+=b
instead of c*=b
then the program will add the all the values inside the loop but what is the reason ? why programm is doing so ??
why it is adding , multiplying the values inside the loop ?? c+=b
in this line we said to program to add the value of c in b , so the answer should be b+1 , but here the output is 10 because program is adding all the values of b together...
why is it doing so ??
okay, sorry, you're right i'm not understanding your question. let me try again.
you're trying to calculate a factorial. in this example you're trying to calculate the factorial of "5". the factorial of 5 is
5! = 1 x 2 x 3 x 4 x 5 = 120.
your program performs this by a using a loop, by multiplying each of these numbers and holding the value in the variable "c". in your specific case, your program does it in reverse
5! = 5 x 4 x 3 x 2 x 1 = 120.
but its the same thing. in any event, please forget that i said anything about addition. you should not be using the addition operator "+" or "+=" for anything here.
here is what you have:
a = the number you want to take factorial of
b = a loop counter
c = the total answer, kept as a running value.
c is initialized to 1
c = 1
a is set to 5
a = 5
b is set to value of a, at the beginning of the "for" loop.
b = a
start loop using b, and decrement by 1 as long as b > 0
first loop:
b = 5
c = c * b
c = 1 * 5
c = 5
second loop:
b = 4
c = c * b
c = 5 * 4
c = 20
third loop:
b = 3
c = c * b
c = 20 * 3
c = 60
fourth loop
b = 2
c = c * b
c = 60 * 2
c = 120
fifth loop
b = 1
c = c * b
c = 120 * 1
c = 120
sixth loop (attempt)
b = 0
exit loop
final answer, c=120
this is how your program as it originally was written works. the program is correct.
your original question, c is set to c=1 for the first iteration of this loop to work correctly. otherwise c would equal zero, or some other unknown number.
.
i know the range of int, it will not find the factorial of 8 or number above 8 because this would be outside the int range....
the size of "int" is determined by the compiler.
if you are using a relatively modern compiler, as in from the past 15 years or so, the size of "int" should be 32 bits.
this will allow factorials up to !12
and if you use a type "long long", this will be 64 bits, and i believe will allow factorials up to !20
thankx thankx thanks alot...!!!! :-)
glad to help.. sorry i got you diverted on the addition thing.
this is the same program using while loop,
can you please explain this >> while (number !=0)
, this programm.
void main(void)
{
Long number=1;
Long answer;
while (number !=0)
{
printf (“\nEnter a number:”);
scanf (“%ld”,&number);
answer=1;
while (number>1)
answer=answer*number--;
printf (“Factorial is: %ld \n”, answer);
}
}
while (number != 0)
allows you to keep entering new numbers until zero is entered at which point the program quits.
the inner loop, "while (number > 1)" counts down the factorial loops, and multiplies each one into the total product, as long as the number is 2 or larger. because to multiply it by 1 doesn't change anything
thankx alot again :)
factriol of three digit number(700!) in c
but i required only the intiger format value
is it possible to store 700! value in integer format in c
multiplication of two numbers using arrAYS IN C
factorial of 700 in c
factorial of 700 using c uSing arraAYS
plz expalin me what is loop counter and in this program why we are using b=a
simply we can take two variable a,b
for eg: void main()
int a,b;
for(a=5;a>0;a--)
an why u r putting c*=b in curly brackits
you have declared as int a,b,c=1; You did not specify any storage class. By default all local variables of auto type. The default value of auto type is garbage value. In for loop c*=b means c=c*b means you are multiplying the values of b and c. where as b is 5 and c is garbage. Simply you will get the garbage value as result instead of actual result. Happy programming
Because you are provide a instruction like this that it multiplies each other or adding each other.....if u are not providing a statement like
c *= b;
or
c += b;
then it does not perform that work....:):):)
We know that Computer does not have any sense.. so the instruction which we are passing it just evaluate it... It does not use any kind of sense... means it does not perform any other activities itself.....:):):)
I think u understand it... what i want to tell u....:):):)
If u have any other problem then post it.... thanks..:):):):)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.