Hey guys, I wanted to create a program that will read an integer and display the sum of the digits of the integer, if the sum of the digits of the integer is more than 9 then the sum of the digits of that number will be calculated as well. This is done until the answer becomes an integer that is between 1 to 9. Don't mind the mechanism if it is confusing to understand, the problem is with the last if statement, I cannot seem to get the right output.
Code:
#include <stdio.h>
int main()
{
int number;
int remainder, remainder2, remainder3, sum=0, sum2=0, sum3=0;
scanf("%d", &number);
while (number!=0)
{
remainder=number%10;
sum=sum+remainder;
number=number/10;
}
if(sum>9)
{
remainder2=sum%10;
sum2=sum2+remainder2;
sum=sum/10;
sum2=remainder2+sum;
}
if(sum2>9){
remainder3=sum2%10;
sum3=sum3+remainder3;
sum2=sum2/10;
sum3=remainder3+sum2;
}
if(sum<=9)
printf("%d \n", sum);
if else(sum2>9)
printf("%d \n", sum2);
else
printf("%d \n", sum3);
}
Specifically the wrong part of the code is
Printing out all the different sums works as I want it to, the If statement logic does not work at all in the part mentioned above.
I really appreciate your help guys, this problem has been a pain in the ass for the past 3 days.