main
- STEP 1: START
- STEP 2: SET result =0
- STEP 3: PRINT statement "Disarium numbers between 1 and 100 are"
- STEP 4: REPEAT STEP 5 &6 UNTIL (i<=100)
- STEP 5: result = sumOfDigits(i)
- STEP 6: if(result==i) then PRINT i
- STEP 7: RETURN 0
- STEP 8: END
sumOfDigits(num)
- STEP 1: START
- STEP 2: SET sum =0, rem =0
- STEP 3: len = calculateLength(num)
- STEP 4: REPEAT STEP 5 to 8 UNTIL (num>0)
- STEP 5: rem = num%10
- STEP 6: sum = sum + (rem) len
- STEP 7: num =num/10
- STEP 8: len--
- STEP 9: RETURN sum
- STEP 10: END
calculateLength(int n)
- STEP 1: START
- STEP 2: SET length =0
- STEP 3: REPEAT STEP 4 & 5 UNTIL (n!=0)
- STEP 4: length = length + 1
- STEP 5: n =n/10
- STEP 6: RETURN length.
- STEP 7: END
PROGRAM:
#include <stdio.h>
#include <math.h>
//calculateLength() will count the digits present in a number
int calculateLength(int n){
int length = 0;
while(n != 0){
length = length + 1;
n = n/10;
}
return length;
}
//sumOfDigits() will calculates the sum of digits powered with their respective position
int sumOfDigits(int num){
int sum = 0, rem = 0;
int len = calculateLength(num);
while(num > 0){
rem = num%10;
sum = sum + pow(rem,len);
num = num/10;
len--;
}
return sum;
}
int main()
{
int result = 0;
//Displays all disarium numbers between 1 and 100
printf("Disarium numbers between 1 and 100 are\n");
for(int i = 1; i <= 100; i++){
result = sumOfDigits(i);
if(result == i)
printf("%d ", i);
}
return 0;
}
Output:
Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89
Happy Number
A digit is considered joyful if it produces 1 when substituted by the total of squares of its digits repeatedly. Should this procedure lead to an infinite loop of numbers that include 4, then the digit will be classified as a sorrowful digit.
Let's understand by an example:
Number = 32
32+ 22= 13
12+ 32= 10
12+ 02= 1
In this instance, we decompose 32 to calculate the sum of the squares of its individual digits, resulting in another number (13). Subsequently, we substitute 32 with 13 to iterate this process until reaching the ultimate result of 1. Consequently, we identify 32 as a happy number.
If a number results in 1 after following the cycle mentioned above, it is considered a Happy number. Otherwise, it will be classified as an unhappy number, resulting in the numbers 4, 16, 37, 58, 89, 145, 42, 20, and so on.
Some Happy numbers are 7, 28, 100, 320 etc.