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

  • Home
  • SEARCH
  • 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 732329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:07:44+00:00 2026-05-14T07:07:44+00:00

I was puzzled with one of the question in Microsoft interview which is as

  • 0

I was puzzled with one of the question in Microsoft interview which is as given below:

A function should accept a range( 3 – 21 ) and it should print all the consecutive numbers combinations to form each number as given below:

3  = 1+2
5  = 2+3
6  = 1+2+3
7  = 3+4
9  = 4+5
10 = 1+2+3+4
11 = 5+6
12 = 3+4+5
13 = 6+7
14 = 2+3+4+5
15 = 1+2+3+4+5
17 = 8+9
18 = 5+6+7
19 = 9+10
20 = 2+3+4+5+6
21 = 10+11
21 = 1+2+3+4+5+6

could you please help me in forming this sequence in C#?

Thanks,
Mahesh

  • 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-14T07:07:45+00:00Added an answer on May 14, 2026 at 7:07 am

    So here is a straightforward/naive answer (in C++, and not tested; but you should be able to translate). It uses the fact that

    1 + 2 + … + n = n(n+1)/2,

    which you have probably seen before. There are lots of easy optimisations that can be made here which I have omitted for clarity.


    void WriteAsSums (int n)
    {
      for (int i = 0; i < n; i++)
      {
        for (int j = i; j < n; j++)
        {
          if (n = (j * (j+1) - i * (i+1))/2) // then n = (i+1) + (i+2) + ... + (j-1) + j
          {
            std::cout << n << " = ";
            for (int k = i + 1; k <= j; k++)
            {
              std::cout << k;
              if (k != j) // this is not the interesting bit
                std::cout << std::endl;
              else
                std::cout << " + ";
            }
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some question which puzzled me for a long time. What is the
Question: I've profiled my Python program to death, and there is one function that
I have one question about static and non-static function and variable. 1) non-static function
Weird question, but one that puzzles me a bit. In the old days when
I'm reading the book Spring Recipes right now, and am puzzled by one thing:
I've been pretty puzzled by this one. It seems as though my implementation of
I'm going through a Fortran code, and one bit has me a little puzzled.
First of all, thanks for taking the time to read my question, I appreciate
This question has puzzled me for a while. A public property that can be
This is a compiler error (slightly changed for readability). This one always puzzled me.

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.