C Program to Count Trailing Zeros in Integer


This is a C Program to count the number of trailing zeroes in integer.

Problem Description

This C Program counts the number of trailing zeroes in integer.

Problem Solution

Take input from the user and counts the number of trailing zeroes in given integer as shown in the program below.

Program/Source Code

Here is source code of the C Program to count the number of trailing zeroes in integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

 * C Program to Count the Number of Trailing Zeroes in Integer
#include <stdio.h>
void main()
    int j = 31, i, count = 0;
    unsigned int num;
    int b[32] = {0};
    printf("enter the number:");
    scanf("%d", &num);
    while (num != 0)
        if (num & 1 == 1)
            num = num >> 1;
    printf("\n%d", count);
Program Explanation

This C Program we are reading the number using ‘num’ variable. While condition statement is used to check that the number is not equal to 0. If the condition is true then execute the statement.

Note: Join free Sanfoundry classes at Telegram or Youtube

If else condition statement is used to check that the copy of the bit 1 in the value of ‘num’ variable is equal to the value of 1. If the condition is true, then exit the condition statement using break statement.

Otherwise, if the condition is false then execute the else statement by increment the value of ‘count’ variable. Using binary right shift operator the value 1 is moved right by the number of bits specified by the value of ‘num’ variable and assign to ‘num’ variable. Print the number of trailing zero’s in integer.

Runtime Test Cases
$ cc bit4.c
$ ./a.out
enter the number:128
$ ./a.out
enter the number:-127

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.