एक श्रृंखला में पदों की संख्या के लिए 'a' पहला पद, 'r' सामान्य अनुपात और 'n' दिया गया है। कार्य श्रृंखला का nवाँ पद ज्ञात करना है।
इसलिए, समस्या के लिए प्रोग्राम लिखने के तरीके पर चर्चा करने से पहले हमें यह जानना चाहिए कि ज्यामितीय प्रगति क्या है।
गणित में ज्यामितीय प्रगति या ज्यामितीय अनुक्रम वे होते हैं जहां पहले पद के बाद प्रत्येक पद को पिछले एक को एक निश्चित संख्या के लिए सामान्य अनुपात से गुणा करके पाया जाता है।
जैसे 2, 4, 8, 16, 32.. पहले पद 2 और सामान्य अनुपात 2 के साथ एक ज्यामितीय प्रगति है। यदि हमारे पास n =4 है तो आउटपुट 16 होगा।
तो, हम कह सकते हैं कि nवें पद के लिए ज्यामितीय प्रगति इस प्रकार होगी -
GP1 = a1 GP2 = a1 * r^(2-1) GP3 = a1 * r^(3-1) . . . GPn = a1 * r^(n-1)
तो सूत्र GP =a * r^(n-1) होगा।
उदाहरण
Input: A=1 R=2 N=5 Output: The 5th term of the series is: 16 Explanation: The terms will be 1, 2, 4, 8, 16 so the output will be 16 Input: A=1 R=2 N=8 Output: The 8th Term of the series is: 128
दृष्टिकोण हम दी गई समस्या को हल करने के लिए उपयोग करेंगे -
- प्रथम पद A, उभयनिष्ठ अनुपात R, और N को श्रृंखला की संख्या लें।
- फिर A * (int)(pow(R, N-1) द्वारा nवें पद की गणना करें।
- उपरोक्त गणना से प्राप्त आउटपुट लौटाएं।
एल्गोरिदम
Start Step 1 -> In function int Nth_of_GP(int a, int r, int n) Return( a * (int)(pow(r, n - 1)) Step 2 -> In function int main() Declare and set a = 1 Declare and set r = 2 Declare and set n = 8 Print The output returned from calling the function Nth_of_GP(a, r, n) Stopको कॉल करने से लौटा आउटपुट
उदाहरण
#include <stdio.h>
#include <math.h>
//function to return the nth term of GP
int Nth_of_GP(int a, int r, int n) {
// the Nth term will be
return( a * (int)(pow(r, n - 1)) );
}
//Main Block
int main() {
// initial number
int a = 1;
// Common ratio
int r = 2;
// N th term to be find
int n = 8;
printf("The %dth term of the series is: %d\n",n, Nth_of_GP(a, r, n) );
return 0;
} आउटपुट
The 8th term of the series is: 128