Search for '{{search_term}}'

Exercise: Calculate the area of a rectangular

CMOS is the Code-Maven Open Source podcast that also includes video interviews. Subscribe to this feed RSS feed with your Podcast listener app or via iTunes iTunes.

Exercise: Calculate the area of a rectangular

Write a program that computes the area of a rectangular.

  • Start by having two hard coded values. Multiply them and print the result. The result should be 42:
    $ perl example.pl
    42
    
  • Then change the code to prompt the user for two numbers. Multiply them and print the result:
    $ perl example.pl
    First number: 7
    Second number: 6
    42
    
    # ruby example.rb
    First number: 3
    Second number: 11 
    33
    
  • Further modify the program to warn if one of the numbers given was negative.
    $ python example.py
    First number: 3
    Second number: -1
    Warning: Invalid input. The side of a rectangular cannot be negative.
    

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus