Answer:
#include <iostream>
using namespace std;
int func(int n)//function..
{
if(n>1)
{
if(n%2==0)//checking if the number is even.
{
return n/2;
}
else
{
return (3*n+1);
}
}
}
int main() {
int num;//taking input of the n.
cin>>num;
while(num>1)//looping while the value of num is greater than 1.
{
int a=func(num);//calling function.
cout<<a<<" ";//printing .
num=a;//changing the value of num.
}
return 0;
}
Input:-
10
Output:-
5 16 8 4 2 1
Step-by-step explanation:
I have created the function named func. Then returning the values according to the problem.In the main function taking the input of integer num. Then calling a loop and calling the function till the value of n is greater than 1 and printing all the values returned by the function.