C# Program to find Number Combination

You are Here:

Find Number Combination

In the following example, we will find all possible combinations of numbers with 4 and 8 within the limit 500.

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { int combination = 2; int[] num = {4, 8}; int limit = 500; int i, j, lastDigit, copyNum, digit, count; Console.WriteLine("List of combinations of 4 and 8 upto 500: "); // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) Console.Write(i +" "); } } } }

Output

List of combinations of 4 and 8 upto 500: 4 8 44 48 84 88 444 448 484 488

Find Number Combination for any Given Numbers

In the following example, we will find all possible combinations of the given numbers within the given limit.

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { int i, j, lastDigit, copyNum, digit, count; int[] num = new int[50]; Console.Write("Enter the number of Combination: "); int combination = Convert.ToInt32(Console.ReadLine()); for(i=0; i<combination; i++) { Console.Write("Enter Digit {0}: ", i+1); num[i] = Convert.ToInt32(Console.ReadLine()); } Console.Write("Enter the Limit: "); int limit = Convert.ToInt32(Console.ReadLine()); // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) Console.Write(i +" "); } } } }

Output

Enter the number of Combination: 2 Enter Digit 1: 1 Enter Digit 2: 2 Enter the Limit: 500 1 2 11 12 21 22 111 112 121 122 211 212 221 222

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C# with examples for quick and easy learning.

We are working to cover every Single Concept in C#.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author