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

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();
}

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

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.

#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

Comments
This look very well done, why there are more posts after yours :/

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....

Comments
no code tags

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

> 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.

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

#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();
}

...

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

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();
}
Comments
Ditto what Salem said
Completely illiterate, 6 months late, and no code tags.

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? :)

Comments
I am Aia, and I approve of this message. ;)
Yeah, stick it to 'em!
int sumDigit(int number)
{
     return number < 10 ? number : number%10 + sumDigit(number/10);
}

This code can solve your problem..

/*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();
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Comments
#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;
}
#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();
}
Comments
Please look at thread dates before posting. Thanks.
#include<stdio.h>
#include<conio.h>
main()
{
    int a,c=0,b;
    clrscr();
    printf("Enter the number");
    scanf("%d",&a);
    while(a!=0)
    {
         b=a%10;
         c++;
         printf("%d-%d",b,c);
         a=a/10;
    }
    printf("Total no of digits are %d",c);
    getch();
}

//I checked it works

Comments
Please look at thread dates before posting. Thanks.
    int main()
    {
        int no;
        printf("Enter the input");
        scanf("%d",&no);
        no=(no<10?no:no%9);
        printf("%d",no);
    } 

In this one i am reducing the sum untill it reaches less than 10
I think this is the most simplest way to find the sum of Digits of a number.

Edited 2 Years Ago by ashokkumar01cbe

@Ancient Dragon . My above program is to find the sum of digits of number until it reaches less than 10. for example if the input is 999 then the sum of 999 is 27 since the sum is not less than 10, so it needs to be summed once again then the final answer is 9.This is what i have done in the above code, Is there any worng with my code? If its wrong suggest me where it occured? thank you.

Edited 2 Years Ago by ashokkumar01cbe: Small mistakes

Is there any worng with my code?

Well, yes. Where's the loop? Test it yourself, it doesn't do anything like you said it should. All it does is either print the number of it's less than 10 or prints the number mod 9. In either event, the program is pretty much useless as it is.

#include<stdio.h>
int main(){
int num;
printf("please enter your number\n");
scanf("%d",&num);

if(num>0&&num<10){
    int ans = num;
    printf("the answer is %d", ans);
}
else if(num>=10&&num<100){
    int remdr = num%10;
    int tens = num/10;

   int ans = remdr + tens;
    printf("the answer is %d + %d = %d\n", tens,remdr,ans);

}

else if (num>=100&&num<1000){
    int remdr = num%100;
    int hunds = num/100;
    int tens = num-((100*hunds)/10);
    int ans = remdr + tens + hunds;

    printf("the answer is %d + %d + %d = %d",hunds,tens,remdr,ans);

}
else {
    printf("out of range");
}
return 0;}

works up to 999... :) you can follow the concept and continue

correct answer coming right up people...!!

#include<stdio.h>
int main(){
int num;
printf("please enter your number\n");
scanf("%d",&num);

if(num>0&&num<10){
    int ans = num;
    printf("the answer is %d", ans);
}
else if(num>=10&&num<100){
    int remdr = num%10;
    int tens = num/10;

   int ans = remdr + tens;
    printf("the answer is %d + %d = %d\n", tens,remdr,ans);

}

else if (num>=100&&num<1000){
    int remdr = num%100;
     int hunds = num/100;
    int tens = remdr/10;
    int remdr2 = remdr%10;

    int ans = remdr2 + tens + hunds;

    printf("the answer is %d + %d + %d = %d",hunds,tens,remdr2,ans);

}
else {
    printf("out of range");
}
return 0;}

there it is.. and it works up to 999 :)

#include<iostream>
#include<math.h>


using namespace std;


int main(){
    int N=0 ;
    cin>>N;
    int sum=0;
    while(N!=0){

            sum += (N%10);
        N = N / 10;
    }
    cout << sum << endl;


}

int num1,num2;
cout<<"enter num1 :"<<endl;
cin>>num1;
cout<<"Enter num2 :"<<endl;
cin>>num2;
cout<<num1<<"+"<<num2 <<" = "<<num1+num2<<endl;

This article has been dead for over six months. Start a new discussion instead.