Question 1 :

Write a class to check whether a given number is Armstrong or not using a function name int checkArmstrong(int num). Function should return a value 1 if number is Armstrong otherwise return 0 .

Explanation :

An armstrong number is a number whose sum of the cubes of its digits is equal to the number itself .
for example , 371 ----> 3*3*3 + 7*7*7 + 1*1*1 = 371
hence , 371 is an armstrong number .


import java.util.* ;
public class armstrong 
{
        
    public void check(int num)
    {
        int digit = 0 ;
        int sum = 0 ;
        int temp = num ;
        int temp2 = num ;
        int count  = 0 ;

        while(temp>0)
        {
            digit = temp%10 ;
            temp = temp/10 ;
            count++ ;
    
        } 
    
        for(int i = 1 ;i<=count;i++)
        {
           digit = temp2%10 ; 
           temp2 = temp2/10 ;
           sum = (int) (sum + 
           Math.pow(digit,count));
        }
             
           
        if(sum == num)
        System.out.println(num+" is a Armstrong number .") ;
    
        else
        System.out.println(num+" is not a Armstrong number .") ; 
    }
                  
    public static void main(String[] args)
    {
           Scanner sc = new Scanner(System.in) ;  
           System.out.println("Enter a number") ;     
           int num = sc.nextInt() ;
               
           armstrong run = new armstrong() ;
           run.check(num);
    }
}         

           
            
Codely Prompt ----->java armstrong.java
Enter a number
371
371 is a Armstrong number
            
        

Contribution by :-

Samar Image

Samar Khan