This is a C Program to count the number of trailing zeroes in integer.
This C Program counts the number of trailing zeroes in integer.
Take input from the user and counts the number of trailing zeroes in given integer as shown in the program below.
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) { break; } else { count++; num = num >> 1; } } printf("\n%d", count); }
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.
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.
$ cc bit4.c $ ./a.out enter the number:128 7 $ ./a.out enter the number:-127 0
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Apply for C Internship
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy Computer Science Books