C Program to Count Trailing Zeros in Integer

This C program is designed to count the number of trailing zeros in the binary representation of an integer. Trailing zeros are zeros that appear at the end (least significant bit) of a binary number. This program provides a simple and efficient way to count trailing zeros in an integer’s binary representation.

Problem statement

Problem: Count Trailing Zeros in an Integer Write a program that takes an integer as input and counts the number of trailing zeros in its binary representation. A trailing zero is defined as a zero bit at the end (least significant bit) of the binary representation.

Your program should perform the following steps:

  1. Prompt the user to enter an integer.
  2. Read the integer from the user.
  3. Convert the integer to its binary representation.
  4. Count the number of trailing zeros in the binary representation.
  5. Display the count of trailing zeros as output

C Program to Count Trailing Zeros in Integer

#include <stdio.h>

int countTrailingZeros(int n) {
    int count = 0;
    
    // Loop until the rightmost bit is 1
    while ((n & 1) == 0) {
        count++;
        n >>= 1; // Right shift by 1 bit
    }
    
    return count;
}

int main() {
    int number;
    
    printf("Enter an integer: ");
    scanf("%d", &number);
    
    int zeros = countTrailingZeros(number);
    printf("Number of trailing zeros: %d\n", zeros);
    
    return 0;
}

How it works

  1. The program prompts the user to enter an integer.
  2. The entered integer is read from the user and stored in the variable number.
  3. The program calls the function countTrailingZeros with the number as an argument.
  4. Inside the countTrailingZeros function, the variable count is initialized to 0. This variable will keep track of the number of trailing zeros.
  5. The while loop (n & 1) == 0 checks if the rightmost bit of n is 0. If it is, it means there is a trailing zero.
  6. Inside the loop, the count variable is incremented by 1, and n is right-shifted by 1 bit using the >>= operator. This shift effectively removes the rightmost bit.
  7. The loop continues until the rightmost bit of n becomes 1, indicating the end of trailing zeros.
  8. Finally, the function returns the value of count.
  9. Back in the main function, the returned value from countTrailingZeros is stored in the variable zeros.
  10. The program displays the number of trailing zeros by printing the value of zeros using the printf function.

By following these steps, the program correctly counts the number of trailing zeros in the binary representation of the entered integer and displays the result

Input / Output

C Program to Count Trailing Zeros in Integer

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program finds the Greatest Common Divisor (GCD) of two given numbers. Problem Statement Write a C program that...
This C program calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0....
This C program allows you to find the length of a linked list, which is the number of nodes present...