-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign2C.c
56 lines (47 loc) · 1.33 KB
/
Assign2C.c
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
//To follow the given sequence and print the values at all operations performed and also the no. operations performed and also required error message.
#include<stdio.h>
int j=0;
void sequence(long long n) //sequence function made to recursively follow the sequence.
{
if(n==1) //if block made to terminate the recursion.
{
printf("Final value->%lld\n",n);
return ;
}
else if(n%2==0)
{
j++;
printf("Next value is %lld\n",n);
sequence(n/2); //sequence function recursively called.
}
else
{
j++;
printf("Next value is %lld\n",n);
sequence(3*n+1); //sequence function recursively called.
}
}
int main()
{
long long n;
printf("Enter a number->");
scanf("%lld",&n); //n is no. entered.
printf("Initial value is %lld\n",n);
if(n<1) //if block made to print the required message at proper condition.
{
printf("Error\n");
return 0;
}
if(n!=1)
{
if(n%2==0)
sequence(n/2); //sequence function called.
else
sequence(3*n+1); //sequence function called.
j++;
printf("Operations performed->%d",j);
}
else
printf("Final value is same as initial value.\nOperations performed->0");
return 0;
}