answersLogoWhite

0


Best Answer

#include
#include

void main()
{
unsigned long int num;
int i=0;
clrscr();
printf("Enter the digit\n");
scanf("%lu",&num);
while(num!=0)
{
num=num/10;
++i;
}
printf("Length=%d",i);
getch();
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

There are two ways. The naive way is to convert the number to a string by inserting it into a stringstream object and returning the size of the resultant string. A more efficient method is to repeatedly divide the number by 10 until the result is zero. The number of divisions gives the count of digits.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

#include<stdio.h>

unsigned count_digits (unsigned num) {

unsigned sum;

sum = 1;

while (num /= 10) ++sum;

return sum;

}

int main (void ) {

unsigned num;

printf ("Enter a positive integer: ");

scanf ("%d", num);

printf ("There are %d digits in the number %d\n", count_digits (num), num);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

I suggest to convert it to a String, then use the String method to count the number of characters.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program which accepts a positive integer and displays the number of digits?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Write a program that input a positive integer and prints a triangle using for loop?

write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?


Which microprocessor accepts the program written for 8086 without any changes?

Which microprocessor accepts the program written for 8086 without any changes?


How can I write a program to display prime numbers from 1 to 100?

Write a function that implements an algorithm that checks to see if a particular integer is prime (returning a boolean). Write a program that uses that function on each number from 1 to 100, and if true, displays that number.


Write a program to subtract integer y from integer x?

x -=y;


Which software program displays advertising on your computer?

youtube


Create a program which accepts a number not less than 10The program prints the reverse of the number?

ten


Is -5 a integer?

1 hour ago my c program said no, but now I know 5 actually is an integer!


What displays the name of the document and the name of the program?

title bar


What is a rectangular box that displays information or a program?

A screen (VDU).


What drug treatmeant program accepts medi-caid in Florida?

gfgyhgcfh


Write a c program which displays digital watch on the screen after taking the hours and minutes from the user?

Write a program which displays digital watch on the screen, after taking the hours and minutes from the user.