DIGIT-ARC read-only archive · snapshot: July 23, 2026

A simple program help???

7 posts · originally from https://geek.digit.in/community/threads/a-simple-program-help.120838/

Zangetsu
Zangetsu Original post #1
Sep 6, 2009 at 6:09 PM
Hi Guys, :oops:

If have a problem in solving this & i googled but cudn't find ne solution...

if integer a = 12345. write a program (without using any in-built function) in C++/C# or
VB.NET. to find the length of integer a (obviously in this case it is 5).remember if its array then its simple.but its an integer datatype.

pls solve my problem?:cry:
V
Sep 6, 2009 at 6:34 PM
int a=12345;
int count = 0;
while(a !=0 )
{
a = a/10; //removes the last digit of the number; or better a /= 10
count++;
}


after the loop is executed a will be zero count will contain the number of digits in the number. if you want back a just use int b = a; //before the loop a =b; //and after the loop
L
Sep 7, 2009 at 12:01 AM
Code:
int a=12345;
if(a<0)a=-a;
int count = 0;
while(a !=0 )
{
a = a/10; //removes the last digit of the number; or better a /= 10
count++;
}
check for negative
V
Sep 7, 2009 at 10:32 PM
does work. the condition is a ! = 0; not a < 0
Zangetsu
Sep 16, 2009 at 11:23 AM
Ohhh! so simple, Thanks Vishal :oops:

ok...if i want 2 reverse the digit then like o/p= 54321 in the same program :-o
V
Sep 22, 2009 at 12:14 AM
int a=12345;
if(a<0)a=-a;
int count = 0;
int remainder, reverse=0; // ----change over here
while(a !=0 )
{

remainder = a%10; // to read the last digit -----change

a = a/10; //removes the last digit of the number; or better a /= 10

reverse = (reverse * 10) + remainder; //this statement should come before count++ else the value will be 543210

count++;
}
Zangetsu
Sep 24, 2009 at 11:25 AM
int a=12345;
if(a<0)a=-a;
int count = 0;
int remainder, reverse=0; // ----change over here
while(a !=0 )
{

remainder = a%10; // to read the last digit -----change

a = a/10; //removes the last digit of the number; or better a /= 10

reverse = (reverse * 10) + remainder; //this statement should come before count++ else the value will be 543210

count++;
}


Oh..thank u very much...but i think for the above program count is not needed...:-D