Showing posts with label Disarium Number. Show all posts
Showing posts with label Disarium Number. Show all posts

Disarium Number

 //Write a program to accept a number and check it is Disarium number or not.

// A number is said to be Disarium number when the sum of its digit raised to the power of their respective

// positions is equal to the number itself. ex=175 1^1 + 7^2 + 5^3 = 1+49+125 = 175

import java.util.*;

class disarium

{//class disarium opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no//175

        int r=0,s=0,k=n,c=0,check=n;//variable r to extract the digits,variable k and check to store the no

        while(n>0)//175>0 t//17>0 t//1>0 t//0>0 f

        {

         n=n/10; //175/10=17//17/10=1//1/10=0

         c=c+1;//variable c to store the no of digits//c+1=1//1+1=2//2+1=3

        }

        while(k>0)//175>0 t//17>0 t//1>0 t//0>0 false

        {

            r=k%10;//175%10=5//17%10=7//1%10=1

            s=s+(int)Math.pow(r,c);//variable s to store the sum//0+125=125//125+49=174//174+1=175

            c--;//3--=2//2--=1//1--=0

            k=k/10;//175/10=17//17/10=1//1/10=0

        }

        if(s==check)//175==175 true

        System.out.println("It is a disarium no");

        else

        System.out.println("It is not a disarium no");

    }//main method closes

}//class closes


Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...