Write a program that reads in a number from the user

Assignment Help Programming Languages
Reference no: EM131251209

Douglas Hofstadter's Pulitzer-prize-winning book Gödel, Escher, Bach contains many interesting mathematical puzzles. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements in Java. The problem can be expressed as follows: Pick some positive integer and call it n. If n is even, divide it by two. If n is odd, multiply it by three and add one. Continue this process until n is equal to 1. Hofstadter illustrates this process with the following example, starting with the number n = 15:

15 is odd, so I make 3n+1: 46
46 is even, so I take half: 23
23 is odd, so I make 3n+1: 70
70 is even, so I take half: 35
35 is odd, so I make 3n+1: 106
106 is even, so I take half: 53
53 is odd, so I make 3n+1: 160
160 is even, so I take half: 80
80 is even, so I take half: 40
40 is even, so I take half: 20
20 is even, so I take half: 10
10 is even, so I take half: 5
5 is odd, so I make 3n+1: 16
16 is even, so I take half: 8
8 is even, so I take half: 4
4 is even, so I take half: 2
2 is even, so I take half: 1

As you can see from this example, the numbers go up and down, but eventually-at least for all numbers that have ever been tried-come down to end in 1. In some respects, this process is reminiscent of the formation of hailstones, which get carried upward by the winds over and over again before they finally descend to the ground. Because of this analogy, this sequence of numbers is usually called the Hailstone sequence, although it goes by many other names as well.

Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1.

Reference no: EM131251209

Questions Cloud

New customers they sign up for new magazine subscriptions : Outbound sales representatives receive a $20 commission on all new customers they sign up for new magazine subscriptions. Each outbound sales representative works 40 hours. Because these employees are paid solely on commission, the employer must ensu..
Write a program to compute the total bonus amount earned : An employee has committed to save $2000 per month. Write a program to compute the total bonus amount earned by these two employees in the year.
Analyze the prioritizing process : Analyze the prioritizing process at D. D. Williamson. Suggest two (2) recommendations to improve the prioritizing process. Create a scenario where the implemented process at D. D. Williamson would not work.
Deliver package to customer before traveling home : Nadine is an administrative assistant for her employer. At the end of her shift one day, her employer requires her to deliver a package to a customer before traveling home. She spends two hours driving to the customer site, and then three hours drivi..
Write a program that reads in a number from the user : Write a program that reads in a number from the user and then displays the Hailstone sequence for that number, followed by a line showing the number of steps taken to reach 1.
Create the code for a login system : SGA1789 Build Dynamic Websites Workbook Assignment. Using PHP session management features create the code for a login system. This will be used for security and access control of the database component you developed in Assessment Task. Ensure there..
Analyze the prioritizing process : Analyze the prioritizing process at D. D. Williamson. Suggest two (2) recommendations to improve the prioritizing process. Create a scenario where the implemented process at D. D. Williamson would not work.
Sole proprietor who performs excavating services : In July of 2009, Mr. Mann, a sole proprietor who performs excavating services, purchased and put to use for business a piece of heavy equipment for $36,500. This piece of equipment was depreciated using the MACRS 150% DB method of depreciation over t..
Corporate social responsibility : 1. In terms of corporate social responsibility, discuss three ways that a company can focus on the needs of its stakeholders without neglecting its shareholder obligations.

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a program that converts english units to metric

Write a unit conversion program that converts English units to Metric using the conversion table given above. Ask the user from which unit they want to convert ( oz, lb, in, ft, mi) and which unit they want to convert to (g, kg, cm, and km).

  Create the logic for a program that accepts input values

Create the logic for a program that accepts input values for the projected cost of a vacation and the number of month until vacation. Pass both values to a method that displays the amount you must save per month to achieve your goal.

  Program to calculate the electricity bill

Write a program to calculate the electricity bill. The rates of electricity per unit are as follows: If the units consumed are equal or less than 100, then the cost is Rs 8/- per unit.

  Find whether each shape is two dimensional shape

Find whether each shape is TwoDimensionalShape or a ThreeDimensionalShape. If it is a TwoDimensionalShape, display its area. If it is a ThreeDimensionalShape, display its area and volume.

  Develop vision system for building high safe mobile robot

You are tasked to develop a vision system for building a high safe mobile robot. In particular, your system should give a warning alarm when there are a danger of colliding (at certain distance) with a pedestrian.

  Simmac language programs

Write three programs in the machine language of the SIMMAC that will total the numbers stored in 20 different locations and place the result in a single location.

  Write a program to define constant

Display each of these constants in decimal, in hexadecimal, and as a character usingcout.

  A fixed quantum of information packaged together

A fixed quantum of information packaged together with an IP address and other data for sending information over the internet

  Write modular program to enter integers for sorting

Write a modular program that performs the following functions: Allows the user to enter 10 integers for sorting, Allows the user to select one of the two types of sorting techniques.

  Briefly describe the properties of type system

Briefly describe the properties of type system of the corresponding programming language. Pay attention to each of the statements since there are a few obvious statements you can make

  Create a template that changes the value of 2 variables

Use the template in a program where you change the value of 2 variables of type int and 2 variables of type long.

  Design and implement a java program

Design and implement a Java program which defines an array of size SIZE, randomly populated with Integer or int values in the range 1 .. MAXRNG and sorts the array in increasing order of its values using QuickSortOpt1 and then by QuickSortOpt2. Consi..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd