Calculate a Factorial
यह शुरुआती exercise Calculate a Factorial के जरिए core syntax और problem solving का अभ्यास कराती है।
Calculate a Factorial क्या है?
यह शुरुआती exercise Calculate a Factorial के जरिए core syntax और problem solving का अभ्यास कराती है।
Calculate a Factorial को C और C++ दोनों में runnable code से हल करें।
महत्वपूर्ण बातें
- Compiler warnings चालू करके सभी ठीक करें।
- हर value का type और lifetime समझें।
- Input और array bounds validate करें।
C और C++ code examples
main.c
#include <stdio.h>
int main(void) {
int number = 6; unsigned long long result = 1;
// Multiply each value into the running product.
for (int value = 2; value <= number; ++value) result *= value;
printf("%d! = %llu\n", number, result);
}
6! = 720
C++
main.cpp
#include <iostream>
int main() {
int number = 6; unsigned long long result = 1;
// Multiply each value into the running product.
for (int value = 2; value <= number; ++value) result *= value;
std::cout << number << "! = " << result << '\n';
}
6! = 720
C और C++ की तुलना
C और C++ implementations की तुलना करें, input बदलें और अतिरिक्त edge cases test करें।
C
C छोटे procedural APIs और representation details स्पष्ट करता है।
C++
C++ low-level model रखकर safer library types जोड़ता है।
अभ्यास
दोनों versions चलाकर बदलें और language guarantees की तुलना करें।
- Invalid और boundary inputs जोड़ें।
- -Wall -Wextra -Wpedantic से compile करें।
- Declarations और implementation अलग करें।