Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to print all possible ways to write a number (N) as a sum of two or more positive integers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class AllWaysToWriteANumberAsSumOfTwoOrMoreInts_VB
    Public Shared Sub printList(ByVal list As IList(Of Integer))
        If list.Count <> 1 Then
            For i As Integer = 0 To list.Count - 1
                If i < list.Count - 1 Then
                    Console.Write(list(i) & " + ")
                Else
                    Console.Write(list(i))
                End If
            Next
        End If

        Console.WriteLine()
    End Sub

    Public Shared Sub allWaysToWriteANumberAsSumOfTwoOrMoreInts(ByVal list As IList(Of Integer), ByVal i As Integer, ByVal n As Integer)
        If n = 0 Then
            printList(list)
        End If

        For j As Integer = i To n
            list.Add(j)
            allWaysToWriteANumberAsSumOfTwoOrMoreInts(list, j, n - j)
            list.RemoveAt(list.Count - 1)
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim n As Integer = 5

        Dim list As IList(Of Integer) = New List(Of Integer)()

        allWaysToWriteANumberAsSumOfTwoOrMoreInts(list, 1, n)
    End Sub
End Class



' run:
'
' 1 + 1 + 1 + 1 + 1
' 1 + 1 + 1 + 2
' 1 + 1 + 3
' 1 + 2 + 2
' 1 + 4
' 2 + 3
'

 



answered Aug 3, 2024 by avibootz
...