Skip to content
Home » C program to print the factorial of alternate elements in an array

C program to print the factorial of alternate elements in an array

Below is the code to print the factorial of alternate elements in an array

#include <stdio.h>
int fact(int n,int i){
    if(i!=0){
        n*=i;
        fact(n,i-1);
    }
    else{
        return n;
    }
}
int main() {
    int n,i,j,x=1;
    printf("Enter how many numbers do you want to enter:");
    scanf("%d",&n);
    int arr[n];
    printf("Enter elements: ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    printf("Elements in the array are:\n");
    for(i=0;i<n;i++){
        printf("%d\n",arr[i]);
    }
    printf("Factorial of alternate numbers are:");
    for(i=0;i<=n;i++){
        if(i%2==0){
           j=arr[i];
           x=fact(1,j);
           printf("%d\n",x);
        }
    }
    
}

Output:

Enter how many numbers do you want to enter:7
Enter elements: 5
3
5
4
2
7
8
Elements in the array are:
5
3
5
4
2
7
8
Factorial of alternate numbers are:120
120
2
40320