-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignment 2 - Question6.java
More file actions
45 lines (43 loc) · 1.07 KB
/
Copy pathAssignment 2 - Question6.java
File metadata and controls
45 lines (43 loc) · 1.07 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
package com.company;
import java.util.*;
import java.lang.Math;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number: ");
long number = scanner.nextLong();
int count = 0;
if (number <= 1)
{
System.out.println("Please enter a number larger than 1.");
}
while (number != 1)
{
if ((number % 2) == 0)
{
long evenNum = number;
long halfNum = (evenNum / 2);
System.out.println(evenNum + " is even, so I take half: " + halfNum);
number = halfNum;
}
else
{
long oddNum = number;
long tripleOne = ((3 * number) + 1);
if(tripleOne>Integer.MAX_VALUE)
{
System.out.println("Integer Overflow Detected!!!");
return;
}
System.out.println(oddNum + " is odd, so I make 3n+1: " + tripleOne);
number = tripleOne;
}
count++;
}
System.out.println("You've reached the number 1, I'm stopping.");
System.out.println("The process took " + count + " steps to reach 1.");
}
}