Write a program to count number of digits in a number

// Write a program to count number of digits in a number
// Ex: 4 -> 1, 23445 -> 5
#include
int main(){
    int n;
    printf("Enter a positive integer:");
    scanf("%d",&n); // input from user
    while(n<0){ printf("Please enter positive integer only:"); scanf("%d",&n); } int count=0; if(n==0){ count++; } while(n>0){
         count++; // counting no. of digits
         n = n/10;
    }
    printf("Number of digits in given number: %d",count);
    getch();
}

No comments:

Post a Comment