Monday, 24 January 2022

A) Write a ‘java’ program to check whether given number is Armstrong or not. (Use static keyword)

 

 Slip 3 Q 1

SAVE CODE AS         "Armstrong.java"


import java.util.Scanner;

public class Slip3Q1 {
static boolean is_armstrong(int n){
int temp,last,digits=0,sum=0;
temp = n;
while (temp>0){
temp=temp/10;
digits++;
}
temp = n;
while (temp>0){
last = temp%10;
sum+=Math.pow(last,digits);
temp=temp/10;
}
return n == sum;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check armstrong : ");
int number = scanner.nextInt();
scanner.close();
if (is_armstrong(number)){
System.out.println(number+" is a armstrong number.");
}
else {
System.out.println(number+" is not a armstrong number.");
}
}
}


OUTPUT :

Write a ‘java’ program to check whether given number is Armstrong or not. (Use static keyword)






No comments:

Post a Comment

Write a Java program to display given extension files from a specific directory on server machine.

 DOWNLOAD     SLIP14Q2 /** * STEPS TO RUN CODE * Step 01 compile the code * Step 02 run the code * Step 03 give a file directory locatio...