If statement exercises
Even or Odd? (PS13L)
Write a program that reads an integer from the user. Then your program should display a message
indicating whether the integer is even or odd.
Write a program that implements the conversion from human years to dog years described in the
previous paragraph. Ensure that your program works correctly for conversions of less than two
human years and for conversions of two or more human years. Your program should display an
appropriate error message if the user enters a negative number.
Year Animal
2000 Dragon
2001 Snake
2002 Horse
2003 Sheep
2004 Mokey
2005 Rooster
2006 Dog
2007 Pig
2008 Rat
2009 Ox
2010 Tiger
2011 Hare
Write a program that reads a year from the user and displays the animal associated with that year.
Your program should work correctly for any year greater than or equal to zero, not just the ones
listed in the table.
Loop exercises
Sentinel Average (P26L)
In this exercise you will create a program that computes the average of a collection of values
entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will
be provided. Your program should display an appropriate error message if the first value entered by
the user is 0. For example:
10 20 30 40 50 60 0
returns 35 as result.
Hint: Because the 0 marks the end of the input it should not be included in the average.
1/n*11 series
Write a program that reads a value n from the user and then calculates the result of the following
series:
1-1/22+1/33-1/44+1/55-…±1/n*11
Pitagoric Table (P13L)
Write a program that reads an integer n between 1 and 10 and then displays in the screen a
pitagoric table for the numbers from 1 to n. The program must check for errors. For example, if user
puts 3, the program must show:
1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
Sentinel Sum
Write a program that calculates the sum of a sequence of integers read from the user. Suppose that
the first integer entered specifies the remaining numbers to enter. The program must read a number
at a time. A typical sequence of number could be:
Hint: Because the first number marks the start of the input it should not be included in the sum.
Sentinel Minimum
Write a program that find the smallest of several integers. Suppose that the first value entered
specifies the remaining number to enter. For example:
5 100 7 89 -35 18
returns 7 as result.
Hint: Because the first number marks the start of the input it should not be included in the
calculation.
Lists / array exercises
first
second
first
third
second
first
second
third
Write a main program that demonstrates your function. Your function should read a list of numbers
from the user and remove the two largest and two smallest values from it. Display the list with the
outliers removed, followed by the original list. Your program should generate an appropriate error
message if the user enters less than 4 values.
Max in position
Write a program that reads a list of number from the user, and then indicates which is the greatest
value and its position in the list.
Filling an array
Write a program defining an array nums that will store 100 numbers initialized like below:
C:
int nums[100] = {1, 3, 5};
Python:
nums = [1, 3, 5]
Although if the array can store 100 numbers, only the first three values have been initialized. Your
program must generates the remaining values integers according to the next formula:
Matrix by scalar
Realizar la multiplicación de una matriz de mxn por un escalar.
Functions
Capitalize It (PS48L)
Many people do not use capital letters correctly, especially when typing on small devices like smart
phones. In this exercise, you will write a function that capitalizes the appropriate characters in a
string. A lowercase “i” should be replaced with an uppercase “I” if it is both preceded and followed
by a space. The first character in the string should also be capitalized, as well as the first non-space
character after a “.”, “!” or “?”. For example, if the function is provided with the string “what time do i
have to be there? what’s the address?” then it should return the string “What time do I have to be
there? What’s the address?”. Include a main program that reads a string from the user, capitalizes it
using your function, and displays the result.
Twin Primes
Los números “primos gemelos” son aquel par de números primos que difieren por 2; por ejemplo, el
3 y el 5 son números “primos gemelos”. Escriba un programa que imprima todos los números “primos
gemelos” desde 1 hasta el 1000.
Hypotenuse
Defina una función hipotenusa() que calcule la longitud de la hipotenusa de un triángulo rectángulo,
cuando son conocidos dos lados. La función debe tomar dos argumentos del tipo double que son las
longitudes de los lados y regresar la hipotenusa también como double.
Power
Escriba una función potenciaEnteros(base, exponente) que devuelva el valor baseexponente.
Suponga que exponente es un entero positivo, y base es un entero. La función potenciaEnteros
debe utilizar un ciclo para controlar el cálculo.
Múltiplo
Escriba una función multiplo que determine para un par de números, si el segundo de ellos es
múltiplo del primero. La función debe tomar dos argumentos enteros y regresar 1 (verdadero) si el
segundo es un múltiplo del primero, y 0 (falso) de no ser así. Utilice esta función en un programa
que introduzca una serie de pares enteros.