xxxxxxxxxx
#include<iostream>
using namespace std;
#define MAX 1000
int multiplyx(int x, int ans[], int size)
{
int carry = 0;
for (int i=0; i<size; i++)
{
int product = ans[i] * x + carry;
ans[i] = product % 10;
carry = product/10;
}
while (carry)
{
ans[size] = carry%10;
carry = carry/10;
size++;
}
return size;
}
void factorial(int n)
{
int ans[MAX];
ans[0] = 1;
int size = 1;
for (int x=2; x<=n; x++)
size = multiplyx(x, ans, size);
for (int i=size-1; i>=0; i--)
cout << ans[i];
}
int main() {
int n;
cin>>n;
factorial(n);
return 0;
}
C++Copy
xxxxxxxxxx
// C++ program to print all prime factors
#include <bits/stdc++.h>
using namespace std;
// A function to print all prime
// factors of a given number n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
cout << 2 << " ";
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
cout << i << " ";
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
cout << n << " ";
}
/* Driver code */
int main()
{
int n = 315;
primeFactors(n);
return 0;
}
// This is code is contributed by rathbhupendra