Home » C solved programs » C digits manipulation programs

C program to count total number of digits of an Integer number

This program will read an integer number and count total number of digits of input number. for example there is a number 1234 and total number of digits are 4.

Logic behind to implement this program - Divide number by 10 and count until number is not zero, before using counter variable, initialize the count variable by 0.

Count Digits of a Number using C program

/* C program to count digits in a number.*/

#include <stdio.h>

int main()
{
	int num,tNum,cnt;

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

	cnt=0;
	tNum=num;

	while(tNum>0){
		cnt++;
		tNum/=10;
	}

	printf("Total numbers of digits are: %d in number: %d.",cnt,num);
	
	return 0;
}

Using User Define Function

/* C program to count digits in a number.*/

#include <stdio.h>

/*function to count digits*/
int countDigits(int num)
{
    int count=0;
    while(num>0)
    {
        count++;
        num/=10;
    }
    return count;
}

int main()
{
    int num,tNum,cnt;
 
    printf("Enter a number: ");
    scanf("%d",&num);
 
    cnt=countDigits(num);
 
    printf("Total numbers of digits are: %d in number: %d.",cnt,num);
     
    return 0;
}

Output

    Enter a number: 28765
    Total numbers of digits are: 5 in number: 28765.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.