1,237 views
4 votes
4 votes

What will be the output of the following C program?

#include <stdio.h>
int main()
{
	int f1(int,int);
	int x = 9,n = 3;
	printf("%d", f1(x, n));
}

int f1(int x, int n)
{
	int y = 1,i = 1;
	for(i = 1;i <= n; i++)
		y = y * x;
	return(y);
}
  1. 27
  2. 729
  3. 81
  4. Compilation Error

2 Answers

Best answer
3 votes
3 votes
x = 9, n = 3; 
int f1(int x, int n)
{
	int y = 1,i = 1;
	for(i = 1;i <= n; i++)
		y = y * x;
	return(y);
}

i = 1, 
y = 1, x = 9, n = 3,
y = 1*9 = 9

i = 2, 
y = 9, x = 9, n = 3,
y = 9*9 = 81

i = 3, 
y = 81, x = 9, n = 3,
y = 81*9 = 729

return (y) i.e. 729
selected by
0 votes
0 votes
1st time y=y*x;

y=1*9=9;

2nd time loop will run

y=y*x=9*9=81;

3rd time loop will run

and again y=y*x=81*9=729;

and next time condition will be false  because i!<=n

so y will return as o/p hence ans. is

729.
Answer:

Related questions

3 votes
3 votes
2 answers
2
8 votes
8 votes
2 answers
3
1 votes
1 votes
3 answers
4