तो, मुझे इस सूत्र को C में लागू करने की आवश्यकता है:
जहां n उपयोगकर्ता द्वारा दर्ज किया गया है और & gt; = 1. Ci संख्या का i-th अंक n k अंकों के साथ है , और Ci रेखा के साथ Ci (अंक + पूरक = 9) का पूरक है। उदाहरण के लिए, 21262 संख्या इस समीकरण को पूरा करती है क्योंकि:
21262 = 7 ^ 5 + 8 ^ 4 + 7 ^ 3 + 3 ^ 2 + 7 ^ 1 = 16807 + 4096 + 343 + 9 + 7 = 21262
मैंने एक एल्गोरिथ्म बनाने और इसे सी प्रोग्राम में बदलने की कोशिश की है, लेकिन जब मैं निष्पादित करता हूं, तो कुछ गलत हो जाता है। मुझे लूप तैयार करने में परेशानी हो रही है जो k-i + 1 पावर करता है।
#include <stdio.h>
int main() {
int n, t, k, c, i, s, power, j;
do {
printf("Enter n = ");
scanf("%d", &n);
} while (n < 1);
t = n; // Here we save the value of n, and operate with t
k = 0; // k counts the number of digits
while (t > 0) {
t = t / 10;
k++; // k = number of digits
}
t = n;
s = 0;
for (i = 1; i <= k; i++) { // Starts the sum from i to k
c = 9 - t % 10; // Complements the digits
power = 1;
for (j = 1; j <= k-i+1; j++) // Start of loop that powers th number
power = c * power;
s = s + power;
t = t / 10;
}
if (s == n)
printf("The number fulfills the equation");
else
printf("The number doesn't fulfill the equation");
return 0;
}
जैसा कि आप देख सकते हैं, मैंने एक लूप बनाकर बिजली की समस्या को हल करने का प्रयास किया जो कि c-i + 1 बार c पूरक करता है। लेकिन कुछ सही नहीं है। कृपया सहायता कीजिए!
3 जवाब
जैसा कि आपने दाएं से बाएं पढ़ा है, शक्ति i
होनी चाहिए और k - i + 1
नहीं होनी चाहिए।
#include<stdio.h>
int main() {
int n, t, k, c, i, s, power, j;
do {
printf("Enter n = ");
scanf("%d", &n);
} while (n < 1);
t = n; // Here we save the value of n, and operate with t
k = 0; // k counts the number of digits
while (t > 0) {
t = t / 10;
k++; // k = number of digits
}
t = n;
s = 0;
for (i = 1; i <= k; i++) { // Starts the sum from i to k
c = 9 - (t % 10); // Complements the digits
power = 1;
for (j = 1; j <= i; j++) // Start of loop that powers th number
power = c * power;
s = s + power;
t = t / 10;
}
if (s == n)
printf("The number fulfills the equation");
else
printf("The number doesn't fulfill the equation");
return 0;
}
आप <math.h>
हैडर फ़ाइल के तहत pow (b, p) फ़ंक्शन का उपयोग करते हैं। यह आपके काम को कम कर सकता है। आपको आंतरिक लूप का उपयोग करने की आवश्यकता नहीं है, आंतरिक लूप के बजाय इस कोड का उपयोग करें
power = pow(c,i);
यह कोड इस तरह हो सकता है-
#include<stdio.h>
#include<math.h>
int main() {
int n, t, k, c, i, s, power, j;
do {
printf("Enter n = ");
scanf("%d", &n);
} while (n < 1);
t = n; // Here we save the value of n, and operate with t
k = 0; // k counts the number of digits
while (t > 0) {
t = t / 10;
k++; // k = number of digits
}
t = n;
s = 0;
for (i = 1; i <= k; i++) { // Starts the sum from i to k
c = 9 - (t % 10); // Complements the digits
power = 1;
power = pow(c,i); // multiply the c by i th time by the pow(b,p) function under math.h.
s = s + power;
t = t / 10;
}
if (s == n)
printf("The number fulfills the equation");
else
printf("The number doesn't fulfill the equation");
return 0;
}
मैंने आपके प्रश्न के लिए एक अलग दृष्टिकोण की कोशिश की और यह ठीक काम करता है ... मुझे लगता है कि यह आपके लिए उपयोगी हो सकता है, इसलिए मैं इसे पोस्ट कर रहा हूं:
नोट: मैंने किसी फ़ंक्शन का उपयोग दर्ज किए गए मानों में अंकों की संख्या की गणना करने के लिए किया है और
pow()
फ़ंक्शन का उपयोग करने के लिएmath.h
फ़ाइल भी शामिल है।
तो यहाँ मेरा कोड है:
#include <stdio.h>
#include <math.h>//math.h to use pow() function
int num_digits(int n); //the sub function to caluclate number of digits
int main()
{
int n,c,t,t1,to_add,ans=0,d,i;
printf("enter number: ");
scanf("%d",&n);
t=t1=n;
d=num_digits(n); //function called
for(i=1;i<=d;i++)
{
t=t1%10;
c=9-t;
to_add=pow(c,i); //pow() used here
ans=ans+to_add;
t1=t1/10;
printf("%d\n",t1);
}
if(ans==n)
printf("condition fulfilled!\n");
else
printf("condition not fulfilled");
return 0;
} //main function ends here
int num_digits(int n) //function to caluclate number of digits
{
int i,c;
c=n;
for(i=1;c>10;i=i+1)
{
c=n/pow(10,i); //pow() even used here
}
return i;
}
और वैसे आपका सवाल काफी चुनौतीपूर्ण है :)
-धन्यवाद