-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuDriven.java
More file actions
73 lines (70 loc) · 2.83 KB
/
MenuDriven.java
File metadata and controls
73 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
public class MenuDriven {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Boolean repeat= true;
while(repeat){
System.out.println("What operations would you like to be performed?");
System.out.println("1. Positive number");
System.out.println("2. Armstrong number");
System.out.println("3. Palindrome number");
System.out.println("4. Addition of number");
System.out.println("5. Digit count");
System.out.println("6. Exit");
int ch= input.nextInt();
if (ch ==1){
System.out.println("Enter a number to check if its positive or not: ");
int p_i= input.nextInt();
if (p_i<0)
System.out.println("The number is negative.");
else
System.out.println("The number is positive.");
System.out.println();
}
else if (ch==2){
System.out.print("Enter a number to see if its armstrong or not: ");
int n = input.nextInt();
int temp=n, result=0, digit=0, temp2=n;
// counting number of digit
while (temp>0) {
temp = temp / 10;
digit++;
}
while (temp2>0){
result+=Math.pow((temp2%10), digit); //1221 = 1^4 + 2^4 + 2^4 + 1^4
temp2/=10;
}
if (result==n)
System.out.println(n+" Is An Armstrong Number.");
else
System.out.println(n+" Is Not An Armstrong Number");
System.out.println();
}
else if (ch==3){
System.out.print("Enter a number to see if its palindrome or not: ");
int n = input.nextInt();
int temp=n, sum=0,digit;
while(temp>0){
digit=temp%10;
sum=(sum*10)+digit;
temp/=10;
}
if (sum==n)
System.out.println(String.format("%d Is A Palindrome Number.",n));
else
System.out.println(String.format("%d Is Not A Palindrome Number.",n));
System.out.println();
}
else if (ch==5){
System.out.println("Enter a number to count its digits: ");
String digits =input.next();
int num = Integer.parseInt(digits);
System.out.println(String.format("%d Int has %d digits.",num,digits.length()));
System.out.println();
}
else if (ch==6)
repeat=false;
input.close();
}
}
}