Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update armstrong.c #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions C_basic_programs/TRANSPOSE_0FMATRIX.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
#include <stdio.h>
// C program to find
// transpose of a matrix

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
#include <stdio.h>
#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;
}
71 changes: 54 additions & 17 deletions C_basic_programs/armstrong.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
// C program to find Armstrong number
#include <stdio.h>
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;
}