Problem statement:- Program to Count the number of zeros in a number using recursion.
Data requirement:-
Input Data:- n
Output Data:-count_digit(n)
Output Data:-count_digit(n)
Program in C
Here is the source code of the C Program to Count the number of zeros in a number using recursion.
Code:
#include<stdio.h>
int count_digit(int num)
{
static int count=0;
if(num>0)
{
if(num%10==0)
count++;
count_digit(num/10);
}
return count;
}
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
printf("The number of Zeros in the Given Number is %d",count_digit(n));
}
Enter a number:10002054
The number of Zeros in the Given Number is 4
Program in C++
Here is the source code of the C++ Program to Count the number of zeros in a number using recursion.
Code:
#include<iostream>
using namespace std;
int count_digit(int num)
{
static int count=0;
if(num>0)
{
if(num%10==0)
count++;
count_digit(num/10);
}
return count;
}
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
cout<<"The number of Zeros in the Given Number is "<<count_digit(n);
}
Enter a number:512456
The number of Zeros in the Given Number is 0
Program in Java
Here is the source code of the Java Program to Count the number of zeros in a number using recursion.
Code:
import java.util.Scanner;
public class CountNumberOfZero {
int count=0;
int count_digit(int num)
{
if(num>0)
{
if(num%10==0)
count++;
count_digit(num/10);
}
return count;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n;
System.out.print("Enter a number:");
n=cs.nextInt();
CountNumberOfZero ob=new CountNumberOfZero();
System.out.print("The number of Zeros in the Given Number is:"+ob.count_digit(n));
cs.close();
}
}
Enter a number:20005
The number of Zeros in the Given Number is:3
Program in Python
Here is the source code of the Python program to Count the number of zeros in a number using recursion.
Code:
count=0
def count_digit(num):
global count
if (num >0):
if(num%10==0):
count +=1
count_digit(num // 10)
return count
n=int(input("Enter a number:"))
print("The number of Zeros in the Given number is:",count_digit(n))
Enter a number:505066660
The number of Zeros in the Given number is: 3
Most Recommend Questions:-
More Questions:-
2 Comments
what about 0100 in this case 0 is returned instead of 2
ReplyDeleteif the input is only (0) it will return 0 instead of 1
ReplyDeletePlease do not Enter any spam link in the comment box