summary refs log tree commit diff stats
path: root/assignments/12-armstrong-range.c
diff options
context:
space:
mode:
authorsmlckz <smlckz@college>2021-12-22 14:56:13 +0530
committersmlckz <smlckz@college>2021-12-22 14:56:13 +0530
commitb73983c3717642ca10e7cfe93d97609adc377da9 (patch)
treea6e9fe4c27e3caa215f8aefa9265fb52f6de4375 /assignments/12-armstrong-range.c
downloadcollege-b73983c3717642ca10e7cfe93d97609adc377da9.tar.gz
backup
Diffstat (limited to 'assignments/12-armstrong-range.c')
-rw-r--r--assignments/12-armstrong-range.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/assignments/12-armstrong-range.c b/assignments/12-armstrong-range.c
new file mode 100644
index 0000000..cc57fb4
--- /dev/null
+++ b/assignments/12-armstrong-range.c
@@ -0,0 +1,62 @@
+#include <math.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+bool is_armstrong(int n)
+{
+	int m = n, r, s = 0;
+	float c = ceil(log10(n));
+	while (m > 0) {
+		r = m % 10;
+		m /= 10;
+		s += pow(r, c);
+	}
+	return n == s;
+}
+
+int main(void)
+{
+	int i, l, h;
+	printf("Print Armstrong Numbers in a given range\n\n");
+	printf("Enter the range: ");
+	scanf("%d%d", &l, &h);
+	if (h < l) {
+		printf("Invalid range\n");
+		return 0;
+	}
+	printf("Armstrong numbers between %d and %d are: ", l, h);
+	for (i = l; i <= h; i++) {
+		if (is_armstrong(i)) printf("%d ", i);
+	}
+	printf("\n");
+	return 0;
+}
+
+/*
+Output:
+Set 1:
+Print Armstrong Numbers in a given range
+
+Enter the range: 100 1
+Invalid range
+
+Set 2:
+Print Armstrong Numbers in a given range
+
+Enter the range: 1 100
+Armstrong numbers between 1 and 100 are: 1 2 3 4 5 6 7 8 9 
+
+Set 3:
+Print Armstrong Numbers in a given range
+
+Enter the range: 100 10000
+Armstrong numbers between 100 and 10000 are: 153 370 371 407 1634 8208 9474 
+
+Set 4:
+Print Armstrong Numbers in a given range
+
+Enter the range: 10000 1000000
+Armstrong numbers between 10000 and 1000000 are: 54748 92727 93084 548834 
+
+*/
+