C++ Wiki
C++ Wiki
C++ Wiki
Use only standard C++. Be compilable. Be in accordance to general coding practices. (no esoteric demonstrations are required) Handle error situations, even if behavior is not defined. Acceptable default is simply reporting an "Error". Be internally consistent in relation to the adopted coding-style. Be as simple as possible on to the point (use the minimum required variables and function calls/classes and reduces convoluted I/O handling).
and should:
Please do not add solutions that are 99% similar to another that is already present, if it is an improvement just add it to the existing solution. Note: Revisers are not required to provide commentary on the errors only to tag the exercise as failing the requested function. Use the talk page to clarify any issues or help submitters if the issue are purely logical, report to the book text otherwise. Fixing failed attempts should be proposed as an extended exercise whenever possible.
EXERCISE 1
Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". Solution
EXERCISE 2
Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. Solution
EXERCISE 3
Same exercise but you must use a while.
Solution
EXERCISE 4
Write a program that asks the user to type 10 integers and writes the sum of these integers. Solution
EXERCISE 5
Write a program that asks the user to type 10 integers and writes the smallest value. Solution
EXERCISE 6
Write a program that asks the user to type an integer N and computes the sum of the cubes from 53 to N3.
Solution
EXERCISE 7
Write a program that asks the user to type an integer N and compute u(N) defined with : u(0)=3 u(n+1)=3*u(n)+4 Solution
EXERCISE 8
Write a program that asks the user to type an integer N and compute u(N) defined with : u(0)=1 u(1)=1 u(n+1)=u(n)+u(n-1) Solution
EXERCISE 9
Write a program that asks the user to type an integer between 0 and 20 (both included) and writes N+17. If someone types a wrong value, the program writes ERROR and he must type another value.
Solution
EXERCISE 10
Write a program that is able to compute some operations on an integer. The program writes the value of the integer and writes the following menu : 1. Add 1 2. Multiply by 2 3. Subtract 4 4. Quit The programs ask the user to type a value between 1 and 4. If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is displayed again. If the user types 4, the program quits. Solution
EXERCISE 11
Write a program that asks the user to type a positive integer. When the user types a negative value the program writes ERROR and asks for another value. When the user types 0, that means that the last value has been typed and the program must write the average of the positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'. Solution
EXERCISE 12
Write a program that asks the user to type an integer N and compute u(N) defined with : u(0)=3 u(1)=2 u(n)=n*u(n-1)+(n+1)*u(n-2)+n Solution
EXERCISE 13
Write a program that asks the user to type 10 integers and write the number of occurrence of the biggest value. Solution
EXERCISE 14
Write a program that asks the user to type the value of N and computes N! .
Solution
EXERCISE 15
Write a program that asks the user to type an integer N and that indicates if N is a prime number or not. Solution
EXERCISE 16
Write a program that asks the user to type an integer N and that writes the number of prime numbers lesser or equal to N. Solution
EXERCISE 17
Write a program that asks the user to type the value of an integer N and compute the N-st prime number. Solution
EXERCISE 18
Write a program that asks the user to type the value of N and writes this picture :
N=1 * N=2 ** * N=3 *** ** *
EXERCISE 19
Write a program that asks the user to type the value of N and display an output like the following:
N=1 * N=2 ** *
N=3 *** ** *
EXERCISE 20
u(n) is defined with: u(0) = a (a is an integer) if u(n) is even then u(n+1)=u(n)/2, else u(n+1)=3*u(n)+1
Conjecture: For all value of a, there exists a value N such that u(N)=1. a)Write a program that asks the user to type the value of an integer a and writes all the values of u(n) from n=1 to n=N.
Solution b) Write a program that asks the user to type a value M and computes the value of a from 2 and M that maximizes the value of N. This value is called A. The program must write the value of A and N. Solution
EXERCISE 21
Request the user to type numbers, each time printing its triple, until the user enters -999. Solution
EXERCISE 22
Request the user to type positive numbers until either a zero or a negative is typed, and then show the user how many positives were typed in. Solution
EXERCISE 23
Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print the average.
Solution
EXERCISE 24
Request the user to type numbers, or type 0 to stop. Show how many numbers were between 100 and 200 (both included). Solution
EXERCISE 25
The country A has 50M inhabitants, and its population grows 3% per year. The country B, 70M and grows 2% per year. Tell in how many years A will surpass B. Solution