Public Group

# C# question

This topic is 3833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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].

##### Share on other sites
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.

##### Share on other sites

string number = null;
while (number == null)
{
Console.Write("Enter a number:");

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));

##### Share on other sites
This better not be homework ;-)

int number = 1515; // From user input
int 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));

##### Share on other sites
Of course it is homework. Have you ever had to do something like that other then homework?

theTroll

##### Share on other sites
haha, I think "TheTroll" hit the head of the nail on this. Where else would this make sense?

##### Share on other sites
Ok ive got this far

using System;

public class proj1
{
public static void Main()
{
Console.Write("Enter a 4 digit number: ");
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);
}
}

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???

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by TheTrollOf course it is homework. Have you ever had to do something like that other then homework?theTroll

touché!

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013247
×