-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOne.java
More file actions
59 lines (49 loc) · 1.59 KB
/
Copy pathOne.java
File metadata and controls
59 lines (49 loc) · 1.59 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
/* This code is my own work. I did not get any help from any online source
such as chegg.com; from a classmate, or any other person other than the instructor
or TA for this course. I understand that getting outside help from this course
other than from the instructor or TA will result in a grade of 0 in this
assignment and other disciplinary actions for academic dishonesty.
# Name : Cristian Z
# Class: CSET 1200
# Instructor: Dr. Jared Oluoch
# Programming Assignment: 13
# Problem: 2
# Date: 12/2/21
# Summary: dividing till its less than one
# Problem 1
Use recursion to write a Java program that takes a
positive number as an input and keeps dividing that number
by 2 until the result is less than 1, at which time
output the result. (7 points)
*
* (8 Points.)
https://youtu.be/7KOfA5G3K30
*/
import java.util.Scanner;
public class One{
//method for division
public static double rec(double num)
{
//check if num is less than 1
if(num < 1.0) //base case
return num; //last num
//System.out.println(num);
return rec(num/2);
}
//recursion - calling on one's own method till base case is met
public static void main(String[] args)
{
//create scanner
Scanner input = new Scanner(System.in);
double number = 0;
System.out.println("Give us a number and we will divide it till its less than one.");
number = input.nextDouble();
if(number <0)
{
System.out.println("Please enter a positive number.");
main(args);
}
else
System.out.println("The remaining number is :" + rec(number));
}
}