Thread: Quick Help!

  1. #1
    Ice_Black
    Guest

    Question Quick Help!

    I have a int varible: int a = 1234

    then i want to return each digit seperately like this.:
    1
    2
    3
    4

    any how?

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    This would count out 1,234 in order...

    Code:
    int main()
    {
    for (int i = 0; i <=1234; i++)
    {
    cout<<i<<endl;
    }
    
    return 0;
    }
    this would show your a= 1234 in reverse ordeR:

    Code:
    int main()
    {
    int a = 1234;
    
    while(a > 0)
    {
    cout<<a<<endl;
    a--;
    }
    
    return 0;
    }
    Last edited by RoD; 12-25-2002 at 12:01 PM.

  3. #3
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    If you are using BC++, try itoa() in stdlib.h
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  4. #4
    Ice_Black
    Guest

    Exclamation

    I dont want it like that..

    i dont want the number to decrease.

    int a = 976724;

    so it would return, 9,7,6,7,2,4;

    so i would be able to add 9 + 7 + 6 + 7 + 2 + 4 from int a.

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i don't think you can do that.

  6. #6
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Try something like

    int i=10;
    int a=123456;
    int d;

    a%i must be 6 --to be kept (may be in an array)
    d=a/i; // d becomes 12345
    a=d;
    a%i must be 5 --to be kept

    do the same in some routines.
    sorry, I did not write program here, just an idea.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    the idea of more variables would work but hes saying he wants one variable, and wants to add(i think it was add) the numbers in it.

    a = 12

    (1 + 2)

    a would = 3;

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    One way I can figure
    Code:
       int num = 1234567;
       int n[7];
    
       n[0] = num - (num/10)*10;
       n[1] = (num - (num/100)*100 - n[0])/10;
       n[2] = (num - (num/1000)*1000 - n[1])/100;
       n[3] = (num - (num/10000)*10000 - n[2])/1000;
    Now; put that in a recursive function, because I sure as hell can't.

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You could do it with one more intermediate variable.
    Code:
    int a=12345;
    int num=0;
    
    while (!a)
    {
       num+=a%10;
       a/=10;
    }
    
    a=num;

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by PJYelton
    You could do it with one more intermediate variable.
    Code:
    int a=12345;
    int num=0;
    
    while (!a)
    {
       num+=a%10;
       a/=10;
    }
    
    a=num;
    I believe that should be while (a), so

    Code:
    int get_sum(int num)
    {
       int sum =0;
       while (num)
       {
          sum+=num%10;
          num/=10;
       }
       return sum;
    }
    Nice solution though.

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Whoops, alright who put that '!' operator in my code??

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    workable version, uncompiled

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      long input;
      int array[10];
      int index = -1;
      
      cout << "enter an integer of value less than 10,000,000 << endl;
      cin > input;
      while(input)
      {
         array[++index] = input % 10;
         input  /= 10;
      }
       
       for( ; index > -1; --index)
       {
          cout << array[index] << endl;
       }
       return 0;
    }

  13. #13
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Originally posted by elad
    workable version, uncompiled

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      long input;
      int array[10];
      int index = -1;
      
      cout << "enter an integer of value less than 10,000,000" << endl;
      cin >> input;
      while(input)
      {
         array[++index] = input % 10;
         input  /= 10;
      }
       
       for( ; index > -1; --index)
       {
          cout << array[index] << endl;
       }
       return 0;
    }
    2 Errors were in the code I have added ,mentioned in red color..

    and I have write a program ... which also requires to seperate each input digit... for adding them with each.. other...It could help too..
    Code:
    include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
     int main(void)
      {
       int leng,res=0; //leng for length of input number.
       long int number,divider=1,remainder; //
       char *value; //value will be recieved in string...
       clrscr();
    
       cout<<"Enter any long integer: ";
       cin>>value;
       leng=strlen(value); //find the length of the string.
       number = atol(value);
       for (int i = 0; i<leng-1; i++)
        {
         divider = divider * 10;
        }
        res = 0; //note result is intitiazed to zero.
       for (i = 0; i<leng; i++)
        {
         res +=  number / divider;
         number = number % divider;
         divider = divider / 10;
        }
       cout<<"Result is:\t"<<res<<endl;
       getch();
    
       return 0;
      }
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  14. #14
    Ice_Black
    Guest

    ...

    Thanks you...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM