The task is to create a program that can do the maximum factorial possible by the machine using "for" cycle.
I understood i have to use bigger data types (the biggest one is "long long", correct me if i'm wrong), and i also understood the concept of the factorial.
Otherwise, i really do not know how to apply what i know in c .
these is the idea at the base of what i wrote down as of now:
#include <cstdlib>
#include <iostream>
#include <math.h>
include namespace std;
int main(int argc, char *argv[])
{
long long i, factorial;
cin<<factorial;
{
for(long long i=1; i=factorial; i )
{
factorial=factorial*i
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
Problems are:
- I don't know if the code it's wrote correctly
- by doing "factorial=factorial*i", the "i=factorial" in the "for" cycle doesn't make sense, since it will stop the program before it has to.
This being said, I would like to point out that i am in high school, my programming knowledge is really poor, and for this reason every kind of advice and information is very well accepted :).
Thanks in advance
CodePudding user response:
Maybe you want to compute the maximum factorial that will fit in an unsigned long long
data type.
But let us look at the horrible program. I add comments with the problems.
#include <cstdlib> // Not to be used in C
#include <iostream>
#include <math.h> // Not needed
include namespace std; // Completely wrong statement. But even if you would have done it syntactically correct, it should never be used
int main(int argc, char *argv[]) // Neither args nor argv is used
{
long long i, factorial; // i will be redefine later. factorial is not initialized
cin<<factorial; // You want to stream somethin into std::cin?
{
for(long long i=1; i=factorial; i ) // Thats heavy stuff. i=factorial? Nobody know, what will happen
{
factorial=factorial*i // Semicolon missing
}
}
system("PAUSE"); // Do not use in C
return EXIT_SUCCESS; // EXIT_SUCCESS is not defined
}
Maybe the below could give you an idea
#include <iostream>
int main() {
unsigned long long number{};
unsigned long long factorial{1};
std::cin >> number;
for (unsigned long long i{ 1 }; i <= number; i)
factorial = factorial * i;
std::cout << factorial;
}