Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program that will read a positive integer from the user and will print out the 3 N + 1 sequence starting from

Write a java program that will read a positive integer from the user and will print out the 3N+1 sequence
starting from that integer. The program should also count and print out the number of terms in the
sequence.
Given a positive integer, N, define the '3N+1' sequence starting from N as follows: If N is an even
number, then divide N by two; but if N is odd, then multiply N by 3 and add 1. Continue to generate
numbers in this way until N becomes equal to 1.
You must validate the starting number must be positive.
C:\WINDOWS ??? system32\cmd.exe
3,10,5,16,8,4,2,1
There were 7 terms in the sequence.Press any key to continue ...
Hint:
For example, starting from N=3, which is odd, we multiply by 3 and add 1, giving N=3**3+1=10. Then,
since N is even, we divide by 2, giving N=102=5. We continue in this way, stopping when we reach 1,
giving the complete sequence: 3,10,5,16,8,4,2,1.
Ask user to input a positive number;
Let N be the user's response;
while N is not positive:
Print an error message;
Read another value for N;
Let counter =0;
while N is not 1:
if N is even:
Compute N=N2;
else
Compute N=3**N+1;
output N;
Add 1 to counter;
output the counter;
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions