Calculate number of digits of any number


Prev Tutorial Next Tutorial

Find number of digits from any number

Any number is a combination of digits, like 342 have three digits. For calculating the number of digits of any number user have three possibilities to input values.

  • Enter +ve Number
  • Enter -ve Number
  • Enter zero

Find number of Digits

This code is correct when user enter +ve numbers Only.

Find number of digits from any numbers

#include<stdio.h>
#include<conio.h>

void main()
{
int no,a=0;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
while(no>0)
{
no=no/10;
a++;
}
printf("Number of digits in given number is: %d",a);
getch();
}

Output

Enter any number: 650
Number of digits in given number is: 3

Find number of digits

This code is satisfy all the above three conditions.

program to find number of digits from any numbers

#include<stdio.h>
#include<conio.h>

void main()
{
int no,a=0;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
if(no<0)
{
no=no * -1; // convert -ve number into +ve number
}
else if(no==0)
{
no=1;
}
while(no>0)
{
no=no/10;
a++;
}
printf("Number of digits in given number is: %d",a);
getch();
}

Output

Enter any number: -650
Number of Digits in given number is: 3

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements