CARVIEW |
Select Language
HTTP/2 200
content-type: text/html; charset=utf-8
date: Mon, 28 Jul 2025 23:31:20 GMT
permissions-policy: interest-cohort=()
strict-transport-security: max-age=31536000; includeSubDomains
server: nginx
cache-control: s-maxage=49134, max-age=0
x-powered-by: Next.js
etag: "ljlegzs3e88pgl"
content-encoding: gzip
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'self' http: https: ws: wss: data: blob: 'unsafe-eval' 'unsafe-inline'; frame-ancestors 'self';
vary: Accept-Encoding
x-cache: Miss from cloudfront
via: 1.1 957f06c0ba7b4f178d377350049dab50.cloudfront.net (CloudFront)
x-amz-cf-pop: BOM78-P11
x-amz-cf-id: abLXsIcZnVd-k2shF7BSsg590WkkzioRWxwqA43GmfzW5ueXtZwDMQ==
Algorithms | Recursion | Question 9
C++
C
Java
Python
JavaScript
Interview Preparation
- Interview Preparation For Software Developers
- Must Coding Questions - Company-wise
- Must Do Coding Questions - Topic-wise
- Company-wise Practice Problems
- Company Preparation
- Competitive Programming
- Software Design-Patterns
- Company-wise Interview Experience
- Experienced - Interview Experiences
- Internship - Interview Experiences
Algorithms | Recursion | Question 9
Last Updated :
Discuss
Comments
Predict output of following program
#include <iostream>
int fun(int n)
{
if (n == 4)
return n;
else return 2 * fun(n + 1);
}
int main()
{
std::cout << fun(2);
return 0;
}
#include <stdio.h>
int fun(int n)
{
if (n == 4)
return n;
else return 2*fun(n+1);
}
int main()
{
printf("%d", fun(2));
return 0;
}
public class Main {
public static int fun(int n) {
if (n == 4)
return n;
else return 2 * fun(n + 1);
}
public static void main(String[] args) {
System.out.println(fun(2));
}
}
def fun(n):
if n == 4:
return n
else:
return 2 * fun(n + 1)
print(fun(2))
function fun(n) {
if (n === 4)
return n;
else return 2 * fun(n + 1);
}
console.log(fun(2));
4
8
16
Runtime Error
This question is part of this quiz :
Top MCQs on Recursion Algorithm with AnswersTags:
Share your thoughts in the comments

GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy