Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:08:15+00:00 2026-05-24T03:08:15+00:00

Im trying to do a permutation. of five in this case, so 5,4,3,2,1 .

  • 0

Im trying to do a permutation. of five in this case, so 5,4,3,2,1 . Eventually I want it to permute up to 100 which can be stored in my intX class. the calculation is fine, but I want to add up all individual numbers of the output, using the script below.

so 5! = 5x4x3x2x1 = 120 —-> 1+2+0 = 3. BUT My script below gives the output 147:

120
1
2
0
147

What am I doing wrong? I allready tried all converts, I started with just using the string[pointer] thingy, I tried different arrays etc.. but it all keeps coming up with 147. Is it some kind of representation thing?

    static void Main(string[] args)
    {
        IntX total=1;
        IntX totalsum = 0;
        int perm = 5;
        for (int i = perm; i > 0; i--)
        {
            total = total * i;
        }
        Console.WriteLine(total);

        string answerstring = Convert.ToString(total);
        char[] answerArray = answerstring.ToArray();
        for (int x = 0; x < answerArray.Length; x++)
        {
            totalsum += Convert.ToInt32(answerArray[x]);
            Console.WriteLine(answerArray[x]);
        }
        Console.WriteLine(totalsum);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:08:15+00:00Added an answer on May 24, 2026 at 3:08 am

    The problem is the way you are converting your answerArray elements back to numbers

    Convert.ToInt32(answerArray[x])
    

    The above line takes the char 1 and converts it to an int. This is not the same as parsing it as an int. 1 is ascii character 49 so internally the char has an int representation of 49 and so that is what it is converted to (since this is just trying to do a type conversion rather than any kind of processing)

    Similarly 2 = 50 and 0 = 48 so you get the total of 147.

    What you want to do is use Integer.Parse to parse strings as numbers. I believe it should implicitly convert the char to a string before parsing it.

    So your loop would be:

        for (int x = 0; x < answerArray.Length; x++)
        {
            totalsum += int.Parse(answerArray[x].ToString());
            Console.WriteLine(answerArray[x]);
        }
    

    You can also do it the way others suggested with subtracting chars. This works because the ascii value of 1 is 1 higher than the ascii value for 0. 2 is 2 higher, etc.

    Of course this only works with single digit chars. If you ever want to convert more than two digit numbers into int from a string you’ll need int.parse.

    For what its worth I suspect that the character subtraction method is the most efficient since it is effectively just doing some very simple type conversion and subtraction. The parse method is likely to do a lot more stuff and so be a bit more heavyweight. I dont’ you will notice a performance difference though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For two days I've been running crazy trying to accomplish this, maybe you can
I'm trying to find all permutations of a string and sort them alphabetically. This
Trying to get my css / C# functions to look like this: body {
Trying to make a make generic select control that I can dynamically add elements
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I'm trying to improve my current algorithm for the 8 Queens problem, and this
I'm trying to write a SQL Server database update script. I want to test
I am currently learning Haskell. I am trying to write a function which given
I have been trying to write a C program which generates all possible permutations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.