🔒 Locked
C# question
Advertisement
Original Post
If I write code to first ask the user to input a 4-digit number. Then I want to print the number in reverse followed by the sum of the digits in the number. I cant figure out how to reverse the number and then add the 4 numbers for the sum. Ex. User inputs 1515. Prints 5151, sum 5+1+5+1=12. Thanks, Andy
First a question. Why do you want to reverse the numbers?
Take at look of the String class and the Convert class. These could help you on your way[smile].
Take at look of the String class and the Convert class. These could help you on your way[smile].
This might be an overly complex answer since I don't have VS C# in front of me, but you could put each number in either an array (if you know the amount of number) or a List and then do a for loop starting from (length - 1) until 0. Otherwise some kind of stack implementation might work where you put the numbers on in order and pull them off in a different order.
for (int i = myVector.Length - 1; i >= 0; i--)
{
sEquation = sEquation & " + " & myVector;
iTotal += myVector
}
Again, I'm not in front of VS C# so idk if this is right but it's generally what to do.
for (int i = myVector.Length - 1; i >= 0; i--)
{
sEquation = sEquation & " + " & myVector;
iTotal += myVector
}
Again, I'm not in front of VS C# so idk if this is right but it's generally what to do.
string number = null;while (number == null){ Console.Write("Enter a number:"); number = Console.ReadLine().Trim(); int temp; if (int.TryParse(number, out temp) == false) { number = null; }}StringBuilder reverseNumber = new StringBuilder();int total = 0;for (int index = number.Length - 1; index >= 0; index--){ reverseNumber.Append(number[index].ToString()); total += int.Parse(number[index].ToString());}Console.WriteLine(string.Format("Reverse={0}, Sum={1}", reverseNumber.ToString(), total));
This better not be homework ;-)
int number = 1515; // From user inputint reversed = 0;int crossFoot = 0;while (number > 0){ int digit = number % 10; number /= 10; crossFoot += digit; reversed += digit; reversed *= 10;}reversed /= 10;Console.WriteLine(string.Format("Reverse={0}, CrossFoot={1}", reversed, crossFoot));
Ok ive got this far
Everything works good except the sum. The sum comes up wrong for some reason and I cant figure out why. I used 1515 and it prints 5151 and the sum 204???
using System;public class proj1{ public static void Main() { Console.Write("Enter a 4 digit number: "); string nums = Console.ReadLine(); int numbers = Convert.ToInt32(nums); char n = nums[3]; char n1 = nums[2]; char n2 = nums[1]; char n3 = nums[0]; int s = Convert.ToInt32(n); int s1 = Convert.ToInt32(n1); int s2 = Convert.ToInt32(n2); int s3 = Convert.ToInt32(n3); int total = (s + s1 + s2 + s3); int num = numbers % 10; int num1 = numbers / 10; Console.Write("\nReversed: "); Console.Write(num); Console.WriteLine(num1); Console.Write("Sum of digits: "); Console.Write(total); Console.ReadLine(); }}Everything works good except the sum. The sum comes up wrong for some reason and I cant figure out why. I used 1515 and it prints 5151 and the sum 204???
Ok, a few problems I can see.
First what happened if they enter 52351? Bad things.
What happens if they enter 3B1C? More bad things.
No reason to have individual chars. myString[0] does the same thing.
char.IsDigit, is pretty handy.
I am pretty sure this is homework, so that is why I am just giving hints.
theTroll
First what happened if they enter 52351? Bad things.
What happens if they enter 3B1C? More bad things.
No reason to have individual chars. myString[0] does the same thing.
char.IsDigit, is pretty handy.
I am pretty sure this is homework, so that is why I am just giving hints.
theTroll
Quote:
Original post by TheTroll
Of course it is homework. Have you ever had to do something like that other then homework?
theTroll
touché!
Quote:
Original post by guzumba
Ok the reverse is wrong when I try 1234. It says to only use mathmatical operations so Im not sure how to fix that?
Look at my code, it does exactly that.
The % operation needs to happen in a loop. It only works for 1515 because it'll result in 151 and 5, which you put backwards (5 and then 151)
% is the 'remainder of a division' operator (modulo). If you divide a number by 10, the % will always be the last decimal digit.
Here's how it typically works when you split an integer by digit:
For example using 1234, this would result in:
value = 1234
start loop.
nextLowestDigit = 4
value = 123
nextLowestDigit = 3
value = 12
nextLowestDigit = 2
value = 1
nextLowestDigit = 1
value = 0
end of loop.
Notice that the 'nextLowestDigit' are split off in reverse order, which is exactly what you want anyway.
% is the 'remainder of a division' operator (modulo). If you divide a number by 10, the % will always be the last decimal digit.
Here's how it typically works when you split an integer by digit:
while (value != 0) // this is a loop that repeats while the condition in parenthesis is true{ nextLowestDigit = value % 10; value /= 10;}For example using 1234, this would result in:
value = 1234
start loop.
nextLowestDigit = 4
value = 123
nextLowestDigit = 3
value = 12
nextLowestDigit = 2
value = 1
nextLowestDigit = 1
value = 0
end of loop.
Notice that the 'nextLowestDigit' are split off in reverse order, which is exactly what you want anyway.
Quote:
Its not homework im reading my book and its one of the challenges at the end of the chapter. So if you could give me more then hints that would be helpful.
Re-read the chapter. The challenges are generally to test how much you learned reading the chapter.
I'll do your homework based on your question though:
private string StringReverse( string S ) { string RetVal = ""; for ( int x = 0; x < S.Length; x++ ) { RetVal = S[x] + RetVal; } return RetVal; } private int CountSumFromString( string S ) { int Sum = 0; for ( int x = 0; x < S.Length; x++ ) { Sum += int.Parse(""+S[x]); } return Sum; } string StringEntered = "1515"; string ReverseString = StringReverse(StringEntered); int SumOfString = CountSumFromString(ReverseString);If the chapter talked about using Convert.ToInt32, or %, or whatever else these other guys posted, your teacher will probably dock ya, cause I didn't use em at all. ;)
It's printing 204 for you because you're adding chars, not ints. (http://www.asciitable.com/) 1 in ASCII is 49, and 5 in ASCII is 53. 49+53+49+53 is 204. If you change my equation in my CountSumFromString function to "Sum += S[x];" you'll see it do the same wrong thing...
Hope this helps.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement