1 April 2020

Find all possible combinations of numbers in c#


Code:

using System;
using System.Collections.Generic; namespace All_Possible_Combinations_of_a_number { class Program { static void Main(string[] args) { Console.WriteLine("Enter a Number:"); int number = Convert.ToInt32(Console.ReadLine()); List<int> listNumbers = new List<int>(); for (int i=1;i<=number;i++) { listNumbers.Add(i); } Console.WriteLine("Output:"); double count = Math.Pow(2, listNumbers.Count); for (int i = 1; i <= count - 1; i++) { string aa = ""; for (int j = 0; j < listNumbers.Count; j++) { int b = i & (1 << j); if (b > 0) { aa = aa + listNumbers[j]; } } Console.WriteLine(aa); } Console.ReadLine(); } } }


Output:


No comments:

Post a Comment