solution_4_2_factorial_calculator.c
C Programming Language/solutions/beginner/week4/solution_4_2_factorial_calculator.c
/**
* Solution 4.2: Factorial Calculator
* Week 4 - Loops and Problem Solving
*
* Description: Calculate factorial of a number
*/
/**
* @file solution_4_2_factorial_calculator.c
* @author [your name]
* @brief Write a program to calculate the factorial of a number by recursion way
* @version 0.1
* @date 2025-09-20
*
* @copyright Copyright (c) 2025
*
*/
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
#include <stdio.h>
int main() {
int number;
long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (int i = 1; i <= number; i++) {
factorial *= i;
}
printf("Factorial of %d = %lld\n", number, factorial);
}
return 0;
}
相关文章
bitwise_operations_advanced.c
bitwise_operations_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/bitwise_operations_advanced.c).
阅读文章 →huffman_coding_complete.c
huffman_coding_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/huffman_coding_complete.c).
阅读文章 →recursion_advanced.c
recursion_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/recursion_advanced.c).
阅读文章 →sorting_algorithms_complete.c
sorting_algorithms_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/sorting_algorithms_complete.c).
阅读文章 →binary_trees_complete.c
binary_trees_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/binary_trees_complete.c).
阅读文章 →linked_lists_complete.c
linked_lists_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/linked_lists_complete.c).
阅读文章 →