简体   繁体   中英

Reduce the given number by adding first and last digits

My program that I am building requires me to input a number such as eg 1234546. The program needs to add the left most and right most digit that has not been added yet and put its sum at the end of the result.

static void Main(string[] args)
{
    int num, sum = 0, firstDigit, lastDigit;
 
    // Reading number
    Console.Write("Enter any number: ");
    num = Convert.ToInt32(Console.ReadLine());
 
    lastDigit = num % 10;
 
    firstDigit = num;
 
    while (num >= 10)
    {
        num = num / 10;
    }
    firstDigit = num;
 
    //Finding sum of first and last digit
    sum = firstDigit + lastDigit;
 
    Console.WriteLine("Sum of first and last digit: "+ sum);
 
    Console.ReadLine();
}

This is what I have so far any help?

It might be easier to keep your numbers in strings for this one. It looks like homework so I won't solve the whole thing but:

Chars in c# can be treated like numbers.

int r = '9' - '0'; //r is 9

Subtracting char '0' from a numerical char gives an int of the result you'd expect. Hence the mathematical sum of the first and last chars in a numerical string str could be given by:

int sum = (str.First() - '0') + str.Last() - '0');

For this assignment you should do this if the string is longer than 1 char

If you use a recent version of .net (8+) you can also get the first char with str[0] and the last char with str[^1] rather than LINQ's First and Last

In older c# it's a bit more wordy for the end one: str[str.Length-1]


Substring can be used to chop off the first and last char

str = str.Substring(1, str.Length-2);

In c# 8+ indexes and ranges can be used too which gives a nice easy thing to reason about :

str = str[1..^1];

When you use + with an operand that is a string and another that is an integer, the integer is appended to the end of the string rather than being numerically added to it

string str = "123";
int sum = str[0] + str[^1] - '0' - '0';
string s = "999" + sum; //s is 9994

As such some repeated use of the

  • "first plus last char to give a number sum result",
  • "concatenate the sum result onto a string",
  • "chop the first and last char off and repeat the above, until you get down to either
    • 1 char remaining - just concatenate it or
    • 0 chars remaining - make the new string you've built to be the string you first/last/chop and repeat the process again (until the built string is 2 chars long)

should get you towards what you want. I would recommend you write the algorithm out in comments first and then translate to c#. In my mind it works like this:

//while the result is longer than two chars

  //make a temporary empty string 

  //while true

    //take the first and last chars, sum them, append to the temp
    //chop the result
    //if the result is one char append to the temp and break the loop
    //if the result is 0 chars break the loop

  //make the result equal to the temp

i suggest you to work with string, and testing if number has an odd or even digits:

        var number = "5659679";
        // test if number has number of digits > 2 or no
        var result = number.Length > 2 ? "" : number;
        while (number.Length > 2)
        {
            var l = number.Length;
            result = "";
            for (var i = 0; i < l / 2; i++)
            {
                //add the both start left and end right digits
                result += (number[i]  + number[l - 1 - i] - 2 * '0').ToString();
            }
            //check if number of digit is odd or event
            if (l % 2 != 0)
                result += number[l / 2];

            number = result;
                        
            Console.WriteLine(number);
        }
        Console.WriteLine(result);

this line of code add char as int: 0 = '0' - '0', 1 = '1' - '0' and so on...

result += (number[i]  + number[l - 1 - i] - 2 * '0').ToString()

following the version of c# you are using, you could write instead of: number[l - 1 - i] -> number[^(i + 1)]

test the result Fiddle

you mean basically like this? (this is awk but you can easily adapt it any language of your choice:

 echo 12345678909999333333333336543234569999998765432101010101 \
\
| mawk '{      __=length(_=$(_=___=""))
          _______=(______=(\
            ____+=++____)+__^!__)^____+!!____;
   print _="__"_;

   while(-__<+__) {
   print \
        _=substr((_=substr(_,______))_,__,__--),\
                    ___=(___)"|"((_____=+\
          substr(_,__^!__,____))%_______+\
                       int(_____/_______))
    __-- } }'

__12345678909999333333333336543234569999998765432101010101
11234567890999933333333333654323456999999876543210101010 |2
023456789099993333333333365432345699999987654321010101 |2|2

1345678909999333333333336543234569999998765432101010 |2|2|4
04567890999933333333333654323456999999876543210101 |2|2|4|4
156789099993333333333365432345699999987654321010 |2|2|4|4|6

0678909999333333333336543234569999998765432101 |2|2|4|4|6|6
17890999933333333333654323456999999876543210 |2|2|4|4|6|6|8
089099993333333333365432345699999987654321 |2|2|4|4|6|6|8|8

1909999333333333336543234569999998765432 |2|2|4|4|6|6|8|8|10
20999933333333333654323456999999876543 |2|2|4|4|6|6|8|8|10|2
399993333333333365432345699999987654 |2|2|4|4|6|6|8|8|10|2|12

4999333333333336543234569999998765 |2|2|4|4|6|6|8|8|10|2|12|13
59933333333333654323456999999876 |2|2|4|4|6|6|8|8|10|2|12|13|14
693333333333365432345699999987 |2|2|4|4|6|6|8|8|10|2|12|13|14|15

7333333333336543234569999998 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10
83333333333654323456999999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11
933333333365432345699999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12

9333333336543234569999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12
93333333654323456999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12
933333365432345699 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12

9333336543234569 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12
93333654323456 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12
633365432345 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9

5336543234 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8
43654323 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7
365432 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9

2543 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9|7
34 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9|7|7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM