Posts

Program to print the sum of each row and each column of a matrix separately

Program to print the sum of each row and each column of a matrix separately #include <stdio.h> #include <conio.h> # define SIZE 3 void main() { int A[SIZE][SIZE]; int row, col, sum = 0; /* Input elements in matrix from user */ printf ("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE); for (row=0; row<SIZE; row++) { for (col=0; col<SIZE; col++) { scanf ("%d", &A[row][col]); } } /* Calculate sum of elements of each row of matrix */ for (row=0; row<SIZE; row++) { sum = 0; for (col=0; col<SIZE; col++) { sum += A[row][col]; } printf ("Sum of elements of Row %d = %d\n", row+1, sum); } /* Find sum of elements of each columns of matrix */ for (row=0; row<SIZE; row++) { sum = 0; for (col=0; col<SIZE; col++) { sum += A[col][row]; } printf ("Sum of elements of Column %d = %d\n", row+1, sum); } getch(); }

Program to input two matrices and store their product in another third array

Program to input two matrices and store their product in another third array #include<stdio.h> #include<conio.h> void main() { int i, j, k, a[3][3], b[3][3], c[3][3]; clrscr(); printf ("Enter the elements of 1st array .\n"); for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { scanf ("%d", &a[i][j]); } } printf ("Elements of 1st array are: \n"); for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { printf ("%d\t", a[i][j]); } printf ("\n"); } printf ("Enter the elements of 2nd array .\n"); for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { scanf ("%d", &b[i][j]); } } printf ("Elements of 2nd array are: \n"); for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { printf ("%d\t", b[i][j]); } printf ("\n"); } printf ("The product of both the matrices are: \n"); for (i=1;i<...

Program to find the largest and smallest element of a 1D array

Program to find the largest and smallest element of a 1D array #include <stdio.h> #include <conio.h> void main() { int arr[10], i, max, min, a; clrscr(); printf ("Enter the number of the elements of an array: "); scanf ("%d", &a); printf ("Enter %d elements of the array: ", a); for ( i=0; i<a; i++ ) { scanf ("%d", &arr[i]); } max = arr[0]; min = arr[0]; for ( i=1; i<a; i++ ) { if ( arr[i]>max ) { max = arr[i]; } if ( arr[i]<min ) { min = arr[i]; } } printf ("Largest element is %d\n", max); printf ("Smallest element is %d", min); getch(); }

Program to access and print an array of 5 elements.

Program to access and print an array of 5 elements. #include <stdio.h> #include <conio.h> void main() { int num[5], a; clrscr(); printf ("Enter the elements: \n"); for ( a=0;a<5;a++ ) { scanf ("%d", &num[a]); } printf ("The elements are: \n"); for (a=0;a<5;a++) { printf ("%d\t", numb[a]); } getch(); }

Program to return a value from user-defined function.

Program to return a value from user-defined function. #include <stdio.h> #include <conio.h> int sum (); void main() { int a, b, c; clrscr(); printf ("Enter two integer values to add: "); scanf ("%d%d", &a, &b); c = sum (a, b); printf ("The addition is: %d", c); getch(); } int sum (int x, int y) { int add; add = x + y; return add; }

Program to implement break statement.

Program to implement break statement.  #include <stdio.h> #include <conio.h> int main() { int i; double number, sum = 0.0; for (i=1; i <= 10; ++i) { printf ("Enter a n%d: ", i); scanf ("%lf", &number); // If user enters negative number, loop is terminated if (number < 0.0) { break; } sum += number; // sum = sum + number; } printf ("Sum = %.2lf", sum); return 0; } 

Program to find predecessor & successor of a number.

Program to find predecessor & successor of a number. #include <stdio.h> #include <conio.h> void main() { int a, b, c; clrscr(); printf ("Enter a number to find it's predecessor & successor: \n"); scanf (%d, &a); b = a + 1; printf ("The successor of %d is: %d\n", a, b); c = a - 1; printf ("The predecessor of %d is: %d\n", a, c); getch(); }