Tricky Questions or puzzles in C Part 3

Thank You for the amazing response to the previous two posts on Tricky questions on C. In this post, we will again see some of the nice and interesting questions often asked in the interviews.




1) How to calculate the Factorial of the number without using Recursion and Loop?

Calculating factorial is very simple using recursion or loop, but how to calculate factorial without using anything?

Using the sterling´s approximation for the factorial, which is given by the formula


n! = sqrt(2*pi*n)*pow((n/e),n) 

Please note that this formula will not give the exact value of the factorial because it is just the approximation of the factorial.

I understand that it is not a "Tricky" question but it depends on the knowledge of the person about this formula, so if somebody has seen the formula then only he can answer the question.

This is the rough code for calculating the above (may not work with large numbers)

int main
{
int a;
float e,y,x,z;
scanf("%d",&a);

e=2.71;
y=a/e;
x=2*3.14*a;
z=sqrt(x)*pow(y,a);
printf("Factorial:%f",z);
}

2) How to print "HelloWorld" without using a semicolon?

int main()
{
if (printf("HelloWorld"))
{
}
}

Also, check my below the post to get Tricky puzzles in JAVA.
Tricky questions or puzzles in JAVA

3) What is the output of the below code snippet?

int main()
{
int a;
if (a=printf("HelloWorld")+4)
{
printf("%d", a);
}

Output : HelloWorld 14

4) What is the output of the below code snippet?

int main()
{
int a;
if (a=printf("HelloWorld"+4))
{
printf("%d", a);
}

Output : oWorld 6

To see the previous post on tricky questions on C, click the below links

Tricky questions in C part 1
Tricky questions in C part 2

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles