# C++ Programming/Exercises/Iterations/Pages

Solutions must:
* Use only standard C++.
* Be compilable.
* Be in accordance to general coding practices. (no esoteric demonstrations are required)
and should:
* 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).
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.

## 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".

## Exercise 2

Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop.
Alternative solution
Alternate solution
Solution in C
Another alternate solution
This program fails to comply fully with the requirements. (please correct this as an extra exercise)

## Exercise 3

Same as Exercise 2 but you must use a while.
//Judy Mok sigurrosist@gmail.com //C++ Exercise 20161103 //Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop.
* include "stdafx.h"
* include <iostream>
using namespace std; int main {int number = 8, correct = 8; while (correct <=23) { cout << "Enter the number " << number << " : " ; cin >> number; while (number != correct) {cout << "Enter the number " << correct << " again : " ; cin >> number;} if (number == correct) {number = 1 + number; correct = 1 + correct;} } cout << "Good job" <<endl; system ("pause");
// By : Sardor Karimov
* include <iostream>
using namespace std;
int main( ) {
//Made by: Reiz Troy D. Durante
Alternative solutions
A solution that uses input from the user could be
An easy solution :) By Francis Percival D. Wisco—Kaykiks<3

## Exercise 4

Write a program that asks the user to type 10 integers and writes the sum of these integers.
//Made by: Reiz Troy D. Durante
Alternative solution by Bartosz Radwanski
The solution in C.
A possible solution using an array.
A possible solution using a vector.
< source lang = "cpp"> \\ By metalhands
* include "stdafx.h"
* include <iostream>
void main{ using namespace std; int i = 1; int sum = 0; while ( i <=10 ){ cout << "Enter number position " << i << ": "; int y; cin >> y; sum = sum + y; i++;
} cout << "Final sum is: " << sum << endl; } </syntaxhighlight>
Author: jimmymass;

## Exercise 5

Write a program that asks the user to type 10 integers and writes the smallest value.
Solution in C

## Exercise 6

Write a program that asks the user to type an integer N and computes the sum of the cubes from 5$^3$ to N$^3$.
Alternative solutions
Solution #2
Solution #3 Here is an example that include numbers greater than 5
Solution #4
Solution #5
Solution #6
Solution #7
Solution #8
Solution in C
Solution #9
Solution #10
Solution #11
Solution #12
Solution #13
Solution #14
Solution #15

## 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 by Matthewkos - compatible with positive and negative N
Alternative solutions
Solution
Solution #2
Solution #3
</syntaxhighlight>

## 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 #1
Solution by Matthewkos - compatible with positive and negative N
Alternative solutions
Solution #2
Solution #3
Solution #4
Solution #5

## 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.
Made by: Reiz Troy D. Durante
The solution in C.

## 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.

## 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'.
Made by: Reiz Troy D. Durante
//Solution by Matthewkos
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 by Matthewkos
Solution #1
Solution #2

## Exercise 13

Write a program that asks the user to type 10 integers and write the number of occurrence of the biggest value.
Made by: Reiz Troy D. Durante
//Solution by Matthewkos

## Exercise 14

Write a program that asks the user to type the value of N and computes N! .
//Made by Reiz Troy D. Durante
//Solution by Matthewkos
Alternate Solutions

## 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.
// Made by: Engineer Reiz Troy D. Durante Please visit my site: ccphplusplus.blogspot.com
//Solution by Matthewkos
Example 2:
Example 3:
Example 4:
Example 5:
Solution #6
Example 7
Solution #8
Solution #9 with recursive method

## 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.
//Made by: Reiz Troy D. Durante
//Solution by Matthewkos
Alternate Solutions
Example 3:
Solution #4
Solution #5

### 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 by Matthewkos
// solution by Cyrex1337
Alternate Solutions

### Exercise 18

Write a program that asks the user to type the value of N and writes this picture :
and so on...
// Easy method using for loop
* include <iostream>
* include <cmath>
using namespace std;
int main { int n;
cin >> n;
for (int i = n; i >= 1; i--) { for (int k = 1; k <= i; k++) cout << "*"; cout << endl; }
system("pause"); return 0; }
//Made by: Reiz Troy D. Durante
//Solution by Matthewkos
Alternate Solutions
The solution in C.
A recursive solution

## Exercise 19

Write a program that asks the user to type the value of N and display an output like the following:
and so on ...
//Solution by Matthewkos
Solution #1
Solution #2
Solution #3
Solution #4
Solution #5
Solution #6 - Using a matrix. By David Castillo (decaalv@gmail.com)
Solution #7 - Using allignment to the right. by Anboias.

## 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.
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.

## Exercise 21

Request the user to type numbers, each time printing its triple, until the user enters -999.
Example 1:
Example 2:
Example 3:

## 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.

## Exercise 23

Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print the average.

## Exercise 24

Request the user to type numbers, or type 0 to stop. Show how many numbers were between 100 and 200 (both included).

## 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.
Alternative Simple Solution:
Alternative Solution:
Yet another alternative Simple Solution:

## Exercise 26

Write a program that asks the user to input 5 sequences of characters. Then it will ask the user for a character to search for and will output the maximum number of times that it occurred between the 5 sequences. Example: Sequence 1: aabc Sequence 2: aaaa If the user chooses to search for character 'a', the program will output "Character a occurred a maximum of 4 times" because it occurred 4 times in the second sequence, and only twice in the first sequence.
Alternative, optimized solution
Class based solution.

## Exercise 27

Write a program that asks the user to type a random number. Write in the output if this number is a prime number or not. Write which numbers your number can be divided by.

## Exercise 28

Write a program for finding the amount of a specific character inside a string.

## Exercise 29

Write a Program to find out no. of Even & Odd no. in a given Data Series.

## Exercise 30

Write a Program to count Zero, Positive, Negative no. in a given Data Series.

## Exercise 31

Write a program to count the numbers which are divisible by 3 in a given Data Series.
Simple alternative solution: