Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
C++
// Main function of the C++ program.

#include <iostream>
using namespace std;

int main()
    {
        int x,op,tp;
        cout<<"Enter a number"<<endl;
        cin>>x;
        op=x%10;
        cout<<"the value in the once place is:"<<op<<endl;
        tp=x/10;
        cout<<"The value in the tens place is:"<<tp<<endl;
        return 0;
    }


What I have tried:

What I intend to do is to generalise the program so that it not only prints numbers in ones and tenth place in a two digit number, but any.

For example,
output: enter any number with how many ever digit;
input:x;
logic: // print the numbers in the once, tens, hundredths, thousandths place and so on that is if number = 4567, then output = number in thousandth place =4, number in hundredth place =5, number in tenth place = 6 and number in ones place= 7.

What should the code be? How should the code go. Please explain the idea and logic?
Posted
Updated 10-Apr-19 16:21pm
v3
Comments
Kornfeld Eliyahu Peter 23-Jul-17 6:10am    
Even you are 'an entry level beginner' it would be much better you share your non-working code...
Deepak Kumar Choudhary 23-Jul-17 6:56am    
My apologies. I just expanded my question and clarified what I needed. Looking forward to your response.
Thank you.
Patrice T 23-Jul-17 6:25am    
Whatever is your level of expertise in programming, keep in mind that we don't know what you talk about, we are not in your head, we don't see your screen.
When you ask a question:
-Explain what you try to do. improve until you can understand without using your untold knowledge of the problem.
- Show sample input with expected output and actual output.
- Show your code and an explanation of the problem.
Deepak Kumar Choudhary 23-Jul-17 6:55am    
My apologies for being unclear. This question was my second question of all time. So, I have tried to expand and clarify the question. I look forward to the reply.
Thank you.
Deepak Kumar Choudhary 23-Jul-17 10:33am    
After trying for some time, I got this solution. It would really mean a lot if you could help me with the printing of the the tens digit is, ones thousandth digit. Thank you.
#include <iostream>
using namespace std;

int main()
{
int x;
int count=1;
cout<<"Enter a number";
cin>>x;
while(x>0)
{
cout<<itoa(count)<<"th place" << x % 10;
x=x%10;
count=count/10;
}
}

Once again, stop guessing, and think.
Use the your solution to your other question this morning: How do I find the length of a question in C++?[^] and combine it with that code. It's pretty obvious!
 
Share this answer
 
Comments
Deepak Kumar Choudhary 23-Jul-17 7:13am    
I tried again.
// Main function of the C++ program.

#include <iostream>
using namespace std;

int main()
{
int x=0,op,tp,hp;
int xlen=1;
cout<<"Enter a number"<<endl;
cin>>x;
do
{
x=x/10;
xlen++;
}
while(x!=0);
cout<<"The length of the number="<<xlen<<endl;
switch(xlen)
{
case 1:
op=x%10;
cout<<"the value in the once place is:"<<op<<endl;
break;
case 2:
op=x%10;
cout<<"the value in the once place is:"<<op<<endl;
tp=x/10;
cout<<"The value in the tens place is:"<<tp<<endl;
break;
case 3:
op=x%10;
cout<<"the value in the once place is:"<<op<<endl;
tp=x/10;
cout<<"The value in the tens place is:"<<tp<<endl;
hp=x/100;
cout<<"The value in the hundredth place is:"<<hp<<endl;
break;
default: cout<<"try again";



}




return 0;
}

I am unable to generalise. Please help!
Graeme_Grant 23-Jul-17 7:21am    
Have you set a breakpoint and stepped through with a debugger to see exactly what your code is doing?
I don't program in C++ however the logic is still the same in any language. Here is a fairly recent coding challenge that uses a strategy that meets your requirements in your question and should point you in the right direction:

Coding challenge: convert an integer to a sentence.[^]

** UPDATE:

Here is a C# quick solution:
C#
static void Main(string[] args)
{
    Console.Write("Input: ");
    string input = Console.ReadLine();
    for (int i = input.Length - 1; i >= 0; i--)
    {
        double digitPosition = Math.Pow(10, i); // 10^i
        Console.WriteLine($"{digitPosition} = {input.Substring(input.Length - i - 1, 1)}");
    }
    Console.ReadKey();
}

Which outputs:
Input: 4567
1000 = 4
100 = 5
10 = 6
1 = 7

Should be pretty easy to convert to C++
 
Share this answer
 
v2
//Program to find once tenth hundredth thousandth of a 4dig num
#include <iostream>
using namespace std;
int main()
{
int dig;
cout << "enter a four dig num " << endl;
cin >> dig ;
int git;
git = (dig % 10);
cout << "num in once place is: " << git << endl;
int one;
one = git;
git = (dig % 100);
cout << "num in tenth place: " << (git - one) / 10 << endl;
git = (dig % 1000);
cout << "num in hundredth:" << (git - one) / 100 << endl;
git = (dig % 10000);
cout << "num in thousandth: " << (git - one) / 1000 << endl;
return 0;
}
 
Share this answer
 
Comments
Dave Kreskowiak 10-Apr-19 22:55pm    
Doing peoples homework for them is frowned upon.
CHill60 11-Apr-19 3:55am    
If they still need the solution to their homework after all this time they are going to fail the course!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900