Saturday, 21 November 2020

Write a program in Java to Create a Single Dimensional Array of n integers. Print only the Armstrong numbers from the Array.

 /* Armstrong Number: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

*/

import java.util.*;

class ArmStrong

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int n,n1,d,cube,sum,i;

        System.out.println("Enter the size of the array");

        n=in.nextInt();

        int a[]=new int[n];

        System.out.println("Enter "+n+" Numbers");

        for(i=0;i<n;i++)

        a[i]=in.nextInt();

        for(i=0;i<n;i++)

        {

         n1=a[i];

         sum=0;

         while(a[i]>0)

        {

          d=a[i]%10;

          cube=d*d*d;

          sum=sum+cube;

          a[i]=a[i]/10;

        }

        if(sum==n1)

        System.out.println(n1+" is an Armstrong Number");

        else

        System.out.println(n1+" is not an Armstrong Number");

        }

    }

}


No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...