You are on page 1of 8

Name: Bhanesh Gopal K Reg no: 21BEC2149

Digital Assignment -1

1. Write a program to find the given number is an Armstrong number or not.


Ex: 153 = 13 + 53 + 33 = 1+125+27
1634= 14 + 64 + 34 + 44

Code:
#include <stdio.h>
#include <math.h>

int main() {
int num, orgNum, rem, result = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
orgNum = num;
while (orgNum != 0) {
orgNum /= 10;
++n;
}
orgNum = num;
while (orgNum != 0) {
rem = orgNum % 10;
result += pow(rem, n);
orgNum /= 10;
}
if (result == num) {
printf("%d = %d ",result,num);
printf("\n so %d is an Armstrong number.\n", num);
} else {
printf("%d != %d",result,num);
Name: Bhanesh Gopal K Reg no: 21BEC2149

printf("\n so %d is not an Armstrong number.\n", num);


}

return 0;
}

Screenshots of Code:

Screenshot of output:
Name: Bhanesh Gopal K Reg no: 21BEC2149

2. Write a C program to multiply a 2 × 3 matrix with a 3 × 4 matrix and then multiply the
resultant matrix with a 4 × 2 matrix. Define matrix elements through user input. Display
the output in the form a matrix.

Code:
#include <stdio.h>

int main() {
int mat1[2][3], mat2[3][4], mat3[4][2], res1[2][4], res2[2][2];

printf("Enter the elements of first matrix:\n");


for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &mat1[i][j]);
}
}
printf("First matrix:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", mat1[i][j]);
}
printf("\n");
}

printf("Enter the elements of the second matrix:\n");


Name: Bhanesh Gopal K Reg no: 21BEC2149

for (int i = 0; i < 3; ++i) {


for (int j = 0; j < 4; ++j) {
scanf("%d", &mat2[i][j]);
}
}
printf("Second matrix:\n");

for (int i = 0; i < 3; ++i) {


for (int j = 0; j < 4; ++j) {
printf("%d ", mat2[i][j]);
}
printf("\n");
}

printf("Enter the elements of the third matrix:\n");


for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d", &mat3[i][j]);
}
}

printf("Third matrix:\n");
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d ", mat3[i][j]);
}
printf("\n");
}

for (int i = 0; i < 2; ++i) {


Name: Bhanesh Gopal K Reg no: 21BEC2149

for (int j = 0; j < 4; ++j) {


res1[i][j] = 0;
for (int k = 0; k < 3; ++k) {
res1[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

printf("\nThe result after multiplying first and second matrices is:\n");


for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d ", res1[i][j]);
}
printf("\n");
}

for (int i = 0; i < 2; ++i) {


for (int j = 0; j < 2; ++j) {
res2[i][j] = 0;
for (int k = 0; k < 4; ++k) {
res2[i][j] += res1[i][k] * mat3[k][j];
}
}
}

printf("\nThe final result after multiplying all matrices is:\n");


for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d ", res2[i][j]);
}
Name: Bhanesh Gopal K Reg no: 21BEC2149

printf("\n");
}

return 0;
}

Screenshot of Code:
Name: Bhanesh Gopal K Reg no: 21BEC2149
Name: Bhanesh Gopal K Reg no: 21BEC2149

Screenshot of output:

You might also like