58

Suma de una Diagonal de nuestra Matriz en Visual Basic.NET [58]

Para Suma de una Diagonal de nuestra Matriz en Visual Basic.NET vamos a seguir los siguientes pasos:

  • Declaramos una matriz de 5 x 5 y la llenamos con una secuencia de del 1 al 25.
  • Mostramos la matriz.
  • Sumamos la diagonal principal y secundaria.
  • Mostramos las diagonales y su suma.

Codigo para suma de una Diagonal de nuestra Matriz en Visual Basic.NET: https://github.com/programadornovato/VisualBasic/commit/80a3e070fc502dc28854a6cede13aea6661c6dfe

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Imports System
'Autor: Programador Novato
'Fecha: 01/01/2021
'Este modulo sirve para ense�ar como funciona Visual Basic.NET
Module Program
'Este codigo escribe un texto en amarillo con fondo azul
Public Sub Main(args As String())
Dim matriz = New Integer(4, 4) {}
Dim dato = 1
For i = 0 To matriz.GetLength(0) - 1
For j = 0 To matriz.GetLength(1) - 1
matriz(i, j) = dato
dato = dato + 1
Next
Next
For i = 0 To matriz.GetLength(0) - 1
For j = 0 To matriz.GetLength(1) - 1
Console.Write(matriz(i, j) & vbTab)
Next
Console.WriteLine()
Next
Dim diagonalPrincipal = New Integer(matriz.GetLength(0) - 1) {}
Dim diagonalSecundaria = New Integer(matriz.GetLength(0) - 1) {}
For i = 0 To matriz.GetLength(0) - 1
For j = 0 To matriz.GetLength(1) - 1
If i = j Then
diagonalPrincipal(i) = matriz(i, j)
End If
If (i + j) = matriz.GetLength(0) - 1 Then
diagonalSecundaria(i) = matriz(i, j)
End If
Next
Next
Dim suma = 0
Console.WriteLine()
Console.WriteLine("Diagonal principal")
For Each numero In diagonalPrincipal
Console.Write(numero & vbTab)
suma = suma + numero
Next
Console.WriteLine(" = " & suma)
suma = 0
Console.WriteLine()
Console.WriteLine("Diagonal secundaria")
For Each numero In diagonalSecundaria
Console.Write(numero & vbTab)
suma = suma + numero
Next
Console.WriteLine(" = " & suma)
Console.Read()
End Sub
End Module
Imports System 'Autor: Programador Novato 'Fecha: 01/01/2021 'Este modulo sirve para ense�ar como funciona Visual Basic.NET Module Program 'Este codigo escribe un texto en amarillo con fondo azul Public Sub Main(args As String()) Dim matriz = New Integer(4, 4) {} Dim dato = 1 For i = 0 To matriz.GetLength(0) - 1 For j = 0 To matriz.GetLength(1) - 1 matriz(i, j) = dato dato = dato + 1 Next Next For i = 0 To matriz.GetLength(0) - 1 For j = 0 To matriz.GetLength(1) - 1 Console.Write(matriz(i, j) & vbTab) Next Console.WriteLine() Next Dim diagonalPrincipal = New Integer(matriz.GetLength(0) - 1) {} Dim diagonalSecundaria = New Integer(matriz.GetLength(0) - 1) {} For i = 0 To matriz.GetLength(0) - 1 For j = 0 To matriz.GetLength(1) - 1 If i = j Then diagonalPrincipal(i) = matriz(i, j) End If If (i + j) = matriz.GetLength(0) - 1 Then diagonalSecundaria(i) = matriz(i, j) End If Next Next Dim suma = 0 Console.WriteLine() Console.WriteLine("Diagonal principal") For Each numero In diagonalPrincipal Console.Write(numero & vbTab) suma = suma + numero Next Console.WriteLine(" = " & suma) suma = 0 Console.WriteLine() Console.WriteLine("Diagonal secundaria") For Each numero In diagonalSecundaria Console.Write(numero & vbTab) suma = suma + numero Next Console.WriteLine(" = " & suma) Console.Read() End Sub End Module
Imports System

'Autor: Programador Novato
'Fecha: 01/01/2021
'Este modulo sirve para ense�ar como funciona Visual Basic.NET
Module Program
    'Este codigo escribe un texto en amarillo con fondo azul
    Public Sub Main(args As String())
        Dim matriz = New Integer(4, 4) {}
        Dim dato = 1
        For i = 0 To matriz.GetLength(0) - 1
            For j = 0 To matriz.GetLength(1) - 1
                matriz(i, j) = dato
                dato = dato + 1
            Next
        Next
        For i = 0 To matriz.GetLength(0) - 1
            For j = 0 To matriz.GetLength(1) - 1
                Console.Write(matriz(i, j) & vbTab)
            Next
            Console.WriteLine()
        Next
        Dim diagonalPrincipal = New Integer(matriz.GetLength(0) - 1) {}
        Dim diagonalSecundaria = New Integer(matriz.GetLength(0) - 1) {}
        For i = 0 To matriz.GetLength(0) - 1
            For j = 0 To matriz.GetLength(1) - 1
                If i = j Then
                    diagonalPrincipal(i) = matriz(i, j)
                End If
                If (i + j) = matriz.GetLength(0) - 1 Then
                    diagonalSecundaria(i) = matriz(i, j)
                End If
            Next
        Next
        Dim suma = 0
        Console.WriteLine()
        Console.WriteLine("Diagonal principal")
        For Each numero In diagonalPrincipal
            Console.Write(numero & vbTab)
            suma = suma + numero
        Next
        Console.WriteLine(" = " & suma)
        suma = 0
        Console.WriteLine()
        Console.WriteLine("Diagonal secundaria")
        For Each numero In diagonalSecundaria
            Console.Write(numero & vbTab)
            suma = suma + numero
        Next
        Console.WriteLine(" = " & suma)
        Console.Read()
    End Sub
End Module

Curso de VB.NET⛓️: https://www.youtube.com/watch?v=aiquJHzxNWw&list=PLCTD_CpMeEKSFwAFjvrfpvSwxmbs2maMo&ab_channel=programadornovato
[CURSO] C##️⃣: https://www.youtube.com/watch?v=NKPMGY6NCko&list=PLCTD_CpMeEKQSOU8Vf9VHXrZa2rc8X0X5&index=1&t=3s&ab_channel=programadornovatoprogramadornovato
[CURSO] C# CON FORMULARIOS#️⃣: https://www.youtube.com/watch?v=l0_U4oyOuns&list=PLCTD_CpMeEKTBih1VgeunCjc83ZQ6UBMI&index=1&ab_channel=programadornovatoprogramadornovato
[Curso] C# MYSQL#️⃣: https://www.youtube.com/watch?v=-5CXNXHIzWk&list=PLCTD_CpMeEKR_4q0-7BxGHXqH0bgpqw5q&ab_channel=programadornovato
[CURSO] C++ DE 0 A HEROE 🦸: https://www.youtube.com/watch?v=APN8aCyPvww&list=PLCTD_CpMeEKTofxs7iottRxJ5YPM7BOcc&ab_channel=programadornovato
[Curso] Java Netbeans GUI Completo☕: https://www.youtube.com/watch?v=18UA7X2ss8g&list=PLCTD_CpMeEKThfXo8D-RXOGu5FarO7_qv&ab_channel=programadornovato

Anterior tutorial Siguiente tutorial

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *