I have a int varible: int a = 1234
then i want to return each digit seperately like this.:
1
2
3
4
any how?
I have a int varible: int a = 1234
then i want to return each digit seperately like this.:
1
2
3
4
any how?
This would count out 1,234 in order...
this would show your a= 1234 in reverse ordeR:Code:int main() { for (int i = 0; i <=1234; i++) { cout<<i<<endl; } return 0; }
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.
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.
i don't think you can do that.
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.
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;
One way I can figure
Now; put that in a recursive function, because I sure as hell can't.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;
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), soOriginally 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;
Nice solution though.Code:int get_sum(int num) { int sum =0; while (num) { sum+=num%10; num/=10; } return sum; }
Whoops, alright who put that '!' operator in my code??
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..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; }
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)
Thanks you...