Program to Find nth 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.

Example:

               Given Number=135
               135⇒1¹+3²+5³=1+9+125=135
               so, 135 is Disarium Number.
                 
                Given Number=120
               120⇒1¹+2²+0³=1+4+0=5
               so, 120 is not a Disarium Number.
                                          
Problem statement:-  Program to Find nth Disarium Number.

Data requirement:-

   Input Data:- rangenumber

   Output Data:- rangenumber, letest

   Additional Data:- cnum, num1, c1, sum, num2, rem

Program in C

Here is the source code of the C Program to Find nth Disarium Number.

Code:

//Nth Disarium Number
#include <stdio.h>
#include<math.h>
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  printf ("Enter the Nth value:");
  scanf ("%d", &rangenumber);
  while (c != rangenumber)
    {
    int num1=num,c1=0;
    int num2=num;
    while(num1!=0)
    {
        num1/=10;
        c1++;
    }
    num1=num;
    int sum=0;
    while(num1!=0)
    {
        int rem=num1%10;
        sum+=pow(rem,c1);
        num1/=10;
        c1--;
    }
   if(sum==num2)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
  printf ("%dth Disarium number is %d", rangenumber, letest);
  return 0;
}

Input/Output:
Enter the Nth value:10                                                                                                               
10th Disarium number is 89  

Program in C++

Here is the source code of the C++ Program to Find nth Disarium Number.

Code:

#include <iostream>
#include<cmath>
using namespace std;
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  cout<<"Enter the Nth value:";
  cin>>rangenumber;
  while (c != rangenumber)
    {
   int num1=num,c1=0;
    int num2=num;
    while(num1!=0)
    {
        num1/=10;
        c1++;
    }
    num1=num;
    int sum=0;
    while(num1!=0)
    {
        int rem=num1%10;
        sum+=pow(rem,c1);
        num1/=10;
        c1--;
    }
   if(sum==num2)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
  cout<<rangenumber<<"th Disarium number is "<<letest;
  return 0;
}

Input/Output:
Enter the Nth value:7
7th Disarium number is 7

Program in Java
  
Here is the source code of the Java Program to Find nth Disarium Number.

Code:

import java.util.Scanner;
public class NthDisariumNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int  rangenumber, num = 1, c = 0, letest = 0;
  System.out.println("Enter Nth number:");
  rangenumber=cs.nextInt();
           
  while (c != rangenumber)
    {
  int num1=num,c1=0;
    int num2=num;
    while(num1!=0)
    {
        num1/=10;
        c1++;
    }
    num1=num;
    int sum=0;
    while(num1!=0)
    {
        int rem=num1%10;
        sum+=Math.pow(rem,c1);
        num1/=10;
        c1--;
    }
   if(sum==num2)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
System.out.println(rangenumber+"th Disarium number is "+letest);
cs.close();
}
}

Input/Output:
Enter Nth number:
15
15th Disarium number is 1306

Program in Python
  
Here is the source code of the Program to Find nth Disarium Number.

Code:

import math
rangenumber=int(input("Enter a Nth Number:"))
c = 0
letest = 0
num = 1
while c != rangenumber:
    num1=num
    c1 = 0
    num2 = num
    while num1 != 0:
        num1 //= 10
        c1 += 1
    num1 = num
    sum = 0
    while num1 != 0:
        rem = num1 % 10
        sum += math.pow(rem, c1)
        num1 //= 10
        c1 -= 1
    if sum == num2:
            c+=1
            letest = num

    num = num + 1
print(rangenumber,"th Sunny number is ",letest)

Input/Output:
Enter a Nth Number:17
17th Sunny number is  2427

More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments