Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C program to Count the digits of a number
Next article icon

C Program to count the number of zeros from 0 to N

Last Updated : 22 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number N, the task is to write C program to count the number of zeros from 0 to N. Examples:

Input: N = 10 Output: 2 Explanation: The number with zeros are 0, 10 till 10. Hence the count of zeros is 2. Input: N = 20 Output: 3 Explanation: The number with zeros are 0, 10, 20 till 20. Hence the count of zeros is 3.

Approach:

  1. Iterate from 0 to N.
  2. For each number do the following:
    • Store the above number in a variable temp.
    • For each digit in temp, if the digit is zero then increment the zero count by 1.
  3. Print the count of zeros calculated in the above steps.

Below is the implementation of the above approach: 

C




// C program for the above approach
#include <stdio.h>
 
// Function to count zero in temp
int countZero(int temp)
{
    int cnt = 0;
 
    // Iterate each digit in temp
    while (temp) {
 
        // If digit is zero, increment
        // the count
        if (temp % 10 == 0)
            cnt++;
 
        temp /= 10;
    }
 
    // Return the final count
    return cnt;
}
 
// Function that counts the number of
// zeros from 0 to N
void countZerostillN(int N)
{
   // To store the finalCount of zero
    int finalCount = 1;
 
    // Iterate from 0 to N
    for (int i = 1; i <= N; i++) {
 
        // Function call to count the
        // zeros in variable i
        finalCount += countZero(i);
    }
 
    // Print the final Count of zero
    printf("%d", finalCount);
}
 
// Driver Code
int main()
{
    // Given number
    int N = 20;
 
    // Function Call
    countZerostillN(N);
}
 
 
Output:
3

Time Complexity: O(N*log10N) 

Auxiliary Space: O(1)



Next Article
C program to Count the digits of a number

M

manikumartharmaraj
Improve
Article Tags :
  • C Language
  • C Programs
  • Programming Language
  • School Programming
  • number-digits

Similar Reads

  • C/C++ Program to Count trailing zeroes in factorial of a number
    Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5 Output: 1 Factorial of 5 is 120 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24Trailing 0s in n! =
    2 min read
  • C program to Count the digits of a number
    Given a number N, write a C program to find the count of digits in the number N. Examples: Input: N = 12345 Output: 5 Explanation: The count of digit in 12345 = 5 Input: N = 23451452 Output: 8 Explanation: The count of digits in 23451452 = 8Methods to Count Digits of a NumberThere are a few methods
    4 min read
  • C program to count zeros and ones in binary representation of a number
    Given a number N, the task is to write C program to count the number of 0s and 1s in the binary representation of N. Examples: Input: N = 5 Output: Count of 0s: 1 Count of 1s: 2 Explanation: Binary representation of 5 is "101".Input: N = 22 Output: Count of 0s: 2 Count of 1s: 3 Explanation: Binary r
    5 min read
  • C/C++ Program to Count set bits in an integer
    Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on “PRACTICE
    2 min read
  • C program to print number of days in a month
    Given a number N, the task is to find the number of days corresponding to each month where 1 is January, 2 is February, 3 is March, and so on. Examples: Input: N = 12 Output: 31 Days Input: N = 2 Output: 28/29 Days Method - 1: using If Else: Get the input month as a number N. If N is one of these va
    4 min read
  • C Program to Check Whether a Number is Positive or Negative or Zero
    Write a C program to check whether a given number is positive, negative, or zero. Examples Input: 10Output: PositiveExplanation: Since 10 is greater than 0, it is positive. Input: -5Output: NegativeExplanation: Since -5 is less than 0, it is negative. Different Ways to Check for Positive Numbers, Ne
    3 min read
  • C Program for Counting Sort
    Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg
    3 min read
  • C program to count Positive and Negative numbers in an Array
    Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples: Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9,
    2 min read
  • C Program For Sorting A Linked List Of 0s, 1s And 2s
    Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL So
    3 min read
  • C Program For Sorting An Array Of 0s, 1s and 2s
    Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences