SYBCom(CA) DS Practical Slip 13 Q.A

Write a C program to display the total degree of each vertex.

#include <stdio.h>

int main() {
    int n, i, j;

    printf("Enter number of vertices: ");
    scanf("%d", &n);

    int adj[n][n];

    // Input adjacency matrix
    printf("Enter adjacency matrix (%dx%d):\n", n, n);
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &adj[i][j]);
        }
    }

    // Calculate and display total degree of each vertex
    printf("Total degree of each vertex:\n");
    for(i = 0; i < n; i++) {
        int degree = 0;
        for(j = 0; j < n; j++) {
            degree += adj[i][j];
        }
        printf("Vertex %d: %d\n", i + 1, degree);
    }

    return 0;
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top