Arnab_Manna

armstrong_number_any_range.java

Dec 7th, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.*;
  2. class armstrong{
  3.     protected String num;
  4.         armstrong(String n)//constructor to take input
  5.         {
  6.           num=n;
  7.         }
  8.         int nDig()//find the number of digits
  9.         {
  10.                int l=num.length();
  11.                return l;
  12.         }
  13.        void outpt()
  14.         {
  15.             double sum=0;
  16.             int n=nDig();
  17.             int stor[]=new int[n];
  18.             for(int i=0;i<n;i++)
  19.             {
  20.                 char c=num.charAt(i);
  21.                 stor[i]=(int)c-48;
  22.             }
  23.             for(int i=0;i<n;i++)
  24.             {
  25.                //System.out.println(stor[i]+" ");
  26.                sum=sum+Math.pow(stor[i],n);
  27.             }
  28.             int sm=(int)sum;
  29.            String chk=Integer.toString(sm);
  30.             if(num.equals(chk))
  31.             {
  32.                 System.out.println(chk+" Amstrong number");
  33.             }
  34.             else
  35.             {
  36.                 System.out.println(chk+" not Arrmstrong");
  37.             }
  38.         }
  39. }            
  40. class O1
  41. {
  42.     public static void main(String args[])
  43.     {
  44.         Scanner in= new Scanner(System.in);
  45.         System.out.println("enter a number");
  46.         String s=in.nextLine();
  47.         armstrong a=new armstrong(s);
  48.         a.outpt();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment