You're online now.

Hurray! you are online now.

Program to check whether number is Disarium Number

A Number is a disarium number if the sum of the digits powered with their respective positions is equal to the number itself.

For Example:
89 is a Disarium number.
89: → 81 + 92 = 8 + 81 = 89

Write a c program to check whether a given number Disarium number

#include <stdio.h>
    #include<math.h>
    
    int main()
    {
        int n, temp;
        int sum = 0, count = 0;
        printf("Enter a number : ");
        scanf("%d", &n);
        // First Count the digits of the number
        temp = n;
        while(temp!=0){
            temp = temp/10;
            count = count + 1;
        }
        temp = n;
        while(count!=0){
            int last_digit = temp%10;
            temp = temp/10;
            sum = sum + pow(last_digit,count);
            count = count - 1;
        }
        if(sum == n){
            printf("%d is a Disarium Number", n);
        }else{
            printf("%d is not a Disarium Number", n);
        }
        return 0;
    }
    

Write a c++ program to check whether a given number Disarium number

// 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 50 + 125 = 175
    
    #include <iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        int n, temp;
        int sum = 0, count = 0;
        cout<<"Enter a number : ";
        cin>>n;
        // First Count the digits of the number
        temp = n;
        while(temp!=0){
            temp = temp/10;
            count = count + 1;
        }
        temp = n;
        while(count!=0){
            int last_digit = temp%10;
            temp = temp/10;
            sum = sum + pow(last_digit,count);
            count = count - 1;
        }
        if(sum == n){
            cout<<n<<" is a Disarium Number";
        }else{
            cout<<n<<" is not a Disarium Number";
        }
        return 0;
    

Algorithm to check the number is Disarium number

  • Calculate the length count digits of given number
    a. Set temp = n
    b. Use a while loop to check whether the number is not equal to 0.
    c. Divide the number by 10 and increment the count by 1.
  • SET temp = n again
  • Calculate the number of the given number
    a. Use a while loop to check whether the count is not equal to 0.
    b. Define and assign last_digit.
    c. Calculate the last digit.
    d. Remove the last digit by temp = temp / 10.
    e. Calculate the sum = sum + pow(last_digit, count).
    f. Decrement the count by 1.
  • Check the given number and sum
    a. If the given number and sum is equal then PRINT “Given number is Disarium” otherwise go to step 2.
    b. PRINT “Given number is not Disarium”
    • EXIT
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...