C# question
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<int> 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));
Of course it is homework. Have you ever had to do something like that other then homework?
theTroll
theTroll
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é!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement