Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 06-09-2009, 06:09 PM   #1 (permalink)
Fullbring
 
Zangetsu's Avatar
 
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
Question A simple program help???


Hi Guys,

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?
__________________
I'm the One you've been Waiting for...
Zangetsu is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 06-09-2009, 06:34 PM   #2 (permalink)
Linux all the way
 
Join Date: Mar 2008
Location: Rasayani
Posts: 155
Default Re: A simple program help???

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
__________________
At times life takes a U turn and you get back where you were...

Last edited by Vishal Patil; 06-09-2009 at 06:45 PM.
Vishal Patil is offline  
Old 07-09-2009, 12:01 AM   #3 (permalink)
Broken In
 
Join Date: Oct 2008
Posts: 133
Default Re: A simple program help???

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
lucifer_is_back is offline  
Old 07-09-2009, 10:32 PM   #4 (permalink)
Linux all the way
 
Join Date: Mar 2008
Location: Rasayani
Posts: 155
Default Re: A simple program help???

does work. the condition is a ! = 0; not a < 0
__________________
At times life takes a U turn and you get back where you were...
Vishal Patil is offline  
Old 16-09-2009, 11:23 AM   #5 (permalink)
Fullbring
 
Zangetsu's Avatar
 
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
Default Re: A simple program help???

Ohhh! so simple, Thanks Vishal

ok...if i want 2 reverse the digit then like o/p= 54321 in the same program
__________________
I'm the One you've been Waiting for...
Zangetsu is offline  
Old 22-09-2009, 12:14 AM   #6 (permalink)
Linux all the way
 
Join Date: Mar 2008
Location: Rasayani
Posts: 155
Default Re: A simple program help???

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++;
}
__________________
At times life takes a U turn and you get back where you were...
Vishal Patil is offline  
Old 24-09-2009, 11:25 AM   #7 (permalink)
Fullbring
 
Zangetsu's Avatar
 
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
Default Re: A simple program help???

Quote:
Originally Posted by Vishal Patil View Post
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...
__________________
I'm the One you've been Waiting for...
Zangetsu is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2