File tree 1 file changed +60
-0
lines changed
1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ //C program to check if a number is magic number or not
2
+ //Magic Number:A number is said to be a magic number
3
+ //if the product of the sum and the reverse number of the sum is the given number then the number is the magic number.
4
+ //Ex:1729 is a magic number
5
+ //Code:
6
+
7
+ #include <stdio.h>
8
+ /* sum of digits of a number */
9
+ int sumOfDigits (int num ) {
10
+ int sum = 0 ;
11
+ while (num > 0 ) {
12
+ sum = sum + (num % 10 );
13
+ num = num / 10 ;
14
+ }
15
+ return sum ;
16
+ }
17
+
18
+ /* returns reverse of a given number */
19
+ int reverse (int num ) {
20
+ int rev = 0 ;
21
+ while (num > 0 ) {
22
+ rev = (rev * 10 ) + (num % 10 );
23
+ num = num / 10 ;
24
+ }
25
+ return rev ;
26
+ }
27
+
28
+ int main () {
29
+ int num , sum , rev ;
30
+
31
+ printf ("Enter the value for a number:" );
32
+ scanf ("%d" , & num );
33
+
34
+ /* find sum of digits by calling function */
35
+ sum = sumOfDigits (num );
36
+
37
+
38
+ //if the value is single digit, then
39
+ //the value and its reverse are same
40
+ if (sum < 10 ) {
41
+ if ((sum * sum ) == num ) {
42
+ printf ("%d is a magic number\n" , num );
43
+ } else {
44
+ printf ("%d is not a magic number\n" , num );
45
+ }
46
+ return 0 ;
47
+ }
48
+
49
+ /* reverse of the given number */
50
+ rev = reverse (sum );//calling reverse function
51
+
52
+ /* printing the outputs */
53
+ if ((sum * rev ) == num ) {
54
+ printf ("%d is a magic number\n" , num );
55
+ } else {
56
+ printf ("%d is not a magic number\n" , num );
57
+ }
58
+
59
+ return 0 ;
60
+ }
You can’t perform that action at this time.
0 commit comments