Announcement

Collapse
No announcement yet.

Adding Numbers to get a single Number.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding Numbers to get a single Number.

    Hi ,

    I am looking for a batch script or a excel macro to add/sum a group of numbers to get a SINGLE no. To make it clear I have a list of many nos which need to be added/Sum individually to get a FINAL one digit no.

    Eg:- I have a figure 3023456, so I am expecting a summation like 3+0+2+3+4+5+6 = 23 , again 2+3 = 5(Final One Digit Nos).

    I shall be very thankful for any suggestion and help.

  • #2
    Re: Adding Numbers to get a single Number.

    This batch sample maybe,
    Code:
    @echo off
    
    Set /a figure = 3023456
    Set /a Cnt  = 0
    Set /a No$ = %figure%
    
    :loop
    Call:nums
    If "%No$:~1%" GEQ "0" goto:loop
    
    echo Value is %No$%
    pause
    
    goto :EOF
    :nums
    If "%No$:~0,1%" GEQ "0" (
       set /a cnt=%cnt% + %No$:~0,1%
       Set "No$=%No$:~1%"
       goto:nums )
    Set /a No$=%cnt%
    Set "Cnt="
    exit /b 0
    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Adding Numbers to get a single Number.

      Thanks Rems,
      it works gr8 much appreciated ur efforts, just would like to know how to modify this script so it will take all these nos from a text file automatically and dumping the sum into another file ??

      3023456
      3023457
      3023458
      3023459
      3023460
      3023461
      3023462
      3023463
      3023464
      3023465

      ====
      Value is 5
      Value is 6
      Value is 7
      Value is 8
      .. so on..

      Comment


      • #4
        Re: Adding Numbers to get a single Number.

        Code:
        @echo off
        
        For /f "usebackq tokens=*" %%* in (
           "figures.txt") do Call :MyBatch %%~*
        
        pause
        
        goto :EOF
        ----------------------------------------------------------------
        :nums
        If "%No$:~0,1%" GEQ "0" (
           set /a cnt=%cnt% + %No$:~0,1%
           Set "No$=%No$:~1%"
           goto:nums )
        Set /a No$=%cnt%
        Set "Cnt="
        exit /b 0
        ----------------------------------------------------------------
        
        :MyBatch * * * * * * * *
        
        Set /a figure = %~1
        Set /a Cnt  = 0
        Set /a No$ = %figure%
        
        :loop
        Call:nums
        If "%No$:~1%" GEQ "0" goto:loop
        
        echo The result of %figure% is: %No$%
        rem pause

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Adding Numbers to get a single Number.

          Very thanks REMS, It works like a charm, once again thanks for all help.

          Comment


          • #6
            Re: Adding Numbers to get a single Number.

            Originally posted by amit_singh View Post
            Very thanks REMS, It works like a charm, once again thanks for all help.
            Glad I was able to help.

            /Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Adding Numbers to get a single Number.

              hey Rem,

              I am again into a big fix & I would like to take your help to modify the same script to take input from a text file(Names.txt) having names & last names and converting each names & last names "alphabets" to its predefined below shown "numeric" value and then adding this numeric value to finally get ONE digit numeric value.

              To make it more clear I would like to explain with an example .The Mapping between "Alphabets" & "Number" which I'll use is as below.

              "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u","v", "w", "x", "y", "z"
              "1", "2", "3", "4", "5", "8", "3", "5", "1", "1", "2", "3", "4", "5", "7", "8", "1", "2", "3", "4", "6", "6", "6", "5", "1","7"

              Rozer Gorth =>Equivalent Numeric value: 2775237245 ==>Individually adding each number: 44 ==> Final ONE numeric digit: 8
              Jimy Dacy =>Equivalent Numeric value: 11414131 ==>Individually adding each number: 16 ==>Final ONE numeric digit: 7
              Peter Pace =>Equivalent Numeric value: 854522135 ==>Individually adding each number: 35 ==>Final ONE numeric digit: 8

              So result will be dumped into another file as

              Rozer Gorth : 8
              Jimy Dacy : 7
              Peter Pace : 8

              Comment


              • #8
                Re: Adding Numbers to get a single Number.

                Hi Rem,

                Can you please help me out again ?

                Comment

                Working...
                X