C program for Hailstones series
March 10, 2020
Hailstones is a sequence of numbers that could be generated from any positive number by performing a specific operation on it until it reaches 1. The fact is that any positive number will reach 1 if we perform this operation. If the number is even, we divide it by 2, otherwise we multiply the number by 3 and add 1 to it. Below is simple C program to generate Hailstone series –
main(){
int n, count = 0;
printf("Enter any number of your choice>>");
do{
scanf("%d", &n);
if(n = 0 || n == 1){
printf("\n\nSorry the operation is possible with 0 or 1. Enter another number>>");
}
}
while(n>1);
printf("\nHAILSTONES Sequence is as below::\n\n%d\n",n);
for( ;n > 1; ){
if(n %2)
n *= 3 + 1;
else
n /= 2;
count += 1;
printf("\n%d\n",n);
}
printf("\n\n--------->Total number of steps to reach 1 is %d",count);
}
For more C program articles, click here – https://www.getallarticles.com/category/software-development/programming/c-programming/