1,105,380 Community Members

Sum of digits

Member Avatar
sakthi_2001
Newbie Poster
1 post since Sep 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all, I am new to C programming, can anyone help me out with a solution to find sum of digits of a 5-digit number without using control statements, like if, for... thank you

Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 

Sorry. I thought this was in C++ ...

make an array, loop through, and sum:

int total = 0;
int array[ARRAY_SIZE];
/* Fill array with info */

for [0 -> ARRAY_SIZE-1] 
  total = total + array[CURRENT_POSITION]

print "Sum: " total

Obviously that's psuedo code.

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 [?]
 
1
 

[...] to find sum of digits of a 5-digit number without using control statements, like if, for... thank you

make an array, loop through, and sum:

Read the question again?

Member Avatar
iamthwee
Posting Sage
7,081 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 596 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Maybe you could use the modulus operator and recursion?

Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 

Oh sorry. I misread. Please forgive :) For my sins -

#include <stdio.h>

int sum_digi( const int &num ) {
  if ( !num ) 
    return num;
  else
    return (num%10)+sum_digi( num/10 );
}

int main( void ) {
  int sum = sum_digi( 12345 );

  printf( "Sum: %d\n\n", sum );

  return 0;
}

Just simple recursion. What it does is it starts at 5 and works to 1. It gets the last digit (through the %10 operation), and then gets the next value through recursively calling the same function but dividing by ten to get the next digit (ie from 5 to 4 in this case).

It'll also work for bigger numbers.

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

Perhaps unroll the loop?

int number = 56789, sum = 0;
   sum += number % 10; number /= 10;
   sum += number % 10; number /= 10;
   sum += number % 10; number /= 10;
   sum += number % 10; number /= 10;
   sum += number % 10; number /= 10;
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
 

Oh sorry. I misread. Please forgive :) For my sins -

...He that is without sin among you, let him first cast a stone... John 8:7
However you are still using if/else control statements.

if ( !num ) 
    return num;
  else
    return (num%10)+sum_digi( num/10 );
Member Avatar
iamthwee
Posting Sage
7,081 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 596 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Oh sorry. I misread. Please forgive :) For my sins -

#include <stdio.h>

int sum_digi( const int &num ) {
  if ( !num ) 
    return num;
  else
    return (num%10)+sum_digi( num/10 );
}

int main( void ) {
  int sum = sum_digi( 12345 );

  printf( "Sum: %d\n\n", sum );

  return 0;
}

Just simple recursion. What it does is it starts at 5 and works to 1. It gets the last digit (through the %10 operation), and then gets the next value through recursively calling the same function but dividing by ten to get the next digit (ie from 5 to 4 in this case).

It'll also work for bigger numbers.

Read it again, you used an if statement as Aia mentioned, in any case why are you doing someone else's homework. Leave them to their own devices:

Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 

>> Leave them to their own devices
I know. I normally don't but I misread the first post and apparently messed up the second. Fat load of use I am, eh?

I should start reading the posts more closely. Dave's method is probably what they want. Why don't you give out to Dave? (Kiddin').

Member Avatar
iamthwee
Posting Sage
7,081 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 596 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Yes in future, know the difference between a helpful prod in the right direction and a complete solution, albeit incorrect. Bah ha ha!

Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 

Well technically then because for him it was incorrect it was a helpful prod in the right direction. All he had to do was 'flatten' it, right? So if two grey cells can rub off one another to create a fire ... you'd expect I could read propah.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article