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;
}
9 28085
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.
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;
}
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?
This is C++. You might try: - stringstream s;
-
s << 12345;
-
string str;
-
s >> str;
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.
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;
}
It seems correct. Does it work OK?
#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;
}
while(c<=5){
fin=x/n; //************** fin=(int)x/n;
n=n/10;
c++;
cout<<fin<<endl;
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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";
|
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 |...
|
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...
|
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...
|
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 :)
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |