Program to find sum of digits of a number . Please help with logical error of program.
-
#include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
void main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
num=num/10;
i++;
}
for(j=i;j>0;j--)
{ multiplier=pow(10,j);
a[j]=num/multiplier;
num=num-(a[j]*multiplier);} for(j=0;j
-
#include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
void main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
num=num/10;
i++;
}
for(j=i;j>0;j--)
{ multiplier=pow(10,j);
a[j]=num/multiplier;
num=num-(a[j]*multiplier);} for(j=0;j
If you want to iterate the digits, then why get the number as a number rather than as a string? I'd get the string, iterate the digits, then get the numeric value of each digit, and then sum them. That makes getting the number of digits simpler as well.
You'll never get very far if all you do is follow instructions.
-
#include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
void main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
num=num/10;
i++;
}
for(j=i;j>0;j--)
{ multiplier=pow(10,j);
a[j]=num/multiplier;
num=num-(a[j]*multiplier);} for(j=0;j
Are you wanting a "digital root" algorithm?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
#include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
void main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num!=0)
{
num=num/10;
i++;
}
for(j=i;j>0;j--)
{ multiplier=pow(10,j);
a[j]=num/multiplier;
num=num-(a[j]*multiplier);} for(j=0;j
The 'quick fix' of your bugs produce the following program:
int main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
// clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num1!=0)
{
num1=num1/10;
i++;
}
num1 = num;
for(j=i-1;j>=0;j--)
{
multiplier=pow(10,j);
a[j]=num1/multiplier;
num1=num1-(a[j]*multiplier);
}
for(j=0;jHowever, I would rather write it this way:
int main()
{
long num, sum = 0;cout<<"Enter the number:"; cin>>num; while (num) { int remainder = num % 10; sum += remainder; num /= 10; } cout <<"The sum of the digits of is:" << sum << endl; getchar();
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite -
The 'quick fix' of your bugs produce the following program:
int main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
// clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num1!=0)
{
num1=num1/10;
i++;
}
num1 = num;
for(j=i-1;j>=0;j--)
{
multiplier=pow(10,j);
a[j]=num1/multiplier;
num1=num1-(a[j]*multiplier);
}
for(j=0;jHowever, I would rather write it this way:
int main()
{
long num, sum = 0;cout<<"Enter the number:"; cin>>num; while (num) { int remainder = num % 10; sum += remainder; num /= 10; } cout <<"The sum of the digits of is:" << sum << endl; getchar();
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA LiteI'd like to remind you that you probably meant to write
remainder
, notreminder
. ;) Good stuff though. :thumbsup:GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
I'd like to remind you that you probably meant to write
remainder
, notreminder
. ;) Good stuff though. :thumbsup:GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
Stefan_Lang wrote:
I'd like to remind you that you probably meant to write
remainder
, notreminder
.:-O :-O :-O Thank you, fixed.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
The 'quick fix' of your bugs produce the following program:
int main()
{
long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
// clrscr();
cout<<"Enter the number:";
cin>>num;
num1=num;
while(num1!=0)
{
num1=num1/10;
i++;
}
num1 = num;
for(j=i-1;j>=0;j--)
{
multiplier=pow(10,j);
a[j]=num1/multiplier;
num1=num1-(a[j]*multiplier);
}
for(j=0;jHowever, I would rather write it this way:
int main()
{
long num, sum = 0;cout<<"Enter the number:"; cin>>num; while (num) { int remainder = num % 10; sum += remainder; num /= 10; } cout <<"The sum of the digits of is:" << sum << endl; getchar();
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA LiteHi, Don't use so many loops unnecessarly, here is the one way to get your answer in optimistic way. int main() { long num,num1,i=0,sum=0; short remainder = 0; cout<<"Enter the number:"; cin>>num; num1=num; while(num!=0) { remainder = num % 10; num = num / 10; sum = sum + remainder; i++; } cout<<"The sum of the digits of the "<