#include <iostream>
usingnamespace std;
void hailstone( int = 1 );
int main()
{
int A,n;
cout <<"Number:" ;
cin >> A;
if (A<1){
cout<<"Invalid number, please try again."<<endl;
}
if (A==1){
cout<<"1"<<endl;
}
else{
hailstone(A);
}
return 0;
}
void hailstone(int n)
{
int m;
cout<<"Limit of terms: ";
cin >> m;//To decide when to print this.
do{
for (int i = 0; i < m; i++)
{
if (n%2 == 0)
n=n/2;
else
n=(3*n)+1;
cout << n << endl;
}
}while(n>1);
}
1. 'Limit of terms:' works, and when it reaches 1 it should stop regardless of this but it continues.
2.When printing numbers, input number must also be printed first.
example out put
Number: 15
Limit of terms: 50
15
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1
#include <iostream>
void hailstoneSequence(int);
int main()
{
int n{};
std::cout << "Starting Number: ";
std::cin >> n;
hailstoneSequence(n);
}
void hailstoneSequence(int n)
{
while (n != 1) // As long as the n value is not 1, keep looping. Stop when n is 1.
{
if (0 == n % 2)
{
n = n / 2;
}
else
{
n = (3 * n) + 1;
}
std::cout << n << std::endl;
}
}