C Program to Print the Nth Term of Fibonacci Series Fibonacci Series का Nवाँ पद प्रिंट करने का C प्रोग्राम
The Fibonacci series is a sequence in which each term is the sum of the previous two terms. The first two terms can be chosen by the user.
Fibonacci Series एक ऐसी संख्या श्रृंखला है जिसमें प्रत्येक अगला पद, पिछले दो पदों के योग के बराबर होता है। इस प्रोग्राम में यूजर पहली दो संख्याएँ और वह पद दर्ज करता है जिसे प्रिंट करना है।
C Program
#include <stdio.h>int main(void) {int n, a, b, c = 0, i;printf("Which term do you want from Fibonacci series?\n");if (scanf("%d", &n) != 1) {printf("Invalid input! Please enter an integer.\n");return 1;}if (n <= 0) {printf("Wrong term! Enter a positive integer.\n");return 1;}printf("Enter two initial numbers a and b:\n");if (scanf("%d%d", &a, &b) != 2) {printf("Invalid input! Please enter two integers.\n");return 1;}if (n == 1) {printf("1st term of Fibonacci series = %d\n", a);} else if (n == 2) {printf("2nd term of Fibonacci series = %d\n", b);} else {for (i = 3; i <= n; i++) {c = a + b;a = b;b = c;}printf("%dth term of Fibonacci series = %d\n", n, c);}return 0;}
Sample Output 1
Which term do you want from Fibonacci series?5Enter two initial numbers a and b:0 15th term of Fibonacci series = 3
Sample Output 2
Which term do you want from Fibonacci series?7Enter two initial numbers a and b:2 37th term of Fibonacci series = 21
Explanation
The program first accepts the term number n and the first two numbers a and b. If n is 1 or 2, it prints the corresponding initial number. Otherwise, the for loop calculates the next terms by adding a and b. The variables are updated after each addition until the required term is reached.
प्रोग्राम पहले पद संख्या n तथा शुरुआती दो संख्याएँ a और b इनपुट करता है। यदि n का मान 1 या 2 है, तो संबंधित शुरुआती संख्या प्रिंट होती है। अन्यथा for लूप पिछले दो पदों को जोड़कर अगला पद निकालता है और आवश्यक पद तक पहुँचने पर उसे प्रिंट करता है।
Modern C Compatibility Note
This program uses standard C and can be compiled using modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().
This program demonstrates how to find the Nth term of a Fibonacci series using a for loop and simple variable updates.
यह प्रोग्राम for लूप और सामान्य variable updates की सहायता से Fibonacci Series का Nवाँ पद निकालना सिखाता है।
Comments
Post a Comment