Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C#] Sum of first x natural numbers

  1. Jun 5, 2014 #1

    adjacent

    User Avatar
    Gold Member

    I am writing this in C#. Here is the code.
    Code (Text):

    using System;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
               
                int sum = 0;
                int uservalue;
                Int32.TryParse(Console.ReadLine(),out uservalue);
                for (int i = 0;i <= uservalue;i++)
                {
                    sum += i;
                }
                    Console.WriteLine("Sum of the Natural numbers from 0 to {1} is:{0}",sum,uservalue);
                    Console.ReadLine();
                    Console.ReadLine();
            }
        }
    }
    Problems;
    1. If I write 100 in the console, it assumes that I wrote 57 :confused:
    2. How do I make it possible for the user to write and calculate the sum as much as he/she wants? I wrote two Console.ReadLines there. So it allows me to write only one number, after getting that string, if I write another number, the console closes :confused:
     
    Last edited: Jun 5, 2014
  2. jcsd
  3. Jun 5, 2014 #2

    Borek

    User Avatar

    Staff: Mentor

    Have you checked docs? What does Console.Read() return?

    No idea why 57, I would expect 49.

    Then

    [tex]\sum_{i=1}^n i= \frac {n(n+1)}2[/tex]
     
  4. Jun 5, 2014 #3

    adjacent

    User Avatar
    Gold Member

    Oh, I got it.
    The code is now updated,but still, I don't know how to solve problem 2 :confused:
    Yes, I know this. However, I want to learn how to do it the long way :smile:
     
  5. Jun 5, 2014 #4

    Borek

    User Avatar

    Staff: Mentor

    The most obvious approach - loop that ends on a particular input.
     
  6. Jun 5, 2014 #5

    adjacent

    User Avatar
    Gold Member

    I have found something : While(true)
    {

    }
    I put all my code into this and it's alright

    I have noticed that large numbers return absurd results, for example, if I write 10000000, it returns -2004260032 . Absurd.
     
  7. Jun 5, 2014 #6

    adjacent

    User Avatar
    Gold Member

    This is my code now
    Code (Text):

    using System;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                int uservalue;
                int sum = 0;
                Console.WriteLine("Calculate the Sum of Natural numbers from 0 to your number");
                Console.WriteLine("----");
                while(true)
                {
                    Console.Write("Enter Number:");
                    Int32.TryParse(Console.ReadLine(),out uservalue);
                    if(uservalue==0)
                    {
                        Console.WriteLine("Invalid Number");
                    }
                    else
                    {
                        for (int i = 0; i <= uservalue; i++)
                            {
                                sum += i;
                            }
                        Console.WriteLine("Sum of the Natural numbers from 0 to {1} is:{0}", sum, uservalue);
                        sum = 0;
                    }    
                   
                }
               
            }
        }
    }
     
     
  8. Jun 5, 2014 #7

    Borek

    User Avatar

    Staff: Mentor

    What are limits to the value that can be stored in a signed, 32 bit int?
     
  9. Jun 5, 2014 #8

    adjacent

    User Avatar
    Gold Member

    ##2^{32}\text{ Which is equal to }4294967296##
     
  10. Jun 5, 2014 #9

    jtbell

    User Avatar

    Staff: Mentor

    Your "while(true)" loop is an "infinite loop", that is, you presumably have to do something like control-C to abort it when you don't want to find any more sums. You might want to consider how to end the program more "gracefully" from the user's point of view.
     
  11. Jun 5, 2014 #10

    Borek

    User Avatar

    Staff: Mentor

    That's actually wrong, you missed the "signed" part, but even if it were correct - what is the expected sum of numbers up to 10000000?
     
  12. Jun 5, 2014 #11

    adjacent

    User Avatar
    Gold Member

    The user can click on the cross to finish it :smile:
     
  13. Jun 5, 2014 #12

    adjacent

    User Avatar
    Gold Member

    Oh it's 2,147,483,647 and -2,147,483,647

    Sum of natural numbers up to 10000000 is ##5.0000005 \times 10^{13}##
     
  14. Jun 5, 2014 #13

    Borek

    User Avatar

    Staff: Mentor

    And you still don't see where the problem is?
     
  15. Jun 5, 2014 #14

    DavidSnider

    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C#] Sum of first x natural numbers
  1. Python or C++ first? (Replies: 20)

  2. C++ power of n number (Replies: 13)

Loading...