-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
63 lines (49 loc) · 1.75 KB
/
main.c
File metadata and controls
63 lines (49 loc) · 1.75 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
60
61
62
63
#include <stdio.h>
// Function to compute the Collatz sequence and find the highest number
void collatz(long long n) {
long long highest_number = n; // Initialize with the starting number
// Prints the starting number
printf("Collatz sequence for %lld:\n", n);
// The loop repeats until the number reaches 1
while (n != 1) {
// Prints the current number
printf("%lld -> ", n);
// Checks if the number is even or odd
if (n % 2 == 0) {
// Even number - divide by two
n = n / 2;
} else {
// Odd number - multiply by three and add one
n = 3 * n + 1;
}
// Checks if the new number is the highest so far
if (n > highest_number) {
highest_number = n;
}
}
// Prints the final number (1)
printf("1\n");
// Prints the highest number reached
printf("\nHighest number reached: %lld\n", highest_number);
}
int main() {
long long number;
// Gets the number from the user
printf("Enter a positive integer: ");
scanf("%lld", &number);
// Checks if the number is positive
if (number <= 0) {
printf("Error: Please enter a positive integer.\n");
return 1; // Terminates the program with an error
}
// Calls the function to compute the sequence
collatz(number);
// Wait for the user to press Enter before exiting.
printf("\nPress Enter to exit the program...");
// Clear the input buffer from the previous `scanf`.
// This part can be tricky and platform-dependent.
// A safer way is to use a loop to consume characters.
while (getchar() != '\n');
getchar(); // Reads the final newline character.
return 0; // Program proběhl úspěšně
}