Q:

Pyramid Programs in VB (Visual Basic)

belongs to collection: Miscellaneous Programs Examples

0

The pyramid is like a triangular structure. Pyramid programs are used to extend coding, thinking, logic, looping and nested looping concepts. The interviewer usually asks these patterns to examine the logic and programming skills of the interviewee. In this topic, we will learn how to create various pyramid patterns in Visual Basic.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1. Star Pattern Programs

Program to print the Half Pyramid

Module1.vb

Imports System  
Module Module1  
    Sub Main()  
        Dim a, b, num As Integer  ' initialize the local variable  
        Console.WriteLine(" Enter the number of rows: ")  
        num = Console.ReadLine() ' take an input from the user  
        'Outer Loop  
        For a = 1 To num  
            'Inner Loop  
            'Value of b should be less than a  
            For b = 1 To a  
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine("") ' jump to new line  
        Next  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print an inverted half pyramid

Module2.vb

Imports System  
Module Module2  
    Sub Main()  
        Dim n, i, j As Integer  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine() ' take an input from the user  
  
        For i = n To 1 Step -1 ' decrement loop by 1  
            For j = 1 To i ' value of j less than i   
                Console.Write("* ") ' print the star  
            Next  
  
            Console.WriteLine("")  ' jump to new line  
        Next  
  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

 

Output:

 

Program to print an inverted full pyramid

Module3.vb

Module Module3  
    Sub Main()  
        Dim n, i, j, k As Integer  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine()  ' take an input from the user  
        For i = n To 1 Step -1 ' value of Outer loop is decremented by 1  
            For j = 1 To m ' value of j should be less than m  
                Console.Write(" ") ' print a white space  
            Next  
            For k = 1 To 2 * i - 1 ' define the condition for printing a pattern  
                Console.Write("*") ' print the pattern  
            Next  
            m += 1 ' incremented by 1  
            Console.WriteLine("")  
        Next  
  
        Console.WriteLine(" Press any key to exit...")  
            Console.ReadKey()  
        End Sub  
  
    End Module  

Output:

 

Program to print the full pyramid

Module4.vb

Module Module4  
    Sub Main()  
  
        Dim i, n, j, k As Integer  
        Dim space As Integer = 1 ' declare the space value by 9  
        Console.WriteLine(" Enter a number to show rows in a Pattern")  
        ' take a number from user    
        n = Console.ReadLine()  
        Console.WriteLine()  
        'Outer loop    
        For i = 1 To n  
            'Inner loop    
            For j = 1 To n - 1 ' value of j less than n-1  
                Console.Write(" ") ' print a space  
            Next  
            For k = 1 To 2 * i - 1 ' define the condition  
                Console.Write("*" + "") ' print star and a space at right end  
            Next  
            Console.WriteLine("")  
            n -= 1 ' decrement n by 1  
        Next  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
  
  
End Module 

 

Output:

 

Program to print two half pyramids

Module5.vb

Module Module5  
    Sub main()  
        Dim i, j, num As Integer  
        Dim ch As Char = "A" ' initialize a character variable  
                Console.WriteLine(" Enter the total number of rows")  
        num = Convert.ToInt32(Console.ReadLine())  
        For i = 1 To num ' value of i is less than num  
            For j = 1 To i ' value of j is less than i   
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine() ' jump to next line  
        Next  
  
        For i = 1 To num ' value of  i is less than num  
            For j = 1 To i  
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine()  
        Next  
        Console.ReadKey()  
  
    End Sub  
End Module  

Output:

 

Program to print a pyramid shape

Module6.vb

Module Module6  
    Sub main()  
    ' define the local variables  
        Dim i, j, num As Integer  
        Dim ch As Char = "A"  
        Console.WriteLine(" Enter the character in uppercse of rows")  
        num = Convert.ToInt32(Console.ReadLine())  
        For i = 1 To num ' value of i is less than num  
            For j = num To i Step -1 ' initialize j with num and decremented by 1  
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine() ' jump to new line  
        Next  
        For i = 1 To num ' value of i is less than num  
            For j = 1 To i ' value of j is less than i  
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine()  
        Next  
  
        Console.ReadKey()  
  
    End Sub  
End Module  

Output:

 

Program to print a full pyramid

Module7.vb

Imports System  
Module Module7  
    Sub Main()  
        Dim n, i, j As Integer  
  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine()  
        For i = 1 To n ' value of i is less than n  
            For j = 1 To i ' value of j is less than i  
                Console.Write("* ") ' print the star  
            Next  
            Console.WriteLine("")   
        Next  
        For i = n To 1 Step -1 ' value of i is decremented by 1  
            For j = 1 To i  
                Console.Write("* ") ' pattern the star  
            Next  
            Console.WriteLine("")  
        Next  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print a right inverted pyramid

Module8.vb

Module Module8  
    Sub main()  
    ' define the local variable  
        Dim i, j, k, n, m As Integer  
        Console.WriteLine("Enter the rows")  
        n = Console.ReadLine() ' take an input from the user  
        m = n ' assign the user value to m  
        For i = 1 To n ' value of i is less than n  
            For j = 1 To i ' value of j is less than i   
                Console.Write(" ") ' print the space  
            Next  
            For k = 1 To m ' k is less than m  
                Console.Write("*") ' print the star  
            Next  
            m -= 1 ' decrement m by 1  
            Console.WriteLine("")  
        Next  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print a right pyramid

Module9.vb

