Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Don't ask to ask, just ask
 
PerlMonks  

Five digit number display separately

by TimTim (Initiate)
on Sep 04, 2013 at 13:01 UTC ( #1052333=perlquestion: print w/ replies, xml ) Need Help??
TimTim has asked for the wisdom of the Perl Monks concerning the following question:

how to write a program to input a 5 digit number and then seperates the number into its individual digits and prints teh fifth digit 5times, fourth digit 4 times, third digit 3 times and so on. The output should be like: enter 5 digit number: 12345 1 22 333 4444 55555

Comment on Five digit number display separately
Re: Five digit number display separately
by choroba (Abbot) on Sep 04, 2013 at 13:07 UTC
    Sounds like a homework.

    A one liner:

    perl -ne 'print $_ x ++$i, " " for split //'

    To understand, read print, split, perlvar (explains $_), perlop (explains x, ++, and //), perlsyn (explains for), and perlrun (explains -ne).

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Five digit number display separately
by MidLifeXis (Prior) on Sep 04, 2013 at 13:08 UTC

    Welcome to the monastery.

    This sounds a lot like homework. WeI don't have problems helping with homework, as long as it is stated up front, and you show your work.

    Some things that may help you find a solution:

    Also, please note that this should have been posted in SoPW, not meditations. It has been moved.

    Update: s/We/I/; # I do not speak for perlmonks, only for myself. And then only when I am not chewing on my foot.

    --MidLifeXis

Re: Five digit number display separately
by hdb (Parson) on Sep 04, 2013 at 13:35 UTC
Re: Five digit number display separately
by BillKSmith (Hermit) on Sep 04, 2013 at 14:17 UTC
    You have already recieved several good solutions to the printing part of your problem. There are a few things to consider about the input. You should always prompt the user when you expect him to do something like type a number. Remove the newline from the response, and make sure that the input is valid. If not, inform the user and give him another chance. Consider using a module such as IO::Prompt::Hooked.
    Bill
Re: Five digit number display separately
by davido (Archbishop) on Sep 04, 2013 at 16:24 UTC

    #include <iostream> #include <string> using namespace std; int main () { string user_input; cout << "Please enter a five digit integer: "; cin >> user_input; for ( size_t ix = 0; ix != user_input.size(); ++ix ) { for ( size_t count = 0; count <= ix; ++count ) cout << user_input.at(ix); cout << endl; } return 0; }

    Oh, you your instructor wanted Perl? This is where you'll have to learn to adapt. ;) Update: Actually Perl can adapt for you:

    #!/usr/bin/env perl use strict; use warnings; use Inline CPP => <<'EOC'; #include <iostream> #include <string> void doit () { std::string user_input; std::cout << "Please enter a five digit integer: "; std::cin >> user_input; for ( size_t ix = 0; ix != user_input.size(); ++ix ) { for ( size_t count = 0; count <= ix; ++count ) std::cout << user_input.at(ix); std::cout << std::endl; } } EOC doit;

    After you turn in a serious solution, try this just for fun. :)


    Dave

      print "insert 5 digit number:\n"; $string = <STDIN>; for ($i=1; $i<=5; $i++) { $var = substr($string,$i-1,1) Print "$var\n"; } For this i am getting output as: 1 2 3 4 5 someone help me out i am new to perl i need output as 1 22 333 4444 55555
        You are almost there. The only thing missing is to repeat each digit. For that, you can include another loop, or use the repetition operator as adviced in my first reply to your question.

        Also, please use <code> ... </code> tags for code and data samples to improve readability.

        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052333]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-04-17 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (454 votes), past polls