Rohun Rohun

Rohun Rohun

  • NA
  • 2
  • 619

Building a calculation program need help

Dec 9 2018 11:48 AM
So i am building a calculation program the program should ask the user a number with at most 7 digits and then calculate the sum of the numbers For example: number = 1259955 , sum= 36 like this (BY ADDING EACH NUMBER 1+2+5+9+9+5+5 = 36 ) then the program should list all divisors of the sum above Example: sum=8 , divisors = 1,2,3,4,6,9,12,18,36 Like this

I did it but the output is like this:
Sum of the digits 1259955 is 36
The divisors of 36 are 1
The divisors of 36 are 2
The divisors of 36 are 3
The divisors of 36 are 4
The divisors of 36 are 6
The divisors of 36 are 9
The divisors of 36 are 12
The divisors of 36 are 18
The divisors of 36 are 36

I need it like this : The divisors of 36 are: 1,2,3,4,6,9,12,18,36
 
//declaring variables
int number;
int sum = 0;
int val;
//input
Console.WriteLine("Enter a number with at most 7 digits: ");
number = Convert.ToInt32(Console.ReadLine());
val = number;
//calculating sum
while (number != 0)
{
sum += number % 10;
number /= 10;
}
Console.WriteLine("Sum of the digits of " + val + " is " + sum);
//calculating divisors
for (int i = 1; i <= sum; i++)
{
if (sum % i ==0)
{
Console.WriteLine("The divisors of " + sum + " are as follows " + i);
}

Answers (1)