Pergunta de entrevista da empresa OSIsoft

Question 3: Write a program in whatever language you like that solves for the factorial of a number.

Respostas da entrevista

Sigiloso

9 de dez. de 2013

import java.util.Scanner; class Factorial { public static void main(String args[]) { int n, c, fact = 1; System.out.println("Enter an integer to calculate it's factorial"); Scanner in = new Scanner(System.in); n = in.nextInt(); if ( n < 0 ) System.out.println("Number should be non-negative."); else { for ( c = 1 ; c <= n ; c++ ) fact = fact*c; System.out.println("Factorial of "+n+" is = "+fact); } } }

Sigiloso

14 de out. de 2014

Fortran program fact integer ans,num print*, 'Enter an integer to find its factorial' read(*,*)num if(num.lt.0) stop ans = rec(num) write(*,*)ans end program recursive function rec(n) result(f) integer,intent(in):: n integer f if(n.eq.0) then f=1 else f = n*rec(n-1) endif end function

Sigiloso

27 de nov. de 2013

My example was not great. I suggested looking online for more sophisticated answers