+ 1

is there anyway if number is 3 digit, it will start divide by thousand, if 4 digit then by 1000 and so on

/*this code is used to seperate numbers individually and print them. if we input a five digit number, we will start it by dividing by 10000(four zeros after 1 and total digits are five). if we have to seperate six digits, we will divide by 100000(5 zeroes after 1 and total digits are 6 as of input)*/ #include<iostream> using namespace std; int main() { int a; cout << "enter a num" << endl; cin >> a; cout << a / 1000 << endl; a = a % 1000; cout << a / 100 << endl; a = a % 100; cout << a / 10 << endl; a = a % 10; cout << a / 1 << endl; } /* INPUT=1234 OUTPUT=1 2 3 4 */

29th Nov 2020, 8:02 AM
Muhammad Ali Zulfiqar
Muhammad Ali Zulfiqar - avatar
4 Réponses
+ 5
Could u clarify ur Question ... Also Can u provide a sample input and output..
29th Nov 2020, 8:09 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 5
Muhammad Ali Zulfiqar Ur code lacks logic .... Try this : cin>>num; int length = 0; int base = 10; int y = x; do { ++length; y /= base; }while(y); int c=1; for (int d=1; d<length; ++d) c= c*10; for ( int i = c; i > 0; i/=10) { cout << num / i <<endl; num = num % i; }
29th Nov 2020, 8:46 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
This code may help you achieve what you're after. At least the part that gives you the length of the number. Then you can just make a loop that counts down from the length and does a % 10 ^ n to get the number you're after, or use the rest of the code to just get the index of the number etc. https://code.sololearn.com/ckMx47j6hN99/?ref=app
29th Nov 2020, 8:48 AM
ChaoticDawg
ChaoticDawg - avatar
0
REVIEW IT NOW
29th Nov 2020, 8:28 AM
Muhammad Ali Zulfiqar
Muhammad Ali Zulfiqar - avatar