Martes, Oktubre 16, 2012

Write a program that will count the digits of a given number in c language using recursion.

Output:
Enter a number: 1234567
Total digits is: 7

Code:
#include<stdio.h>

int countDigits(num);
int main(){
int num,count;

printf("Enter a number: ");
scanf("%d",&num);

count = countDigits(num);

printf("Total digits is: %d",count);
return 0;
}

int countDigits(int num){
static int count=0;

if(num!=0){
count++;
countDigits(num/10);
}

return count; }

Walang komento:

Mag-post ng isang Komento