5 Computer -- Programming II

Define recursion. Write a C program to calculate factorial of a given number using recursion.

Define recursion. Write a C program to calculate factorial of a given number using recursion.


Recursion in C is a process where a function calls itself as a subroutine in order to solve a particular problem. In this process, the function repeats itself with different arguments until a specific termination condition.

OUPUT:

Enter any number 6

factorial is 720


PROGRAMS:

#include<stdio.h> 

int fact (int);

int main()

{

    int n,f;

    printf("Enter any number ");

    scanf("%d",&n);

    f = fact(n);

    printf("factorial is %d",f);

    return 0;

}

int fact (int n)

{

    if (n<=1)

        return 1;

    else

        return (n*fact(n-1));

}


More questions on Programming II

Differentiate between structure and union.

Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.

Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:

struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...
Close Open App