Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can i separate number and add them?

How can i separate number and add them?

Scheduled Pinned Locked Moved C#
question
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pakpatel
    wrote on last edited by
    #1

    If i have any number like : 1234 and i want to do 1+2+3+4 = 10 What should i do? and what method should i use ? -Thanks

    Peter

    I A 2 Replies Last reply
    0
    • P pakpatel

      If i have any number like : 1234 and i want to do 1+2+3+4 = 10 What should i do? and what method should i use ? -Thanks

      Peter

      I Offline
      I Offline
      Ian Uy
      wrote on last edited by
      #2

      string number="1234"; int total=0; for(int i=0;i { total+=Int32.Parse(number); }

      It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

      P 1 Reply Last reply
      0
      • I Ian Uy

        string number="1234"; int total=0; for(int i=0;i { total+=Int32.Parse(number); }

        It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

        P Offline
        P Offline
        pakpatel
        wrote on last edited by
        #3

        Thanks i think this is really tricky..any other way you can solve this ?

        Peter

        I 1 Reply Last reply
        0
        • P pakpatel

          Thanks i think this is really tricky..any other way you can solve this ?

          Peter

          I Offline
          I Offline
          Ian Uy
          wrote on last edited by
          #4

          How is it "tricky"?

          It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

          T 1 Reply Last reply
          0
          • I Ian Uy

            How is it "tricky"?

            It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.

            T Offline
            T Offline
            talisker77
            wrote on last edited by
            #5

            Here is a method that can work, should in theory also be able to add numbers in string with chars.

            string s = "1234";
            int total = 0;
            foreach (char c in s)
            {
                int x;
                int.TryParse(c.ToString(), out x);
                total += x;
            }
            
            E 1 Reply Last reply
            0
            • T talisker77

              Here is a method that can work, should in theory also be able to add numbers in string with chars.

              string s = "1234";
              int total = 0;
              foreach (char c in s)
              {
                  int x;
                  int.TryParse(c.ToString(), out x);
                  total += x;
              }
              
              E Offline
              E Offline
              elektrowolf
              wrote on last edited by
              #6

              ..and the shortest one: string s = "1234"; int total = s.ToCharArray().Sum((x) => int.Parse(x.ToString())); ;)

              R 1 Reply Last reply
              0
              • E elektrowolf

                ..and the shortest one: string s = "1234"; int total = s.ToCharArray().Sum((x) => int.Parse(x.ToString())); ;)

                R Offline
                R Offline
                Roger Alsing 0
                wrote on last edited by
                #7

                Naaah ;)

                int total = s.ToCharArray().Sum((x) => ((int)x)-48);

                (ok ok , it is more of a hack ;) )

                My Blog

                E 1 Reply Last reply
                0
                • R Roger Alsing 0

                  Naaah ;)

                  int total = s.ToCharArray().Sum((x) => ((int)x)-48);

                  (ok ok , it is more of a hack ;) )

                  My Blog

                  E Offline
                  E Offline
                  elektrowolf
                  wrote on last edited by
                  #8

                  But you can still leave out the brackets around x :P ;)

                  1 Reply Last reply
                  0
                  • P pakpatel

                    If i have any number like : 1234 and i want to do 1+2+3+4 = 10 What should i do? and what method should i use ? -Thanks

                    Peter

                    A Offline
                    A Offline
                    Alan N
                    wrote on last edited by
                    #9

                    You should use the modulus or remainder function and integer division to solve this problem. Using a divisor of 10 you extract one decimal digit at a time from the number. The same technique with a divisor of 16 can be used to extract hexdecimal digits.

                      Int32 num = 123456789;
                      Int32 sum = 0;
                      Int32 temp = num;
                      while (temp > 0) {  
                        sum += temp % 10;    // sum remainders
                        temp = temp / 10;    // integer division
                      }
                      Console.WriteLine("Sum of all digits in {0} is {1}", num, sum);
                    

                    AlanN

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups