Accenture Coding Question
Factorials
Given ‘a’ (1<=n<=100), find the factorial of ‘n’. The factorial of ‘n’ is defined as the product of all integers.
Input specification:
Input : any number ‘n’
Output Specification:
Return a string containing the factorial.
Example 1:
Input : 5
Output: 120
Explanation:
5!=5*4*3*2*1 = 120
Example 2:
Input 1: 6
Output: 720
Explanation: 6!=6*5*4*3*2*1 = 720
Solution in Python 3:
#https://codewindow.in
#join our telegram channel
n=int(input())
fact=1
for i in range(1,n+1):
fact=fact*i;
print(str(fact))
# Telegram @codewindow
Solution in C :
//https://codewindow.in
//join our telegram channel @codewindow
#include<stdio.h>
int main()
{
int number;
long i,fact = 1;
scanf("%d",&number);
for(i=1;i<=number;i++)
fact=fact*i;
printf("%ld",fact);
return 0;
}
Solution in C++ :
//https://codewindow.in
//join our telegram channel @codewindow
#include<iostream>
using namespace std;
int main()
{
long i,fact=1;
int number;
scanf("%d",&number);
for(i=1;i<=number;i++)
fact=fact*i;
printf("%ld",fact);
return 0;
}
Solution in JAVA :
//https://codewindow.in
//join our telegram channel @codewindow
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
static int factorial(int n)
{
int res = 1, i;
for (i=2; i<=n; i++)
res *= i;
return res;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(factorial(num));
}
}
Output:
5
120