What's new

Help Java-Ulam Sequence

BototoyTheGreat24

Honorary Poster
Joined
Oct 1, 2018
Posts
533
Reaction
109
Points
185
Patulong naman po kung paano gawin yung ulam sequence sa java using while or do-while loop.

If N is 1, stop.
If N is even, replace it with N/2.
If N is odd, replace it with 3 * N + 1.

example:

user input: 6

output: 6, 3, 10, 5, 16, 8, 4, 2, 1

Sana may makatulong. Salamat!
 
pa try nalang ts,
[CODE lang="java" title="Test" highlight="9,10,11,13"]import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input: ");
int N = sc.nextInt();
System.out.print("Output: ");
while(N != 1){
if(N % 2 == 0)
N /= 2;
else
N = 3 * N +1;
System.out.printf("%d ",N);
}
}
}[/CODE]
 

Similar threads

Back
Top