ALl i need to find is the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6).I have coded with some logic and i could able to find the sum til this part 12345=>1+2+3+4+5=15 but i couldn able to carry on after that plz guide me with that

``````#include <stdio.h>
#include <stdlib.h>

/* Puzzle A20 - print a triangle of stars, n rows tall */
int main(int argc, char *argv[])
{
int sum = 0;
int n;
scanf("%d",&n);
int t = n;
while (n > 0) {
int p = n % 10;
sum = sum + p;
n = n / 10;
}
printf("%d", sum);
}``````

[pseudocode]

``````get 'number'

do outer loop while 'number' > 9

set value of 'sum' to zero

do inner loop for each 10's place digit in 'number'

increment 'sum' by value of each digit in 'number'

replace 'number' with this 'sum'

'number' is now sum of digits reduced …``````

## All 2 Replies

[pseudocode]

``````get 'number'

do outer loop while 'number' > 9

set value of 'sum' to zero

do inner loop for each 10's place digit in 'number'

increment 'sum' by value of each digit in 'number'

replace 'number' with this 'sum'

'number' is now sum of digits reduced to a single digit``````

[/pseudocode]

commented: Thanks for using pseudocode tags +4
``````#include <stdio.h>
#include <stdlib.h>

/* Puzzle A20 - print a triangle of stars, n rows tall */

int main(int argc, char *argv[])
{

int n;
scanf("%d",&n);
while(n>9)
{
int sum = 0;

while (n > 0) {
int p = n % 10;
sum = sum + p;
n = n / 10;
}
n = sum;
}
printf("%d", n);
}``````

ty bro did it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.