My program that I am building requires me to input a number such as e.g. 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 ?
CodePudding user response:
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
CodePudding user response:
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()
test the result Fiddle