Module Module9  
    Sub main()  
        Dim i, j, k, n, m As Integer  
        Console.WriteLine("Enter the rows")  
        n = Console.ReadLine()  
  
        For i = n To 1 Step -1 ' initialize the i with user input and decrement by 1  
            For j = 1 To i - 1 ' value of j is less than i-1  
                Console.Write(" ") ' print the space  
            Next  
            For k = 1 To m ' value  of k is less than m  
                Console.Write("*") ' print the *  
            Next  
            Console.WriteLine("")  
            m += 1 ' increment m by1  
        Next  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

2. Number Pattern Program

Program to print the Half Pyramid of number

Module1.vb

Module Module1  
    Sub Main()  
            'Initialize the local variables a, b and num  
            Dim a, b, num As Integer  
  
            Console.WriteLine(" Enter the number of rows: ")  
            num = Console.ReadLine()  ' take an input from the user  
        Console.WriteLine()  
        'Outer Loop  
        For a = 1 To num  
            'Inner Loop  
            'Value of b should be less than a  
            For b = 1 To a  
                Console.Write("{0} ", b) ' print the number  
            Next  
            Console.WriteLine("") ' jump to new line  
        Next  
        Console.WriteLine(" Press any key to exit...")  
            Console.ReadKey()  
        End Sub  
    End Module  

Output:

 

Program to print the Floyd's Triangle

Module2.vb

Module Module2  
    Sub Main()  
        Dim n, i, j As Integer  ' define the local variable  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine() 'take an input from the user  
        Console.WriteLine()  
    ' Outer loop  
        For i = 1 To n  
    ' inner loop  
            For j = 1 To i  
                Console.Write("{0} ", m)  ' print the incremented value of m  
                m += 1  ' increment the value of m  
            Next  
            Console.WriteLine("") ' jump to new line  
        Next  
  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print the Half Pyramid of the same number

Module3.vb

Module Module3  
  
    Sub Main()  
        ' initialize the local variables a, b, m and num  
        Dim a, b, num As Integer  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows: ")  
            num = Console.ReadLine()  ' take an integer from the user  
        Console.WriteLine()  
        'Outer Loop  
        For a = 1 To num  
            'Inner Loop  
            'Value of b should be less than a  
            For b = 1 To a  
                Console.Write("{0} ", m) ' print the value of m  
            Next  
            Console.WriteLine("")  
        Next  
        Console.WriteLine(" Press any key to exit...")  
            Console.ReadKey()  
        End Sub  
    End Module  

Output:

 

Program to print an inverted half pyramid of number

Module4.vb

Module Module4  
    Sub Main()  
        Dim n, i, j As Integer  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine()  
  
        For i = n To 1 Step -1 ' decrement value by 1  
            For j = 1 To i ' increment by 1 and j should be less than i  
                Console.Write("{0} ", j) ' print the value of j  
            Next  
  
            Console.WriteLine("") ' jump to new line  
        Next  
  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print an inverted half pyramid of numbers

Module5.vb

Module Module5  
    Sub Main()  
        Dim n, i, j As Integer  
        Dim m As Integer = 1  
        Console.WriteLine(" Enter the number of rows")  
        n = Console.ReadLine() ' take an input from the user  
  
        For i = n To 1 Step -1 ' outer loop decremented by 1  
            For j = 1 To i ' value of j is less than i  
                Console.Write("{0} ", i) ' print the value of i  
            Next  
  
            Console.WriteLine("") ' jump to next line at each iteration  
        Next  
  
        Console.WriteLine(" Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

Output

 

Program to print the Pascal's triangle

Module6.vb

Module Module6  
    Sub main()  
    ' initialize the local variable  
        Dim i, j, k, l, n As Integer  
        Console.WriteLine("Enter the number of rows")  
        n = Console.ReadLine() ' take an input variable from the user  
        For i = 1 To n ' outer loop  
            For j = 1 To n - i ' value of j is less than n-1   
                Console.Write(" ") ' print a white space  
            Next  
            For k = 1 To i  
                Console.Write("{0}", k) ' print k  
            Next  
            For l = i - 1 To 1 Step -1 ' initialize l with i-1 and decrement by 1  
                Console.Write("{0}", l) ' print l  
            Next  
            Console.WriteLine("")  
        Next  
        Console.WriteLine("Press any key to exit...")  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

3. Alphabets Pattern Program

Program to print a half pyramid of alphabets

Module1.vb

Module Module1  
    Sub main()  
        Dim i, j As Integer  
        Console.WriteLine("Print the Alphabets pattern")  
        For i = 65 To 75 Step 1 ' value of i is less than 75  
            For j = 65 To i ' value of j is less than i  
                Console.Write("{0}", Chr(j)) ' print j  
            Next  
            Console.WriteLine("")  
        Next  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

 

Program to print a half pyramid of alphabets

Module2.vb

Module Module2  
    Sub main()  
        Dim i, j As Integer  
        Console.WriteLine("Print the Alphabets pattern")  
        For i = 65 To 75 Step 1 ' initialize I with 65 and less than 75  
            For j = 65 To i ' value of j is less than i  
                Console.Write("{0} ", Chr(i)) ' print the value of i  
            Next  
            Console.WriteLine("") ' new line  
        Next  
        Console.ReadKey()  
    End Sub  
End Module  

Output:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Miscellaneous Programs Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Program to calculate Volume of Cone... >>
<< Program to Convert Kilometer to cm...