-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayfunctions
96 lines (79 loc) · 2.24 KB
/
arrayfunctions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Q11 Write a c program to store N multi digit elements in 1 D array then count and print
a) total palindrome
b) total prime no.
c) total Armstrong no.
d) total elements having sum of digits less than 10
Ex. 123,345,567,121,222,153
code
#include <stdio.h>
#include <math.h>
// Function to check if a number is palindrome
int isPalindrome(int n) {
int original = n, reversed = 0;
while (n > 0) {
reversed = reversed * 10 + (n % 10);
n /= 10;
}
return original == reversed;
}
// Function to check if a number is prime
int isPrime(int n) {
if (n <= 1)
return 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return 0;
}
return 1;
}
// Function to check if a number is Armstrong
int isArmstrong(int n) {
int original = n, sum = 0, digits = 0, remainder;
// Count number of digits
while (n > 0) {
digits++;
n /= 10;
}
n = original;
// Calculate sum of digits raised to the power of number of digits
while (n > 0) {
remainder = n % 10;
sum += pow(remainder, digits);
n /= 10;
}
return original == sum;
}
// Function to get the sum of digits of a number
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int N, countPalindrome = 0, countPrime = 0, countArmstrong = 0, countSumLessThan10 = 0;
printf("Enter the number of elements (N): ");
scanf("%d", &N);
int arr[N];
// Input array elements
printf("Enter the %d multi-digit elements:\n", N);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
// Check each element
for (int i = 0; i < N; i++) {
if (isPalindrome(arr[i])) countPalindrome++;
if (isPrime(arr[i])) countPrime++;
if (isArmstrong(arr[i])) countArmstrong++;
if (sumOfDigits(arr[i]) < 10) countSumLessThan10++;
}
// Print the results
printf("\nTotal Palindrome numbers: %d", countPalindrome);
printf("\nTotal Prime numbers: %d", countPrime);
printf("\nTotal Armstrong numbers: %d", countArmstrong);
printf("\nTotal numbers with sum of digits less than 10: %d", countSumLessThan10);
return 0;
}
*/