Skip to content
Home » C program to find greatest number in a row

C program to find greatest number in a row

Below is the program to find the greatest number in a matrix row

#include <stdio.h>
int main(){
    int r,c;
    printf("Enter number of rows: ");
    scanf("%d",&r);
    printf("Enter number of columns: ");
    scanf("%d",&c);
    int arr[r][c];
    printf("Enter elements of a matrix\n");
    int i=0,j;
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("Enter a%d%d: ",i+1,j+1);
            scanf("%d",&arr[i][j]);
        }
    }
    printf("Elements in the matrix are:\n");
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("%d\t\t",arr[i][j]);
        }
    printf("\n\n");
    }
    int matrix[r],max_num=0;
    i=0;
    while(i<r){
        for(j=0;j<c;j++){
            if(arr[i][j]>max_num){
                max_num=arr[i][j];
            }
        }
        matrix[i]=max_num;
        max_num=0;
        i++;
    }
    for(i=0;i<c;i++){
        printf("Largest number in row %d is %d\n",i,matrix[i]);
    }
}

Output:

Enter number of rows: 2
Enter number of columns: 2
Enter elements of a matrix
Enter a11: 3
Enter a12: 4
Enter a21: 2
Enter a22: 5
Elements in the matrix are:
3 4

2 5

Largest number in row 0 is 4
Largest number in row 1 is 5