Certainly! Here’s an example of a factorial program in C:
#include <stdio.h> // Function to calculate the factorial of a number int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (number < 0) { printf("Error: Factorial of a negative number is undefined.\n"); } else { int result = factorial(number); printf("Factorial of %d = %d\n", number, result); } return 0; }
In this program, we have a factorial
function that calculates the factorial of a given number using recursion. The main
function prompts the user to enter a positive integer, reads the input, and then calls the factorial
function to calculate the factorial. It checks for negative numbers and displays an error message in such cases. Finally, it prints the calculated factorial.
To run this program, you can save it with a .c
extension (e.g., factorial.c
), compile it using a C compiler, and then execute the resulting binary file.
Factorial Program using loop:
Certainly! Here’s an example of a factorial program in C using a loop:
#include <stdio.h> // Function to calculate the factorial of a number int factorial(int n) { int result = 1; // Multiply numbers from 1 to n for (int i = 1; i <= n; i++) { result *= i; } return result; } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (number < 0) { printf("Error: Factorial of a negative number is undefined.\n"); } else { int result = factorial(number); printf("Factorial of %d = %d\n", number, result); } return 0; }
In this program, we have a factorial
function that calculates the factorial of a given number using a loop. The main
function prompts the user to enter a positive integer, reads the input, and then calls the factorial
function to calculate the factorial. It checks for negative numbers and displays an error message in such cases. Finally, it prints the calculated factorial.
To run this program, you can save it with a .c
extension (e.g., factorial.c
), compile it using a C compiler, and then execute the resulting binary file.
Factorial Program using recursion in C:
Certainly! Here’s an example of a factorial program in C using recursion:
#include <stdio.h> // Function to calculate the factorial of a number int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); if (number < 0) { printf("Error: Factorial of a negative number is undefined.\n"); } else { int result = factorial(number); printf("Factorial of %d = %d\n", number, result); } return 0; }
In this program, we have a factorial
function that calculates the factorial of a given number using recursion. The main
function prompts the user to enter a positive integer, reads the input, and then calls the factorial
function to calculate the factorial. It checks for negative numbers and displays an error message in such cases. Finally, it prints the calculated factorial.
To run this program, you can save it with a .c
extension (e.g., factorial.c
), compile it using a C compiler, and then execute the resulting binary file.