Not Yet Answered # The sum of digits of a number

thinker -4 Ancient Dragon 5,243 Jishnu 160 doublex 1 thinker -4 doublex 1 Salem 5,138 thinker -4 rajusankar28 emmwithjesus -9 Nick Evan 4,005 MustyCE akshay.thakur -5 qasimhasnain -7 mridul.ahuja 4 saguna.saha -3 Ramya murugan -3 ashokkumar01cbe ashokkumar01cbe Ancient Dragon 5,243 samson.dadson.3 samson.dadson.3 samson.dadson.3 rayhaneh Unsa

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.

2

```
#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

-2

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

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

-2

how can i go for looping loop concept in c.????

how to print

*

**

*** in c using for loop

-4

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

-2

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

-2

```
#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

-2

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

0

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

0

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.

0

```
#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

0

oops... huge mistake >.< i guess it'll only work correctly to 99 .. i'll try to fix the problem. forgive me

0

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

0

```
#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;
}
```

-1

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.

Recommended Articles

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...

I need to comput and display the sum and average of the cubes of the even natural numbers between 2 and n. Where n is entered by the user. Im stuck on how to average the sum of the cubes. Help Please. This is what I have so far.

`def ...`

I'm new to python, for my project I need to send mail from python.. I'm getting the following error can someone please help me how to get out of it..

```
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for ...
```