diff --git a/C_basic_programs/TRANSPOSE_0FMATRIX.c b/C_basic_programs/TRANSPOSE_0FMATRIX.c index b547ef9..17dd4a3 100644 --- a/C_basic_programs/TRANSPOSE_0FMATRIX.c +++ b/C_basic_programs/TRANSPOSE_0FMATRIX.c @@ -1,29 +1,38 @@ -#include +// C program to find +// transpose of a matrix -int main() -{ - int m, n, c, d, matrix[10][10], transpose[10][10]; +#include +#define N 4 - printf("Enter the number of rows and columns of a matrix\n"); - scanf("%d%d", &m, &n); - printf("Enter elements of the matrix\n"); +// This function stores transpose of A[][] in B[][] +void transpose(int A[][N], int B[][N]) +{ + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + B[i][j] = A[j][i]; +} - for (c = 0; c < m; c++) - for (d = 0; d < n; d++) - scanf("%d", &matrix[c][d]); +// Driver code +int main() +{ + int A[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; - for (c = 0; c < m; c++) - for (d = 0; d < n; d++) - transpose[d][c] = matrix[c][d]; + // Note dimensions of B[][] + int B[N][N], i, j; - printf("Transpose of the matrix:\n"); + // Function call + transpose(A, B); - for (c = 0; c < n; c++) - { - for (d = 0; d < m; d++) - printf("%d\t", transpose[c][d]); - printf("\n"); - } + printf("Result matrix is \n"); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + printf("%d ", B[i][j]); + printf("\n"); + } - return 0; + return 0; } diff --git a/C_basic_programs/armstrong.c b/C_basic_programs/armstrong.c index 3959da5..24bdb43 100644 --- a/C_basic_programs/armstrong.c +++ b/C_basic_programs/armstrong.c @@ -1,25 +1,62 @@ +// C program to find Armstrong number #include -int main() + +// Function to calculate x raised to +// the power y +int power(int x, unsigned int y) +{ + if (y == 0) + return 1; + if (y % 2 == 0) + return power(x, y / 2) * power(x, y / 2); + return x * power(x, y / 2) * power(x, y / 2); +} + +// Function to calculate order of the number +int order(int x) { - int num, originalNum, remainder, result = 0; - printf("Enter a three-digit integer: "); - scanf("%d", &num); - originalNum = num; + int n = 0; + while (x) { + n++; + x = x / 10; + } + return n; +} - while (originalNum != 0) - { - // remainder contains the last digit - remainder = originalNum % 10; +// Function to check whether the +// given number is Armstrong number or not +int isArmstrong(int x) +{ + // Calling order function + int n = order(x); + int temp = x, sum = 0; + while (temp) { + int r = temp % 10; + sum += power(r, n); + temp = temp / 10; + } - result += remainder * remainder * remainder; + // If satisfies Armstrong condition + if (sum == x) + return 1; + else + return 0; +} - originalNum /= 10; - } +// Driver Code +int main() +{ + int x = 153; + if (isArmstrong(x) == 1) + printf("True\n"); + else + printf("False\n"); - if (result == num) - printf("%d is an Armstrong number.", num); - else - printf("%d is not an Armstrong number.", num); + x = 1253; + if (isArmstrong(x) == 1) + printf("True\n"); + else + printf("False\n"); - return 0; + return 0; }