C++ Programming Tutorial 37 - For Loops (How to Calculate Factorial)

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

I did the factorial code counting up, here is how it looks:




#include <iostream>

int main()
{
int number = 4;
int result = 1;
for(int i = 1; i <= number; i++){
result = result * i;
}
std::cout << "Factorial of " << number << " is: " << result << "\n";
}

ReddoX
Автор

If you are running an infinte loop, you can slow it down by including the stdlib.h header and by putting _sleep(milliseconds) into the loop.

michaelswagson
Автор

Mr. Caleb is the best teacher. Your teaching way so easy.
I can't thank you enough
Watching from Ethiopia

yasina
Автор

I feel like I should get this but why use int i = factorial - 1 in this instance? Why not just use factorial? Isn't it 5 × 4 × 3 ×2 ×1? So starting at four and going to i>0 would still give you would only go to 1 right?

patrickconrad
Автор

note: this factorial program can only calculate the factorial of values up to 65 with an unsigned long long datatype

solidstatedrive.
Автор

for some reason it works fine for me, then when changed to user input it breaks and outputs: "the factorial of 5 is 0"

int main()
{
int num;
int factorial = num;
std::cout << "Enter a integer to factorial: ";
std::cin >> num;

for(int i = factorial - 1; i > 1; i--)
{
factorial = factorial * i;
}
std::cout << "the factorial of " << num << " is " << factorial;

return 0;
}

bryced
Автор

curious isnt i equal 4 cuz 5 minus 1 is 4 so 4 x 3 x 2 is 24

togurosensei
Автор

If you're still looking for a replacement for the loop:

int factorial(int number)
{
if (number < 2)
return number;

return number * factorial(number - 1);
}

aronwouters
Автор

the colon in between the quotation marks?

twotoes
Автор

Please can someone help with this:

Write a program in c++ that requests for n, number of students , read their ages and determine their average ages .

Note: Using for loop control structure

Have written this code well but am wondering why I keep getting errors.
Please kindly assist me, it's urgent. Thanks in advance

odelolatechup
Автор

Hey dude this is so simple
Why are u wasting my time 😂

tebeckcollins