Uploaded by fagip34627

c program 6 answers

advertisement
1.Sort values in a array using pointers.(Ascending order, No repeated values in the array)
•
•
#include <stdio.h>
void sort(int *arr, int n) {
•
int i, j, temp;
•
for (i = 0; i < n; i++) {
•
for (j = i + 1; j < n; j++) {
•
if (*(arr + j) < *(arr + i)) {
•
temp = *(arr + i);
•
*(arr + i) = *(arr + j);
•
*(arr + j) = temp;
•
}
•
}
•
•
•
}
}
int main() {
•
int arr[20],a,i;
•
printf("Enter no of elements:");
•
scanf("%d",&a);
printf("Enter the elements:");
•
for (i = 0; i < a; i++)
•
{
•
scanf("%d",&arr[i]);
•
}
•
•
int n = sizeof(a) / sizeof(arr[0]);
•
sort(arr, n);
•
for (int i = 0; i < n; i++)
•
•
printf("%d ", arr[i]);
return 0;}
.
2
Check if the given number is a palindrome using pointers.
• #include <stdio.h>
•
int is_palindrome(int *num) {
•
int temp = *num;
•
int reversed = 0;
•
while (temp != 0) {
•
reversed = reversed * 10 + temp % 10;
•
temp /= 10;
•
}
•
return reversed == *num;
• }
•
int main() {
•
int num;
•
printf("Enter any number:");
•
scanf("%d",&num);
•
if (is_palindrome(&num))
•
•
•
•
• }
printf("%d is a palindrome\n", num);
else
printf("%d is not a palindrome\n", num);
return 0;
3. Check if a given matrix is a upper triangular matrix.
•
•
#include <stdio.h>
int is_upper_triangular(int n, int matrix[n][n]) {
•
for (int i = 1; i < n; i++)
•
for (int j = 0; j < i; j++)
•
if (matrix[i][j] != 0)
•
return 0;
•
•
•
return 1;
}
int main() {
•
int n;
•
printf("Enter the size of the matrix: ");
•
scanf("%d", &n);
•
int matrix[n][n];
•
printf("Enter the elements of the matrix:\n");
•
for (int i = 0; i < n; i++)
•
for (int j = 0; j < n; j++)
•
scanf("%d", &matrix[i][j]);
•
if (is_upper_triangular(n, matrix))
•
printf("The given matrix is an upper triangular matrix.\n");
•
else
•
printf("The given matrix is not an upper triangular matrix.\n");
•
•
return 0;
}
.
4
•
•
Check if a given matrix is symmetric.
#include <stdio.h>
int is_symmetric(int n, int matrix[n][n]) {
•
for (int i = 0; i < n; i++)
•
for (int j = 0; j < n; j++)
•
if (matrix[i][j] != matrix[j][i])
•
return 0;
•
•
•
return 1;
}
int main() {
•
int n;
•
printf("Enter the size of the matrix: ");
•
scanf("%d", &n);
•
int matrix[n][n];
•
printf("Enter the elements of the matrix:\n");
•
for (int i = 0; i < n; i++)
•
for (int j = 0; j < n; j++)
•
scanf("%d", &matrix[i][j]);
•
if (is_symmetric(n, matrix))
•
printf("The given matrix is symmetric.\n");
•
else
•
printf("The given matrix is not symmetric.\n");
•
•
return 0;
}
.
5
Find number and sum of digits in a given number using recursion function.(If 1234 is given no of digits:4, sum of digits:10)
• #include <stdio.h>
•
int count_digits(int num) {
•
if (num == 0)
•
return 0;
•
return 1 + count_digits(num / 10);
• }
•
int sum_digits(int num) {
•
if (num == 0)
•
return 0;
•
return num % 10 + sum_digits(num / 10);
• }
•
int main() {
•
int num;
•
printf("Enter a number: ");
•
scanf("%d", &num);
•
printf("Number of digits: %d\n", count_digits(num));
•
printf("Sum of digits: %d\n", sum_digits(num));
•
return 0;
• }
•
•
•
6.Reverse an array using function.
#include <stdio.h>
void reverse_array(int n, int *arr) {
•
for (int i = 0; i < n / 2; i++) {
•
int temp = arr[i];
•
arr[i] = arr[n - i - 1];
•
arr[n - i - 1] = temp;
•
•
•
}
}
int main() {
•
int n;
•
printf("Enter the size of the array: ");
•
scanf("%d", &n);
•
int arr[n];
•
printf("Enter the elements of the array:\n");
•
for (int i = 0; i < n; i++)
•
scanf("%d", &arr[i]);
•
reverse_array(n, arr);
•
printf("Reversed array:\n");
•
for (int i = 0; i < n; i++)
•
printf("%d ", arr[i]);
•
printf("\n");
•
return 0;}
Download