Skip to content

Commit 2607b37

Browse files
committed
new
1 parent c3da01d commit 2607b37

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,80 @@
11
package com.sbiswas001.twelveproject;
22

3+
import java.util.Scanner;
4+
5+
/**
6+
* Checks if a number is armstrong or not.
7+
* A number is armstrong if the sum of each
8+
* digit to the power number of digits is
9+
* equal to the number itself.
10+
* @author Sayan Biswas
11+
* @version 25.04.2022
12+
*/
313
public class ArmstrongNumber {
14+
/**
15+
* Stores the number
16+
*/
17+
private int number;
18+
19+
/**
20+
* Initializes instance variables
21+
*/
22+
private ArmstrongNumber() {
23+
number = 0;
24+
}
25+
26+
/**
27+
* Inputs a number from user
28+
*/
29+
private void input() {
30+
Scanner sc = new Scanner(System.in);
31+
System.out.print("Enter number: ");
32+
number = Integer.parseInt(sc.nextLine());
33+
}
34+
35+
/**
36+
* Returns number of digits of a number
37+
* @param n Number to be checked
38+
* @return Number of digits
39+
*/
40+
private int numberOfDigits(int n) {
41+
return (int)Math.floor(Math.log10(n))+1;
42+
}
43+
44+
/**
45+
* Checks if a number is armstrong or not.
46+
* A number is armstrong if the sum of each
47+
* digit to the power number of digits is
48+
* equal to the number itself.
49+
* @param a Number to be checked
50+
* @return true or false
51+
*/
52+
private boolean armstrongCheck(int a) {
53+
int sum = 0;
54+
while(a > 0) {
55+
sum += (int)Math.pow(a % 10, numberOfDigits(number));
56+
a /= 10;
57+
}
58+
return sum == number;
59+
}
60+
61+
/**
62+
* Displays the result if number is armstrong or not
63+
*/
64+
private void display() {
65+
66+
System.out.println(armstrongCheck(number) ?
67+
"Number is armstrong." :
68+
"Number is not armstrong.");
69+
}
70+
71+
/**
72+
* Calls other methods
73+
* @param args Arguments passed to main method
74+
*/
75+
public static void main(String[] args) {
76+
ArmstrongNumber ob = new ArmstrongNumber();
77+
ob.input();
78+
ob.display();
79+
}
480
}

src/com/sbiswas001/twelveproject/UniqueNumber.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ private UniqueNumber() {
2828
private void input() {
2929
//Variable to store the number
3030
String s;
31-
3231
Scanner sc = new Scanner(System.in);
3332
System.out.print("Enter number: ");
3433
if(!sc.hasNextInt()){

0 commit comments

Comments
 (0)