-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIronical.java
More file actions
53 lines (45 loc) · 1.04 KB
/
Ironical.java
File metadata and controls
53 lines (45 loc) · 1.04 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
package classLabs;
import java.util.Scanner;
public class Ironical
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter the value of X: ");
double userInput = input.nextInt();
System.out.println("The values in the collatz sequence are: ");
computeProblem(userInput);
}
public static boolean isEven(double x)
{
if(x%2==0)
{
return true;
}
else
{
return false;
}
}
public static void computeProblem(double x)
{
double y=x;
double counter=1;
while(x!=1)
{
System.out.print(x+" , ");
if(isEven(x))
{
x/=2;
}
else
{
x=3*x+1;
}
counter++;
}
System.out.print(x); System.out.println();
System.out.println("Number of times: "+counter);
System.out.println("Occurence Percentage: "+(counter/y*100)+" %");
}
}