Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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

My code i wrote is executing the sum part but not the divisor part

Also when the line Console.Writeline("Sum of the digits of" + number + "is"+ sum) is executing The output is : Sum of the digits 0 is 36 Why is it saying 0

What I have tried:

C#
int number;
        int sum = 0;
        Console.WriteLine("Enter a number with at most 7 digits: ");
        number = Convert.ToInt32(Console.ReadLine());

        while (number != 0)
        {
            sum += number % 10;
            number /= 10;
        }

        Console.WriteLine("Sum of the digits of " + number + " is " + sum);



        for (int i = 1; i <= number; i++)
        {
            if (number % i ==0)
            {

                Console.WriteLine("The divisors of" + sum + " are " + i.ToString());
            }


        }

        Console.ReadLine();
    }
}
Posted
Updated 9-Dec-18 8:03am
v2
Comments
Richard MacCutchan 9-Dec-18 11:21am    
Your initial loop reduces number to zero. Please try reading your code through if it does not do what you expect.

Hello,
Hope you are doing well.

there is a bit problem in your algorithm you are using a 'number' as an integer to get data from a user and using the same variable inside the while loop makes it value 0,

that's why it says
Sum of the digits 0 is 36


and this is the reason the for loop is not executing because of the number=0.



Solution is simple

take another variable save the original value in it and that's it.

int number,val;
            int sum = 0;
            Console.WriteLine("Enter a number with at most 7 digits: ");
            number = Convert.ToInt32(Console.ReadLine());
            val = number;
            while (number != 0)
            {
                sum += number % 10;
                number /= 10;
            }

            Console.WriteLine("Sum of the digits of " + val + " is " + sum);



            for (int i = 1; i <= val; i++)
            {
                if (val % i == 0)
                {

                    Console.WriteLine("The divisors of " + sum + " are " + i.ToString());
                }


            }

            Console.ReadLine();
 
Share this answer
 
v2
Comments
Member 14075859 9-Dec-18 12:08pm    
yeah thanks it works now
but also i need to to display all the divisors on one line without again displaying :
Enter a number with at most 7 digits:
1259955
Sum of the digits 1259955 is 36
The divisors of36 are 1
The divisors of36 are 2
The divisors of36 are 3
The divisors of36 are 4
The divisors of36 are 6
The divisors of36 are 9
The divisors of36 are 12
The divisors of36 are 18
The divisors of36 are 36

i want it to display it like : The divisors of 36 are: 1,2,3,4,6,9,12,18,36
Patrice T 9-Dec-18 14:05pm    
if problem is solved, accept useful solution.
it will tell everyone that help is no more needed.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900