Jump to content
  • Advertisement
Sign in to follow this  
guzumba

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


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

Share this post


Link to post
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: ");
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???

Share this post


Link to post
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 this post


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

theTroll


touché!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!