473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Split number into digits

hey sir i am having problem in splitting a number into its digits if i use remainder and division operation then i can split a number and print it in reverse order but dont know how to split that number and print that in correct order
here is my program

#include<iostream>
using namespace std;
int main()
{
int x=12345,r,c=1;
while(c<=5){
r=x%10;
x=x/10;
cout<<r;
c++;


}

cin.get();
return 0;
}
Nov 20 '11 #1
9 28085
alexis4
113 100+
Follow this algorithm:

(12345/10000)%10 = 1
(12345/1000)%10 = 2
(12345/100)%10 = 3
(12345/10)%10 = 4
(12345/1)%10 = 5


Implement this algorithm in code and off you go!

EDIT: Obviously the "%10" in line 1 and the "/1" in line 5 is not needed. But I gave it this way to make the algorithm more obvious, in case you implement a loop.
Nov 20 '11 #2
well mmm i did this before i read your reply i think i succeeded anyway thanks for reply i'm really grateful to you

#include<iostream>
using namespace std;
int main()
{
int x=12345,c=1,n=10000,fin;
while(c<=5){
fin=x/n;
n=n/10;

c++;
cout<<fin<<endl;
}
cout<<endl;
cout<<"at the end here you go "<<fin<<endl;

cin.get();
return 0;
}
Nov 20 '11 #3
alexis4
113 100+
while(c<=5){
fin=x/n;
n=n/10;

c++;
cout<<fin<<endl;
}
How can that work? You are printing fin right? So:

Loop 1: fin=12345/10000 = 1
Loop 2: fin = 12345/1000 = 12

So how could you separate the digit without the remainder?
Nov 20 '11 #4
weaknessforcats
9,208 Expert Mod 8TB
This is C++. You might try:

Expand|Select|Wrap|Line Numbers
  1. stringstream s;
  2.  s << 12345;
  3.  string str;
  4.  s >> str;
Nov 21 '11 #5
donbock
2,426 Expert 2GB
Does your function need to handle negative inputs?

Look at your code in the original post. Looping 5 times only works if the input is a 5-digit number. Instead, loop until x/10 is zero.

Your original algorithm collects digits from right to left. Make the change I suggested above and your original algorithm works for all positive inputs regardless of how many digits they have. On the other hand, the revised algorithms that collect digits from left to right are tuned to a specific number of digits. I suggest you use the original algorithm to fill an array with digits and then print the array contents in reverse order. That leaves you with the most flexible function.
Nov 21 '11 #6
ok how about this one check this out
#include<iostream>
using namespace std;
int main()
{
int x=12345,c=1,n=10000,fin;
while(c<=5){
fin=x/n;
n=n/10;
fin%=10;
c++;
cout<<fin<<endl;
}
cout<<endl;
cin.get();
return 0;
}
Nov 21 '11 #7
alexis4
113 100+
It seems correct. Does it work OK?
Nov 23 '11 #8
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main()
{
int c=0,fin,r;
int x,xx,n;
cout<<"Enter the number ; ";
cin>>x;
xx=x;
while(x!=0)
{
r=x%10;
c++;
x=x/10;
}
c--;
n=pow(10,c);
while(n!=0)
{
fin=xx/n;
fin=fin%10;
cout<<fin<<endl;
n=n/10;
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Nov 25 '11 #9
while(c<=5){
fin=x/n; //************** fin=(int)x/n;
n=n/10;

c++;
cout<<fin<<endl;
}
Jan 13 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: nicolas | last post by:
Aim: scanf a number what random digit,split this number; for example: input "5680" output "5,6,8,0"; input "45" output "4,5";
11
by: Jay | last post by:
Hey Guys, I need an algorithm/formula to do the following: I have two 32-bit timers cascaded to form a 64-bit timer, max value per timer(50sec). This is the way they work: value | timer1 |...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
4
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am trying to convert the digits to even number so for example if I have 3 digit number, I want it to be 4 digit and If I have 5 digit number, I want it to be 6 digit. How can i...
9
by: Jason7899 | last post by:
hi, i have a number with 9 digits lenght and i want split this number: 922888222 into this: 922 888 222 how can i do that? thanks a lot for your help :)
5
by: Alex Hoilo | last post by:
Hello, I'm a novice to VBA and need to import raw data into an access database. I created the table, it contains 87 fields, all numeric. The data I have looks like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.