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 575869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:10+00:00 2026-05-13T13:59:10+00:00

How would I convert this code to have n nested for loops: int num

  • 0

How would I convert this code to have n nested for loops:

            int num = 4;

            for (int i = 0; i <= num; i++)
            {
                for (int j = 0; j + i <= num; j++)
                {
                    for (int k = 0; i + j + k <= num; k++)
                    {
                        for (int l = 0; i + j + k + l <= num; l++)
                        {
                            Console.WriteLine(i + " " + j + " " + k + " " + l);
                        }
                    }
                }
            }

So if num is 2 then there would only be 2 for loops; i and j.

This is NOT homework and I was hoping to do it iteratively. Each Console.WriteLine() needs to be stored as like an element all together.

The output of this programs creates n dimensional hyperspase exponents.

  • 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-13T13:59:11+00:00Added an answer on May 13, 2026 at 1:59 pm

    OK, you want a nonrecursive solution which is parameterized in num and has a constant number of nested loops, yes?

    Here’s a sketch of a method that does that. Filling out the details is left as an exercise.

    First, I assume that you have an immutable type “Vector” which can be a 0-tuple, 1-tuple, 2-tuple, 3-tuple, … n-tuple.

    The method takes in the size of the vector and returns a sequence of vectors of that size.

    IEnumerable<Vector> MakeVectors(int num)
    {
        Vector current = new Vector(num); // make an all-zero vector with num items.
        while(true)
        {
            yield return current;
            Vector next;
            bool gotAnother = GetNextVector(current, out next);
            if (!gotAnother) break;
            current = next;
        }
    }
    

    There. The problem has now been reduced to two smaller problems:

    1) Given a vector of size num, is it the last vector in the sequence?

    2) If not, what is the next vector?

    It should be quite straightforward to work out what the next vector is given the current one: increase the value of the last slot. If that makes it too big, set it to zero and increase the value of the previous slot. Repeat until you’ve found the thing that is to be increased.

    Make sense?

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a Excel CSV files with employee records in them. Something like this:
I have a collection of legacy C code which I'm refactoring to split the
I haven't used rails since version 1.2 or so and a few things have
I have a Page (form) that has many UserControls on it, and I'm trying
I need to flatten objects into nested dicts of the object's properties. The objects
While implementing a design using nested generic collections, I stumbled across those limitations apparently
biglist = [ {'title':'U2 Band','link':'u2.com'}, {'title':'ABC Station','link':'abc.com'}, {'title':'Live Concert by U2','link':'u2.com'} ] I would
I have a business logic classes that are written in pure C# (without any
I'm writing some unit tests and I need to be able to Assert whether

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.