Sasha
0
Q:

factorial c++ without using function

#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;

int fact(int i){
	if (i <= 1) return 1;
  	else return i*fact(i-1);
}

int main(){
  	ios::sync_with_stdio(0);
  	cin.tie(0);
  	int N;
  	cin >> N;
  	cout << fact(N) << "\n";
  	return 0;
}
3
#include <iostream>
using namespace std;

int main()
{
	unsigned int num; //unsigned is used for not taking negetive values.
	unsigned long long factorial = 1; //Since the factorial a number can be large, so long long data type is used.
	cout << "Give me any positive number :	";
	cin >> num;

	for (int i = 1; i <= num; i++) {
		factorial = factorial * i;
	}
	cout << "Factorial of the given number is: " << factorial;
}
1
#include <iostream> 

//n! = n(n-1)!
int factorial (int n)
{
  if (n ==0)
  {
    return 1; 
  }
  else 
  {
	return n * factorial(n-1); 
  }
}
0

New to Communities?

Join the community