5. if and for Statements

Exercises

Note: If the task description says that the program has to read in one natural number, then the program does not have to verify that the input value truly respects that requirement. The task description actually limits what a user is allowed to provide as the input data. Similarly, the program does not need to verify that the input numbers are different if the task description states that they have to be.

  1. Modify the preceding program so that it now prints out squares of the first 20 natural numbers.
  2. Write a program that reads in one natural number (let us call it n) and then prints out squares of the first n natural numbers.
  3. Write a program that reads in two numbers (from standard input) and then writes out the value of the smaller one.
  4. Write a program that reads in three different numbers and then writes out the value of the smallest one.
  5. Write a program that reads in one natural number and then writes out a message telling whether the given number has exactly two digits. If you need a hint, see the footnote[*].
  6. Let us play with corporate taxes. Write a program that reads in two numbers: the annual income and annual expenses of a company, expressed in dollars. The program has to write out the amount of tax that the company has to pay; let us call it the profit tax. If the expenses are greater than the income, then the profit tax equals 0 dollars. Otherwise, let the profit tax be equal to 20% of the profits, where profits equal the difference of income and expenses.

Click here for solutions