C Program to Print Fibonacci Series of N Numbers N संख्याओं की Fibonacci Series प्रिंट करने का C प्रोग्राम
The Fibonacci series is a sequence in which each term is the sum of the previous two terms. In this program, the user enters the number of terms and the first two numbers of the series.
Fibonacci Series एक ऐसी संख्या श्रृंखला है जिसमें प्रत्येक अगला पद, पिछले दो पदों के योग के बराबर होता है। इस प्रोग्राम में यूजर श्रृंखला के पदों की संख्या और शुरुआती दो संख्याएँ दर्ज करता है।
C Program
#include <stdio.h>
int main(void) {
int n, a, b, c, i;
printf("How many numbers do you want in Fibonacci series?\n");
if (scanf("%d", &n) != 1) {
printf("Invalid input! Please enter an integer.\n");
return 1;
}
if (n <= 0) {
printf("Wrong input! Enter a positive number.\n");
return 1;
}
printf("Enter initial numbers a and b:\n");
if (scanf("%d%d", &a, &b) != 2) {
printf("Invalid input! Please enter two integers.\n");
return 1;
}
printf("Fibonacci series of %d numbers is:\n", n);
if (n == 1) {
printf("%d\n", a);
} else {
printf("%d %d", a, b);
for (i = 3; i <= n; i++) {
c = a + b;
printf(" %d", c);
a = b;
b = c;
}
printf("\n");
}
return 0;
}Sample Output 1
How many numbers do you want in Fibonacci series?
7
Enter initial numbers a and b:
0 1
Fibonacci series of 7 numbers is:
0 1 1 2 3 5 8Sample Output 2
How many numbers do you want in Fibonacci series?
6
Enter initial numbers a and b:
2 3
Fibonacci series of 6 numbers is:
2 3 5 8 13 21Explanation
The program accepts the number of terms n and the first two numbers a and b. It prints these initial numbers and then uses a for loop to calculate the remaining terms. Each new term is calculated by adding the previous two terms.
प्रोग्राम n में पदों की संख्या तथा a और b में शुरुआती दो संख्याएँ इनपुट करता है। इसके बाद शुरुआती संख्याएँ प्रिंट होती हैं और for लूप पिछले दो पदों को जोड़कर बाकी पद निकालता है।
Modern C Compatibility Note
This program uses standard C and works with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().
This program demonstrates how to generate a Fibonacci series of a given number of terms using a for loop.
यह प्रोग्राम for लूप की सहायता से Fibonacci Series के दिए गए पदों को प्रिंट करना सिखाता है।
Comments
Post a Comment