Find Number of Digits of Any Numbers in C


Prev Tutorial Next Tutorial

Calculate Number of Digits of Any Numbers in C

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 of Any Numbers in C

#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.

Calculate Number of Digits of Any Numbers in C

#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




Download Projects

Advertisements

FREE Online Google Play APK Downloader

Buy APP Installs, Buy 100% Genuine Installs