Problem statement:- Program to count the number of digits 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 digits in a number using recursion.
Code:
#include<stdio.h>
int count_digit(int num)
{
static int count=0;
if(num!=0)
{
count++;
count_digit(num/10);
}
return count;
}
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
printf("The number of digits in the Given Number is %d",count_digit(n));
}
Enter a number:4512556
The number of digits in the Given Number is 7
Program in C++
Here is the source code of the C++ Program to Count the number of digits in a number using recursion.
Code:
#include<iostream>
using namespace std;
int count_digit(int num)
{
static int count=0;
if(num!=0)
{
count++;
count_digit(num/10);
}
return count;
}
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
cout<<"The number of digits in the Given Number is "<<count_digit(n);
}
Enter a number:123456
The number of digits in the Given Number is 6
Program in Java
Here is the source code of the Java Program to Count the number of digits in a number using recursion.
Code:
import java.util.Scanner;
public class NumberOfDigit {
int count=0;
int count_digit(int num)
{
if(num!=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();
NumberOfDigit ob=new NumberOfDigit();
System.out.print("The number of digits in the Given Number is "+ob.count_digit(n));
cs.close();
}
}
Enter a number:456
The number of digits in the Given Number is 3
Program in Python
Here is the source code of the Python Program to Count the number of digits in a number using recursion.
Code:
count=0
def count_digit(num):
global count
if (num != 0):
count +=1
count_digit(num // 10)
return count
n=int(input("Enter a number:"))
print("The number of digits in the Given Number is ",count_digit(n))
Enter a number:7012549632
The number of digits in the Given Number is 10
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box