Write a C Program to Create a Hailstone Series Given a First Number ‘x’ by User up to nth Term

October 8, 2022 0 Comments

In this tutorial learn how you can write a c program to create a hailstone series given a first number ‘x’ by user up to nth term.

A Hailstone series is defined as follows: start with any integer value greater than 0, say x. If x is even, then the next value in the series is x/2; if x is odd, then the next value in the series is 3x + 1. Now apply the same rules to create the next value in the series, and so on.

C programming is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, etc.

 

write c program to create hailstone series given a first number x by user up to nth term



 

Write a Program to Create a Hailstone Series Given a First Number ‘x’ by User up to nth Term

Here is the code to create a hailstone series given a first number ‘x’ by user up to nth term in C programming.

#include <stdio.h> //WAP to create a hailstone series given a first number ‘x’ by user up to nth term.
int main()
{
    int x, n, i, s;
    printf("Enter first terms for series:n");
    scanf("%d", &x);
    printf("Enter n terms for series:");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        printf("%dt", x);
        if (x % 2 == 0)
        {
            x = x / 2;
        }
        else
        {
            x = x * 3 + 1;
        }
    }

    return 0;
}

Test Live At Jdoodle

Conclusion

This is how you can create a hailstone series given a first number ‘x’ by user up to nth term. in c programming. Comment below if you like our tutorial.

Leave a comment

Gallery