In: Computer Science
What is the output of the following C program?
#include<stdio.h>
int fac (int x);
void main( )
{
for (int i=1; i<=2; i++)
printf("%d", fac(i));
}
int fac(int x)
{
x = (x>1) ? x + fac(x-1) : 100);
return x;
}
The given C program is :
#include<stdio.h>
int fac (int x);
void main( )
{
for (int i=1; i<=2; i++)
printf("%d", fac(i));
}
int fac(int x)
{
x = (x>1) ? x + fac(x-1) : 100; // in question there is a ) symbol after 100. So, this will give error, otherwise if there is no such symbols then there will be a valid output
return x;
}
The above program uses the concept of recursion.
The working of fac(1) is :
So, value of fac (1) is 100.
The working of fac ( 2 ) is :
So, value of fac (2) is 102.
The print statement does not have any \n that is new line escape sequence.
Thus, the output is
100102