Knowee
Questions
Features
Study Tools

Write a C program, using functions where the function calculates the factorial of a given integer, and the main() program does I/O and calling of factorial function.

Question

Write a C program, using functions where the function calculates the factorial of a given integer, and the main() program does I/O and calling of factorial function.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that calculates the factorial of a given integer using a function:

#include <stdio.h>

// Function to calculate factorial
long long factorial(int n) {
    if (n >= 1)
        return n*factorial(n-1);
    else
        return 1;
}

int main() {
    int num;
    printf("Enter a positive integer: ");
    scanf("%d",&num);
    if (num < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        printf("Factorial of %d = %lld", num, factorial(num));
    }
    return 0;
}

In this program, the factorial() function is used to calculate the factorial of a number. The main() function handles the input/output and calls the factorial() function. If the input number is less than 0, it will print an error message. Otherwise, it will print the factorial of the input number.

This problem has been solved

Similar Questions

write an iterative code for factorial in c

c++ program on factorial of a number in short

Write a program to calculate the factorial of N.

Write a program that takes a positive integer as input and calculates its factorial using a for loop. The factorial of a number is the product of all positive integers up to that number.

Write an assembly language program for calculating the factorial of a number (between 0 and8) using an 8085 microprocessor. Assume that the number for which the factorial needs to becalculated is loaded in the memory location F000H. Results should be available in the F001H &F002H location. Use the subroutine or CALL instruction.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.