C C++ program generate factorial of number using recursion

Coding in C

#include<stdio.h>
int fact (int);
int main()
{
    int num,c=1,res=1;
    printf("Enter a number: ");
    scanf("%d",&num);
    printf("Factorial = %d",fact(num));
    return 0;
}
int fact(int x)
{
    if(x==0)
        return 1;
    else
        return x*fact(x-1);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int fact (int);
int main()
{
    int num,c=1,res=1;
    cout<<"Enter a number: ";
    cin>>num;
    cout<<"Factorial = "<<fact(num);
    return 0;
}
int fact(int x)
{
    if(x==0)
        return 1;
    else
        return x*fact(x-1);
    return 0;
}

Output

C C++ program generate factorial of number using recursion

Published by

admin

I am a computer teacher, programmer and web developer