本文介绍了数字ex 9 + 8 = 17的数字之和应该是1 + 7 = 8应答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助来计算一些大于9的数字。
例如: - 如果我计算 9 + 8 = 17
的总和,我想再次将它们压缩成单个数字再次添加它们请帮帮我。
我申请了
Hi,
I need help to calculate some of digits of number greater than 9.
For example :- If I calculate sum of 9 + 8 = 17
, again I want to condense this some into single digit by adding them again please help me in this.
I have applied
while(number!=0)
{
rem=number%10;
sum=sum+rem;
number=number/10;
}
这很好,但我想把它压缩成一位数。
This is fine, but I want to condense it into single digit.
推荐答案
dr(n) =
0 if n == 0
9 if n != 0 and n is divisable by 9
n mod 9 in all other cases
请参阅 []
这使得实现该功能变得非常容易,即使不使用循环:
see Wikipedia:Digital Root[^]
That makes it really easy to implement that function, even without using a loop:
int DigitalRoot (int n)
{
if (n == 0)
return 0;
else
{
int m = n % 9;
return (m == 0) ? 9 : m;
}
}
int reducer(int number)
{
int sum = 0;
while(number != 0)
{
rem = number % 10;
sum += rem;
number /= 10;
}
if (sum > 10)
sum = reducer(sum);
return sum;
}
#include<stdio.h>
void main()
{
int num1, num2, number , sum = 0, rem = 0, count = 0, next = 0;
printf("Enter two nos");
scanf("%d %d", &num1, &num2);
number = num1 + num2;
if(number == 0)
{
printf("Sum: %d", sum);
}
for( ;number != 0; )
{
//finding the sum of the digits of the sum of the origional numbers
while(number != 0)/
{
rem = number % 10;
sum = sum + rem;
number = number / 10;
}
next = sum;
//checking the no of digits of the sum calculated in the above while loop
while(next != 0)
{
rem = number % 10;
next = next / 10;
count++;
}
//if the digit is single then the number is printed
if(count == 1)
{
printf("Sum: %d", sum);
//go out of the loop
break;
}
//if the no has more than 1 digits,then some variables are changed and as per next ,it's already 0 in the second for loop
else
{
number = sum;
sum = 0;
count = 0;
}
}
}
这篇关于数字ex 9 + 8 = 17的数字之和应该是1 + 7 = 8应答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!