1.11M Members

The sum of digits of a number

 
0
 

I spent 4 hours trying to find out how I can add the digits of a number. Could you give me an idea?

 
0
 

>Could you give me an dea?
A search, perhaps?

 
0
 
void main()
{
  int 
  clrscr();
  printf("Enter the number");
  scanf("%d",&a);
  do
  {
    n=a%10;
    flag=(flag*10)+n;
  }
  while(a==0);
  printf("The sum of digits of a number %d is %d",a,flag);
  getch();
}
 
1
 

Sorry Thinker but you program doesn't hack it. Its full of syntax errors and undeclared variables, and it just plain doesn't work right. Line 10 does NOT calculate the sum of the digits. If the input number is 15 then the sum of the digits is 1 + 5 = 6. You are on the right track with line 9 but you flunked the course with line 10

 
0
 

The logic is there in your mind. But you have posted something that is very much far from a sound error-free program. Either you were in too much hurry or you need to clear your basics.

 
0
 

u just declare the variables .... forgot to do so ....

 
1
 
#include <stdio.h>

int main()
{
	int n = 12345;
	int sum = 0;
	
	while (n != 0) {
		int c = n%10;
		sum += c;
		n /= 10;
	}
	
	printf("The sum is %d\n", sum);
	
	return 0;
	
}

this should work just fine

 
-1
 

sorry buddy just try this out...

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,n,sum=0;
  clrscr();
  printf("Enter the number");
  scanf("%d",&a);
  do
  {
    n=a%10;
    sum=sum+n;
    a=a/10;
  }
  while(a=0);
  printf("The sum of digits of the number %d is %d",a,sum);
  getch();
}

i think this should work....

 
0
 

sorry buddy just try this out...
#include<stdio.h>
#include<conio.h>
void main()
{
int a,n,sum=0;
clrscr();
printf("Enter the number");
scanf("%d",&a);
do
{
n=a%10;
sum=sum+n;
a=a/10;
}
while(a=0);
printf("The sum of digits of the number %d is %d",a,sum);
getch();
}

i think this should work....

...
}while(a=0) makes the while loop execute only once.

you mean while (a != 0)

you should verify your programs before submitting

 
2
 

> sorry buddy just try this out...
> #include<stdio.h>
> #include<conio.h>
> void main()

Great, just what we want around here, another void main, conio.h riddled post without code tags.

 
0
 

yep got my mistake ... thanks for the help.
just made a mistake to type the program twice....

 
-1
 
#include<stdio.h>
#include<conio.h>
void main()
{
     int n,s=0,r;
     clrscr();
     printf("Enter the number");
     scanf("%d",&n);
      do
{
    r=n%10;
    s=s+r;
    n=n/10;
}while(n!=0);
    printf("the sum is %d",s);
    getch();
}
 
0
 

...

Read this, it applies to your program. (conio, void main(), getch clrscr are bad coding habits)
Next: read this to learn how to make a senseable post
Then read this about code tags
And learning what indention is, will help you in the future

regards Niek

 
-2
 

u cn jst tr ds out dat cn wrk 4 any num f long integer range sch as 123456789 etc.,

#include<stdio.h>
main()
{
	int a,b,c;
	long int n;
	clrscr();
	printf("Enter the Number:  ");
	scanf("%li",&n);
	c=0;
	while(n>0)
	{
		b=n%10;
		c+=b;
		n=n/10;
	}
	printf("The Sum of the
                          Number : %d",c);  
	getch();
}
 
2
 

u cn jst tr ds out dat cn wrk 4 any num f long integer range sch as 123456789 etc.,

Would you like to try that again in English?

Tada: the rulebook
- do not use kiddie-leet-speak
- use code tags when posting code
- do not resurrect year-old threads
- code formatting
- don't use scanf
- void main/int main/main
- getch() isn't standard C, getchar() is.
- clrscr();
- use meaningful names for your variables.

Should I continue? :)

 
0
 
int sumDigit(int number)
{
     return number < 10 ? number : number%10 + sumDigit(number/10);
}

This code can solve your problem..

 
-1
 

how can i go for looping loop concept in c.????
how to print
*
**
*** in c using for loop

 
-3
 
/*sum of digits*/
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int num,n,digits,r,remainder,i,sum,m;
clrscr();
n=10;
m=1;
r=0;
sum=0;/*remember to initialize variables*/
printf("Enter The No\n");
scanf("%d",&num);
printf("Enter The No of digits\n");
scanf("%d",&digits);
for(i=1;i<=digits;i++)
	{
	num=num-r;
	r=num%n;
	remainder=(r/m);
	sum=sum+remainder;/*modulus operator can not be used on float*/
	n=n*10;
	m=m*10;
	printf("%d\n",remainder);
	}
printf("%d\n",sum);
getch();
}
 
-2
 
#include <stdio.h>

main()
{
   int n, sum = 0, remainder;

   printf("Enter an integer\n");
   scanf("%d",&n);

   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

   printf("Sum of digits of entered number = %d\n",sum);

   return 0;
}
 
-1
 
#include <stdio.h>
#include <conio.h>

main()
{
   int n, sum = 0, remainder;

   printf("Enter an integer= ");
   scanf("%d",&n);

   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
   printf(" \n Sum of digits of entered number = %d.",sum);
getch();
}
You
Post:
Start New Discussion
Tags Related to this Article