A simple program help???

Discussion in 'Programming' started by Zangetsu, Sep 6, 2009.

Thread Status:
Not open for further replies.
  1. Zangetsu

    Zangetsu PM me for any queries in digirForum!

    Joined:
    Jan 8, 2008
    Messages:
    10,058
    Likes Received:
    152
    Trophy Points:
    63
    Location:
    Soul Society
    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:
     
  2. Vishal Patil

    Vishal Patil Linux all the way

    Joined:
    Mar 23, 2008
    Messages:
    158
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Rasayani
    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
     
    Last edited: Sep 6, 2009
  3. lucifer_is_back

    lucifer_is_back New Member

    Joined:
    Oct 17, 2008
    Messages:
    124
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. Vishal Patil

    Vishal Patil Linux all the way

    Joined:
    Mar 23, 2008
    Messages:
    158
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Rasayani
    does work. the condition is a ! = 0; not a < 0
     
  5. OP
    OP
    Zangetsu

    Zangetsu PM me for any queries in digirForum!

    Joined:
    Jan 8, 2008
    Messages:
    10,058
    Likes Received:
    152
    Trophy Points:
    63
    Location:
    Soul Society
    Ohhh! so simple, Thanks Vishal :oops:

    ok...if i want 2 reverse the digit then like o/p= 54321 in the same program :-o
     
  6. Vishal Patil

    Vishal Patil Linux all the way

    Joined:
    Mar 23, 2008
    Messages:
    158
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Rasayani
    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++;
    }
     
  7. OP
    OP
    Zangetsu

    Zangetsu PM me for any queries in digirForum!

    Joined:
    Jan 8, 2008
    Messages:
    10,058
    Likes Received:
    152
    Trophy Points:
    63
    Location:
    Soul Society

    Oh..thank u very much...but i think for the above program count is not needed...:-D
     
Thread Status:
Not open for further replies.

Share This Page