1,105,585 Community Members

The sum of digits of a number

Member Avatar
Adnan S.
Newbie Poster
1 post since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 

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

Member Avatar
thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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();
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,684 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,041 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
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

Member Avatar
Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 160 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
doublex
Newbie Poster
18 posts since Nov 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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....

Member Avatar
doublex
Newbie Poster
18 posts since Nov 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
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.

Member Avatar
thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
rajusankar28
Newbie Poster
3 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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();
}
Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
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

Member Avatar
emmwithjesus
Newbie Poster
1 post since Jul 2008
Reputation Points: -9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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();
}
Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
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? :)

Member Avatar
MustyCE
Newbie Poster
5 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
int sumDigit(int number)
{
     return number < 10 ? number : number%10 + sumDigit(number/10);
}

This code can solve your problem..

Member Avatar
akshay.thakur
Newbie Poster
1 post since Oct 2009
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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

Member Avatar
qasimhasnain
Newbie Poster
4 posts since Jan 2011
Reputation Points: -7 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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();
}
Member Avatar
mridul.ahuja
Junior Poster in Training
53 posts since Oct 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
-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;
}
Member Avatar
saguna.saha
Newbie Poster
1 post since Sep 2013
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 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