#factorial of a number

1 messages · Page 1 of 1 (latest)

severe grove
#

!formatting cpp

rare laurelBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```cpp
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

dark mantle
#

btw explain i dont think that logic holds true for numbers above 2,

#

1*3=3(right)

#

but 3 factorial is 6

severe grove
dark mantle
#

ill send a screenshot

severe grove
#

😑

dark mantle
#

how is factorial = factorial *number

severe grove
#

May I ask how you solved this problem if you don't understand the code?

dark mantle
severe grove
#

Do you know what the definition of a factorial is?

dark mantle
#

yes

#

uhm so can you explain it in detail , i somewhat got the answer and i tried coding it

severe grove
#

I will not do that, but I can guide you towards understanding it

#

I believe just explaining how things work is a subpar method of teaching

#

and learning

dark mantle
#

ok first ill tell you what i did

#

so we need a factorial of any number which is n*(n-1) .....x1=n !.

severe grove
#

yup

dark mantle
#

first i took input from these things - 1) n. then i assigned a value 1 to factorial variable since factorial starts with 1

#

factorial =1;

severe grove
#

sure

dark mantle
#

now im looping it until it reaches 1 so i use a for loop and increment it using a variable i, given that i should be less than or equal to the number inputted by user. i<=n, now i gets incremented. i =i+1; or i++.

#

but the catch over here is that i need factorial , so im factorial = factorial (initial=1)* i(which is 1 again). and im printing factorial out of the loop so that i get the factorial value.

#

1!=1*1

#

and now factorial increses to 2 after 1 loop.

#

2!=2*1.

#

3!=2*3(2 factoria value printed from previous iteration and 3 is number i)

#

this is what i understood.

severe grove
#

seems to be on the right track

dark mantle
#

sometimes my brain gets confused on stuff i know

#

🤣

#

btw i think i forgot to add if else condition in the solution since factorial cant be available for negative integers and zero

severe grove
#

My opinion is, don't waste your time on coding challenges as a beginner

#

There are much better ways to learn

dark mantle
#

coding challenges?

#

im learning though im not solving any problems