सी प्रोग्राम में अंकों का योग उपयोगकर्ता को किसी भी संख्या में प्रवेश करने की अनुमति देता है, उस संख्या को अलग-अलग संख्याओं में विभाजित करता है, और उन अलग-अलग संख्याओं को जोड़ देता है।
उदाहरण 1:
दी गई संख्या = 14892 => 1 + 4 + 8 + 9 + 2 = 24।
दी गई संख्या "14892" के अंकों का योग 24 है।
उदाहरण 2:
दी गई संख्या = 3721 => 3 + 7 + 2 + 1 = 13।
दी गई संख्या "3721" के अंकों का योग 13 है।
समस्या समाधान
- सी में अंक एल्गोरिदम का योग:
- संख्या को इनपुट के रूप में लें।
- संख्या को 10 से विभाजित करें और शेष को एक चर में जमा करें।
- शेषफल को योग में जोड़ें।
- इस प्रक्रिया को तब तक दोहराएं जब तक कि संख्या 0 न हो जाए।
- राशि प्रिंट करें।
#include<stdio.h>
main()
{
int dummy,n,sum=0,x;
printf("Enter a number\n");
scanf("%d",&n);
dummy=n;
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("The sum of all digits in %d is = %d\n",dummy,sum);
}
Output:-
Enter a number
673
The sum of all digits in 673 is = 16
Explain:-
- एक पूर्णांक को इनपुट के रूप में लें और इसे चर संख्या में संग्रहीत करें।
- चर योग को शून्य से प्रारंभ करें।
- इनपुट पूर्णांक को 10 से विभाजित करें और उसका शेषफल और भागफल प्राप्त करें।
- शेष को चर रेम में संग्रहित करें।
- वेरिएबल योग को वेरिएबल रेम के साथ बढ़ाएं।
- भागफल को चर संख्या में संग्रहित करें।
- इस प्रक्रिया को तब तक दोहराएं जब तक कि संख्या 0 न हो जाए।
- चर राशि को आउटपुट के रूप में प्रिंट करें और बाहर निकलें।