Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Iterations #include<conio.

h>
//Write a program that asks the user to type an integer and
writes "YOU WIN"
EXERCISE 1 //if the value is between 56 and 78 (both included). In the
other case it writes "YOU LOSE".
Write a program that asks the user to type an integer and writes "YOU WIN" if the value is int main()
between 56 and 78 (both included). In the other case it writes "YOU LOSE". {
int bet;
char option;
Solution start: cout<<"\nEnter you bet: ";
#include<iostream> cin>>bet;
using namespace std; if(bet>=56&&bet<=78)
{
int main() cout<<"\nYou win!";
{ }
int a; else
cout << "Type an integer : "; {
cin >> a; cout<<"\nYou lose!";
if ( (a >= 56) && (a <= 78) ) }
cout << "YOU WIN" << endl; ask: cout<<"\n\nYou want to try your luck again? [Y][N]";
else cin>>option;
cout << "YOU LOSE" << endl; switch(option)
return 0; {
} case 'Y' : goto start;
case 'y' : goto start;
The solution in C. case 'n' : return 0;
case 'N' : return 0;
default: cout<<"\nInvalid Input";
#include <stdio.h> goto ask;
}
int main() getch();
{ return 0;
int a; }
printf("Integer: "); #include <iostream>
scanf("%d", &a); using namespace std;
if((a > 55) && (a < 79))
{ int main()
printf("YOU WIN\n"); {
} int input = 0;
else cin >> input;
{ while(!((input >= 56) && (input <= 78)))
printf("YOU LOSE\n"); {
} cout << "you Loose" << endl;
return 0; cin >> input;
} }
cout << "you Win" << endl;
system("pause");
Solution by Marvin Gabriel }
/*
EXERCISE 1
#include<iostream.h>
Write a program that asks the user to type an integer and #include <iostream>
writes "YOU WIN" using namespace std;
if the value is between 56 and 78 (both included). In the other
case it writes "YOU LOSE". int main()
*/ {
int a = 8;
#include <iostream> int number;
using namespace std; cout << "Enter all the numbers from 8 - 23.\n";
for(; a < 24;)
void main() { {
int num; cin >> number;
char again = 'y'; if(number == a)
while (again == 'y') { {
cout << "Please enter a number from 1 - 100: a++;
"; cout << "Next number:\n";
cin >> num; }
if (num >= 56 && num <= 78) { else
cout << "Congratulations you cout << "?";
win.\n"; }
again = 'n'; return 0;
}else { }
cout << "You lose, try again ?
[y][n]: ";
cin >> again; Solution by Marvin Gabriel
}
} #include<iostream.h>
system("pause"); #include<conio.h>
} //Write a program that asks the user to type all the integers
between 8 and 23
//(both included) using a for loop.
EXERCISE 2
int main()
{
Write a program that asks the user to type all the integers between 8 and 23 (both included) int a,b;
using a for loop. start:
cout<<"\nEnter numbers from 8-23";
cin>>b;
Solution if((b<8)||(b>23))
#include <iostream> {
using namespace std; cout<<"invalid";
goto start;
int main() { }
// Input and loop defined, only adds if input is correct. else
for (short int i=8, input; i <= 23; i+=(i == input) ) { {
cout << "Enter the number " << i << ": "; for(a=8;a<23;a++)
cin >> input; {
} cin>>b;
if(b>23)
return(0); {cout<<"\nExceeds required input";
} goto start;
}
A solution that uses input from users could be
}} //by Faulkner
getch(); #include <iostream>
return 0;
} using std::endl;
using std::cout;
#include <iostream> using std::cin;
using namespace std;
int main()
int main() {
{ int i=0;
int typedInt = 0; int askNum =0;
cout << "Type all numbers between 8 and 23: " << endl;
for(int i = 8; i <= 23; i++) for(int i=8; i <= 23 ; i++)
{ {
cin >> typedInt; goUp:
if (typedInt != i) cout << endl;
{ cout << "Type the any Integer between 8 to
cout << "You Missed the sequence the next number 23 to proceed ; Current Iteration :" << i << endl;
was " << i << endl; cin >> askNum;
}
} if(8 <= askNum && 23 >= askNum)
} {
/*EXERCISE 2 cout << "Typed Number : " << askNum
~asdfk << endl;
Write a program that asks the user to type all the integers }else{
between 8 and 23 (both included) using a for loop. cout << "You typed a wrong number
*/ repeat again" << endl;
goto goUp;
#include <iostream> }
using namespace std;
}
void main() {
int num, next(8);
for (; next < 25;) { cin >> i;
cout << "Please enter a number: "; return 0;
cin >> num; }
if (num == next) {
next++;
}else { EXERCISE 3
cout << "Wrong input, actual
number: " << next << endl; Same exercise but you must use a while.
system("pause");
return;
} Solution
} #include <iostream>
cout << "Congratulations\n"; using namespace std;
system("pause"); int main() {
} short int input, i(8); // Input and loop variables.

while (i <= 23) {


Another Solution cout << "Enter the number " << i << ": ";
cin >> input; return 0;
i += (input == i); // Only add to the loop if input is }
correct #include <iostream>
} using namespace std;

return(0); int main()


} {
int typedInt = 0;
int i = 8;
A solution that uses input from the user could be cout << "Enter the numbers between 8 and 23: " << endl;
while(i <= 23)
#include <iostream> {
using namespace std; cin >> typedInt;
if(typedInt != i)
int main() {
{ cout << "You missed a number in the sequence " << i
int a = 8, number; << endl;
cout << "Enter numbers from 8 to 23\n"; }
while(a < 24) else
{ {
cin >> number; i++;
if(a == number) }
{ }
cout << "Next number: "; }
a++; /*EXERCISE 3
} ~asdfk
else Write a program that asks the user to type all the integers
cout << "?"; between 8 and 23 (both included) using a "while" loop.
} */
return 0;
} #include <iostream>
using namespace std;
Solution by Marvin Gabriel void main() {
int num, next(8);
#include<iostream.h> while (next < 25) {
#include<conio.h> cout << "Please enter a number: ";
//Write a program that asks the user to type all the integers cin >> num;
between 8 and 23 if (num == next) {
//(both included) using while. next++;
int main() }else {
{ cout << "Wrong input, actual
int a,b; number: " << next << endl;
start: system("pause");
cout<<"\nEnter numbers from 8-23"; return;
cin>>b; }
while(b>=8&&b<=23) }
{ cout << "Congratulations\n";
cout<<b; system("pause");
} }
getch();
cin >> temp;
n = n + temp;
}
EXERCISE 4
cout << "\nThe sum of the integers is: " << n << endl;
Write a program that asks the user to type 10 integers and writes the sum of these integers.
return 0;
}
Solution
/* #include<iostream>
Cobin John Bluth Oct 18th 2010 using namespace std;
*/
#include <iostream> int main()
{
using namespace std; int i,s=0,x;
int main() for(i=0;i<10;i++)
{ {
int sum=0; cout<<"Type an integer: ";cin>>x;
int temp=0; s=s+x;
}
cout << "Enter 10 integers and i will write the sum of
them.\n"; cout<<"The sum is : "<<s<<endl;
for (int loop=1; loop<=10; loop++) return 0;
{ }
cout << "Integer #" << loop << ": ";
cin >> temp;
sum += temp;
} The solution in C.

cout << "The sum of all the integers entered is " << sum << #include <stdio.h>
".";
} int main()
/* A program to sum 10 integers using a loop {
Tim Cross 2008*/ int i, num, sum = 0;
#include <iostream> for(i = 0; i<10; i++)
using namespace std; {
printf("Integer: ");
int main() scanf("%d", &num);
{ sum += num;
//Define variables }
int i = 1; //Counter
int n = 0; //Sum printf("The sum is %d\n", sum);
int temp; //Input store return 0;
}
cout << "This program sums ten user entered integers\n\n";

for (i = 1 ; i <= 10 ; i++) { A possible solution using an array.


cout << "Type integer " << i << ": ";
//By Orrill cout << "Sum of all your ten numbers are: " << sum <<
#include <iostream> endl;
using namespace std; system("pause");
}
int main()
{
int numbers[10], total=0; EXERCISE 5
cout << "Please enter 10 integers. " << endl;
for ( int n=0,c=1; n<10; n++ ) { Write a program that asks the user to type 10 integers and writes the smallest value.
cout << c << ". ";
cin >> numbers[n];
c++; Solution
total+=numbers[n]; #include<iostream>
} using namespace std;
cout << endl << "Total = " << total;
return 0; int main()
} {
#include <iostream>
using namespace std; int i,ppt,x;

int main() for(i=0;i<10;i++)


{ {
int countingSum = 0; cout<<"Type an integer: ";cin>>x;
int currentNum = 0; if(i==0)ppt=x;else if(x<ppt)ppt=x;
cout << "Type any 10 numbers and ill add them up for }
you..." << endl;
for(int i = 0; i < 10; i++) cout<<"The lesser value is: "<<ppt<<endl;
{
cin >> currentNum; return 0;
countingSum += currentNum; }
} /*Solution #2
cout << "Total: " << countingSum << endl; by sharpskater80
system("pause"); 9-7-07*/
}
/*EXERCISE 4 #include <iostream>
~asdfk #include <algorithm>
Write a program that asks the user to type 10 integers and #include <vector>
writes the sum of these integers.
*/ using std::cin;
using std::cout;
#include <iostream> using std::vector;
using namespace std;
int main()
void main() {
int num, sum(0); {
for (int i = 1; i < 11; i++) { double input;
cout << "Input number " << i << ": "; vector <double> vec;
cin >> num; cout << "Enter 10 separate integers:\n";
sum += num;
} for ( int i = 0; i < 10; ++i )
{ for(i=5;i<=n;i++)
cin >> input; {
vec.push_back(input); for(j=1;j<=3;j++)
} {
cube=cube*i;
sort(vec.begin(),vec.end());
}
cout << "Smallest value is " << vec[0]; sum=sum+cube;
cube=1;
} }
/*EXERCISE 5
~asdfk cout<<"Sum of cubes from 5 to "<<n<<" = "<<sum<<endl;
Write a program that asks the user to type 10 integers and }
writes the smallest value.
*/ #include<iostream>
using namespace std;
#include <iostream> int main()
using namespace std; {
int N,s=0,i;
void main() {
int num, temp(0); cout<<"Type the value of N : ";cin>>N;
for (int i = 1; i < 11; i++) {
cout << "Input number " << i << ": "; for( i=5 ; i<=N ; i++ )
cin >> num; s=s+i*i*i;
if (i == 1 || num < temp)
temp = num; cout<<"The sum is : "<<s<<endl;
} return 0;
cout << "Smallest number: " << temp << endl; }
system("pause");
}
Here is an example that include numbers greater than 5

#include <iostream>
using namespace std;
EXERCISE 6 int main()
{
int num, sum = 0;
Write a program that asks the user to type an integer N and computes the sum of the cubes cout << "Enter a number from 1-10: "; cin >> num;
from 53 to N3.
if (num < 5)
Solution {
for(; num < 5; num++)
\\By : Taimoor Aslam sum =sum + num * num * num;
#include<iostream.h> }
void main() else if (num > 5)
{ {
int n=0,sum=0,cube=1,j,i; for(; num > 5; num--)
sum = sum + num * num * num;
cout<<"Enter no. = "; }
cin>>n; cout << sum + 125 << endl;
return 0;
} if (N > i) {
for (; i <= N ; i++) {
result += i*i*i;
}
} else if (N < i) {
//another example by using math.h library and pow() function for (; i >= N ; i--) {
#include <iostream> result += i*i*i;
#include <math.h> }
} else {
using std::endl; result = 125;
using std::cout; }
using std::cin; cout << "Result: " << result << endl << "Press any key
to exit...";
getch();
int main() }
{ /* A program that asks the user to type an integer n and
double x(0.0) , y(0.0); computes the sum of the cubes from 5^3 to N^3 in C ~~~ by nour
y = pow(5.0,3.0); */
cout << y << endl; #include <stdio.h>
for(int i=0 ; i<10 ; i++)
{ int main (void) {
cout << "type any value to proceed.\n"; int n; // Number input by user.
cin >> x; int x = 5;
y += pow(x,3.0); int total = 0;
cout << y << endl;
} printf("Input a number: ");
cout << "the final answer is the : " << y << endl; scanf("%i", &n); // The user inputs an integer, note,
system("pause"); it can be greater or less than 5.
return 0;
} /**** We add the if-statement here because we have
three possibilities for the input: the integer could be greater
than 5, less than 5 or equals 5.
And we will deal with each case separately.

Note we can replace > with >= and we wouldn't need the
/* last "else" statement */
EXCERCISE 6 if (n > x) {
~asdfk printf("Total sum of the cubes from %i^3 to
Write a program that asks the user to type an integer N 5^3 = ", n);
and computes the sum of the cubes from 53 to N3. while (n >= x) {
*/ total += n*n*n;
n -= 1;
#include <iostream> }
#include<conio.h> printf("%i\n", total);
}
using namespace std;
else if (n < x) {
void main() { printf("Total sum of the cubes from 5^3 to
int N, i(5), result(0); %i^3 = ", n);
cout << "Number please: "; while (n <= x) {
cin >> N; total += n*n*n;
n += 1;
} }
printf("%i\n", total); int main() {
} int input;
else {
total = 125; while (input!=-1)
printf("Cube of 5 = %i\n", total); {
} cout << "Enter an integer or -1 to Exit:";
} cin >> input;
cout << "U("<< input <<")=" << u(input) <<"
\n";
EXERCISE 7 }
return 0;
Write a program that asks the user to type an integer N and compute u(N) defined with : }
u(0)=3 //A not so good solution by s0ul-child
u(n+1)=3*u(n)+4
#include<iostream>
using namespace std;
Solution
#include<iostream> int n,output;
using namespace std;
//function u(x)
int main() int u(int n)
{ {
int i,u=3,N; int ans,i;
ans =3;
cout<<"Type the value of N : ";cin>>N; for(i=1;i<=n;i++)
{
for(i=0;i<N;i++) ans = 3*ans+4;
u=u*3+4; }
return ans;
cout<<"u("<<N<<")="<<u<<endl;
}
return 0;
} int main()
//Another program writen by Ahmed Zamouche {
#include <iostream> cin>>n;
cin.ignore();
cout<<u(n);
using namespace std; cin.get();
}
int u(int n, int i=0, int sum = 3){
EXERCISE 8
if (n==i || n==0)
{ Write a program that asks the user to type an integer N and compute u(N) defined with :
return sum; u(0)=1
}
u(1)=1
sum = 3 * sum + 4;
u(n+1)=u(n)+u(n-1)
i++;
u(n,i,sum);
Solution //not so good solution by s0ul-child
#include<iostream>
using namespace std; #include<iostream>
using namespace std;
int main()
{ int n;
int i,u=1,v=1,w,N;
int u(int n)
cout<<"Type the value of N : ";cin>>N; {
int fibo[n+1];
w=1; int i,final;
final=1;
for(i=2;i<=N;i++) fibo[0]=1;
{ fibo[1]=1;
w=u+v; for(i=2;i<=n;i++)
u=v; {
v=w; fibo[i]=fibo[i-1]+fibo[i-2];
} final=fibo[i];
}
cout<<"u("<<N<<")="<<w<<endl; return final;
}
return 0;
} int main()
{
#include <iostream> cin>>n;
cin.ignore();
using namespace std; cout<<u(n);
cin.get();
extern int u(int); }

int u(int N)
{ EXERCISE 9
if(N==0) return 1;
else 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
if(N==1) return 1; another value.
else return u(N-1)+u(N-2);
}
} Solution
#include<iostream>
int main() using namespace std;
{
int n; int main()
{
cout << "Insert N: "; cin >> n; int N;
cout << "Result: " << u(n) << endl; bool ok;

system("PAUSE"); do
return 0; {
} cout<<"Type the value of N between 0 et 20 :";cin>>N;
ok= N<=20 && N>=0;
if(!ok)cout<<"ERROR"<<endl; (both included) and writes N+17. If someone types a wrong
}while(!ok); value,
the program writes ERROR and he must type another value.
N=N+17; */
cout<<"The final value is : "<<N<<endl;
#include <iostream>
return 0; #include <conio.h>
}
using namespace std;
The solution in C. void main() {
int num;
#include <stdio.h> entry:
cout << "Please enter a number: ";
int main() cin >> num;
{ while (num < 8 || num > 20) {
int N; cout << "Error\n";
goto entry;
do }
{ cout << "N+17";
printf("Integer between 0 and 20: "); getch();
scanf("%d", &N); }
if((N < 0) || (N > 20))
printf("ERROR\n");
EXERCISE 10
} while((N < 0) || (N > 20));

printf("The final value is %d\n", N+17); Write a program that is able to compute some operations on an integer. The program writes
return 0; the value of the integer and writes the following menu :
}
#include <iostream>
using namespace std; 1. Add 1
2. Multiply by 2
int main() 3. Subtract 4
{ 4. Quit
int userIn;
cout << "Enter a number between 0 and 20: " << endl; The programs ask the user to type a value between 1 and 4. If the user types a value from 1 to 3
cin >> userIn; the operation is computed, the integer is written and the menu is displayed again. If the user
while(!((userIn >= 0) && (userIn <= 20))) types 4, the program quits.
{
cout << "Wrong Input!" << endl;
cin >> userIn; Solution
} #include<iostream>
cout << "N+17 = " << (userIn + 17) << endl; using namespace std;
system("pause");
} int main()
/* {
EXERCISE 9 int x=0,choice;
~asdfk
Write a program that asks the user to type an integer between 0 do
and 20 {
cout<<"The value of x is "<<x<<endl;
cout<<"1 : Add 1"<<endl;
cout<<"2 : Multiply by 2"<<endl; system("PAUSE");
cout<<"3 : Subtract 4"<<endl; return 0;
cout<<"4 : Quit"<<endl;
cout<<"Your choice : ";cin>>choice; }

switch(choice)
{ Another solution
case 1 : x++;break;
case 2: x=x*2; break;
case 3: x=x-4;break; #include <iostream>
} using namespace std;
}while(choice!=4);
int main ()
cout<<"The final value of x is : "<<x<<endl; {
cout<<"Enter an integer: ";
return 0; int N, n;
} cin>>N;
for(n=0; n>=0; n++)
{
Alternative Solution
cout<<"\n";
cout<<"Now please choose from the following:\n";
cout<<"1. Add 1 \n";
#include<iostream> cout<<"2. Multiply by 2 \n";
using namespace std; cout<<"3. Subtract 4 \n";
cout<<"4. Quit \n\n";
int main() cout<<"Selection? ";
{
int number, processNumber; int selection;
cin>>selection;
cout<<"Enter a number: ";
cin>>number; if (selection==1)
{
cout<<"What would you like to do to your number? \n"; N=N+1;
cout<<" 1. Add 1 \n 2. Multiply by 2 \n 3. Subtract 4 \n 4. cout<<"\nThe current value is: "<<N<<"\n";
Quit \n \n"; }
cin>>processNumber;
else if (selection==2)
switch (processNumber) {
{ N=2*N;
case 1: cout<<"You chose to add: " << number + 1 <<endl; cout<<"\nThe current value is:: "<<N<<"\n";
break; }
case 2: cout<<"You chose to multiply: " << number*2 <<endl;
break; else if (selection==3)
case 3: cout<<"You chose to subtract: " << number-4 <<endl; {
break; N=N-4;
case 4: exit(1); cout<<"\nThe current value is:: "<<N<<"\n";
break; }
}
else if (selection==4)
{n=-5;
cout<<"Goodbye"; << "2. "<<n<< " * 2\n"
} << "3. "<<n<< " - 4\n"
else << "4. Quit\n\n"
cout<<"\nPlease enter a valid selection\n"; << "Your option is:";
} cin >> i;
switch (i)
return 0; {
} case 1 : b=n+1;break;
case 2 : b=n*2;break;
case 3 : b=n-4;break;
Another solution }
if (i!= 4)
// Solution {
#include <iostream> cout << "result: " << b<<endl;
using namespace std; system("pause");
system("cls");
int main () }
{
int a, b=5; }
cout << "The number is 5" << endl; return 0;
cout << "1. Add 1" << endl << "2. Multiply by 2" << endl << }
"3. Subtract 4" << endl << "4. Quit" << endl; #include <iostream>
cin >> a; using namespace std;
if (a==1){
b=b+1; int addone(int toAdd)
cout << "The result is " << b; {
} toAdd = toAdd + 1;
else if (a==2) { return toAdd;
b=b*2; }
cout << "The result is " << b;
} int multiplyByTwo(int toMultiply)
else if (a==3) { {
b=b-4; toMultiply = toMultiply * 2;
cout << "The result is " << b; } return toMultiply;
else if (a==4) { }
b=5
return 0; } int subtractFour(int toSubtract)
} {
toSubtract = toSubtract - 4;
return toSubtract;
Another solution: }

#include <iostream> int main()


using namespace std; {
int main() int numberToOperateOn = 0;
{ int selectedOption = 0;
int n,i=0,b=0; cout << "Enter a number to operate on: " << endl;
cout << "Input number n: "; cin>>n; cin >> numberToOperateOn;
while (i!=4) while(selectedOption != 4)
{ {
cout << "1. "<<n<< " + 1\n" selectedOption = 0;
cout << "1. Add By 1" << endl << "2. Multiply By 2" cin >> num;
<< endl; switch (choice) {
cout << "3. Subtract By 4" << endl << "4. Quit" << case 1:
endl; sum = num + 1;
cin >> selectedOption; break;
if(selectedOption == 1){numberToOperateOn = case 2:
addone(numberToOperateOn);} sum = num * 2;
else if(selectedOption == 2){numberToOperateOn = break;
multiplyByTwo(numberToOperateOn);} case 3:
else if(selectedOption == 3){numberToOperateOn = sum = num - 4;
subtractFour(numberToOperateOn);} break;
cout << "Result: " << numberToOperateOn << endl; default:
} cout << "Wrong input du ma !";
} getch();
/* return;
EXERCISE 10 }
~asdfk cout << "Result: " << sum << ", Try again ? [Y] [N]: "
Write a program that is able to compute some operations on an << endl;
integer. cin >> again;
The program writes the value of the integer and writes the }
following menu : }
1. Add 1
2. Multiply by 2
3. Subtract 4 Another solution that opens a file to show the menu
4. Quit
#include <iostream>
The programs ask the user to type a value between 1 and 4. #include <vector>
If the user types a value from 1 to 3 the operation is #include <string>
computed, #include <fstream>
the integer is written and the menu is displayed again. If the using namespace std;
user types 4, the program quits. vector<string> readAllLines(string const& filename)
*/ {
vector<string> lines;
#include <iostream> ifstream ifs(filename.c_str());
#include <conio.h> while (ifs)
{
using namespace std; string line;
getline(ifs, line);
void main() { if (ifs) lines.push_back(line);
int num, choice, sum; }
char again = 'y'; return lines;
cout << "1. Add 1" << endl << }
"2. Multiply by 2" << endl << int main()
"3. Subtract by 4" << endl << {
"4. Quit\n"; int x;
while (again == 'y' || again == 'Y') { int y;
cout << "Choice: "; int z;
cin >> choice; cout << "Please enter a number: ";
if (choice == 4) cin >> x;
return; vector<string> lines = readAllLines("Menu.txt");
cout << "Please enter a number: ";
copy(lines.begin(), lines.end(), {
ostream_iterator<string>(cout, "\n")); case 1 : output = inputVal + 1;
cin >> y; cout << endl << " The New value is
if (y == 1) " << output;
{ break;
z = x + 1; case 2 : output = inputVal * 2;
cout << "The answer is: " << z << endl; cout << endl << " The New value is
} " << output;
else if (y == 2) break;
{ case 3 : output = inputVal - 4;
z = x * 2; cout << endl << " The New value is
cout << "The answer is: " << z << endl; " << output;
} break;
else if (y == 3) case 4 : break;
{ }
z = x - 4;
cout << "The answer is: " << z << endl; cout << endl << endl << "Press enter to continue . .
} .";
else if (y == 4)
{ cin.get();
return 0; cin.ignore();
}
else }
{
cout << "Uhhh, that number was not listed as
a choice" << endl;
}
} EXERCISE 11
Another Alternate ->

// Lyndon Malan Z.A Write a program that asks the user to type a positive integer. When the user types a negative
#include "stdafx.h" value the program writes ERROR and asks for another value. When the user types 0, that
#include <iostream> means that the last value has been typed and the program must write the average of the
using namespace std; positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'.
void main()
{ Solution
int selectedOption; // EXERCISE 11 - Solution 1
double inputVal;
double output; #include<iostream>
using namespace std;
cout << "*Enter a Number you wish to use" << endl << "
"; int main()
cin >> inputVal; {
int x, s=0,nb=0;
cout << "*Please select ONE Option from the list double average;
below" << endl;
cout << " 1. Add 1 " << endl << " 2. Multiply by do{
2" << endl << " 3. Subtract 4" << endl << " "; cout<<"Type an integer:";cin>>x;
cin >> selectedOption; if(x>0){s=s+x;nb++;}
else if(x<0)cout<<"ERROR ";
switch (selectedOption)
}while(x!=0);
int main()
if(nb==0)cout<<"NO AVERAGE"<<endl; {
else { vector<int> numList;
average=(double)s/nb; vector<int>::const_iterator iter;
cout<<"The average is : "<<average<<endl;
} int num, total(0), average(0);

return 0; do
} {
cout << "Enter a whole number to add to list, '0' to
get average.";
// EXERCISE 11 - Solution 2 cin >> num;

#include <iostream> if (num < 0)


#include <conio.h> cout << "ERROR\n";
else if (num > 0)
using namespace std; numList.push_back(num);
else;
void main() { } while (num != 0);
int num, i(0);
double sum(0); for (iter = numList.begin(); iter != numList.end(); iter++)
cout << "Type a number, type 0 to stop: "; total += *iter;
cin >> num;
while (num != 0) { if (total == 0)
cout << "Number " << i + 2 << ": "; cout << "NO AVERAGE\n";
cin >> num;
if (num < 0) { average = (total / numList.size());
cout << "ERROR"; cout << "Average is " << average;
getch();
return; return 0;
} else if (num != 0) { }
sum += num;
i++;
}
}
if (sum != 0) EXERCISE 12
cout << "Average: " << sum / i;
else
cout << "No average"; Write a program that asks the user to type an integer N and compute u(N) defined with :
getch(); u(0)=3
} u(1)=2
u(n)=n*u(n-1)+(n+1)*u(n-2)+n

// EXERCISE 11 - Solution 3 Solution


#include<iostream>
#include <iostream>
#include <vector> int main()
#include <cstdlib> {
int N,u,i=0,v,w;
using namespace std;
cout<<"Type the value of N : ";cin>>N; /*
u=3; EXERCISE 13
v=2; ~asdfk
if(N==0)w=u; Write a program that ask the user to type 10 integers and write
else if(N==1)w=v; the number of occurrence of the biggest value.
else for(i=2;i<=N;i++){w=i*v+(i+1)*u+i;u=v;v=w;} */
#include <iostream>
cout<<"u("<<N<<")="<<w<<endl; #include <conio.h>

return 0; using namespace std;


}
void main() {
int number, biggest(0), occur(0);
for (int i = 1; i < 11 ; i++) {
cout << "Input " << i << ": ";
cin >>number;
if (number > biggest){
biggest = number;
EXERCISE 13 occur = 0;
}
Write a program that asks the user to type 10 integers and write the number of occurrence of if (biggest == number)
the biggest value. occur++;
}
cout << "Biggest number inputted was: " << biggest << "
Solution \nOccurance: " << occur << endl;
\\By : Taimoor Aslam system("pause");
#include<iostream.h> getch();
void main() }
{
int i,a=0,n=0,occur=0;

cout<<"Enter 10 numers\n"; EXERCISE 14

for(i=1;i<=10;i++) Write a program that asks the user to type the value of N and computes N! .
{
cin>>n;
Solution
if(i==1 || n>a) #include<iostream>
{ using namespace std;
a=n;
occur=0; int main()
} {
int N,i,f=1;
if(a==n)
occur++; cout<<"Type the value of N : ";cin>>N;
} for(i=2;i<=N;i++)f=f*i;
cout<<N<<"! is "<<f<<endl;
cout<<"\noccurance of biggest no. "<<a<<" =
"<<occur<<endl; return 0;
} }
/*
EXERCISE 14 Example 1:
~asdfk
Write a program that asks the user to type the value of N and
#include <iostream>
computes N! .
#include <conio.h>
*/
using namespace std;
#include <iostream>
int main()
#include <conio.h>
{
bool prime(true);
using namespace std;
cout << "Enter a number: ";
void main() {
cin >> input;
int num, sum(1);
cout << "Enter a number: ";
for (int i = 2, input; i < input/2 && prime; i++)
cin >> num;
prime = !(input%i == 0);
for (int i = 1; i != num ; i++) {
sum += sum * i;
cout << input << " is " << (prime ? "" : "not ") <<
}
"prime.";
cout << num << "! = " << sum;
getch();
getch();
}
return(0);
}
#include<iostream>
using namespace std;
int factorial(int n); Example 2:
int main()
{
int n; #include<iostream>
cout<<"Input n: "; using namespace std;
cin>>n;
cout<<"Factorial of n is :"<<factorial(n); int main()
return 0; {
} int n;
int factorial(int n) bool prime=true;
{ cout<<"Write a number: ";
if(n==0) cin>>n;
return 1;
else if (n==1) for (int i=n-1;i>1 && prime;i--){
return 1; if(n%i==0) prime=false;
else }
return n*factorial(n-1); if (n==2) prime=false;
}
cout<<"Is prime: "<<boolalpha<<prime<<endl;

EXERCISE 15 return 0;
}
Write a program that asks the user to type an integer N and that indicates if N is a prime
number or not. Example 3:

Solution #include <iostream>


using namespace std; else
cout << "Not prime number";
int main() getch();
{ }
int N,i;
bool prime;
do{
cout << "input an integer: ";
cin >> N; EXERCISE 16
} while (N<=0);
if (N==2)
cout << "it is a prime!" << endl; Write a program that asks the user to type an integer N and that writes the number of prime
else { numbers lesser or equal to N.
for (i=2;i<N;i++){
if (N%i==0){ Solution
cout << "it is not a prime!" << #include<iostream>
endl;prime=false;break;} using namespace std;
else
{prime=true;} int main()
} {
if (prime==true) int N,i,nb=0,d;
cout << "it is a prime!" << endl;} bool is_prime;
return 0;
} cout<<"Typer the value of N : ";cin>>N;

Example 4: for(i=2;i<=N;i++)
{
/* test if i is prime*/
/* is_prime=true;
EXERCISE 15 d=2;
~asdfk while(is_prime && d*d<=i)
Write a program that asks the user to type an integer N if(i%d==0)is_prime=false; else d++;
and that indicates if N is a prime number or not.
*/ if(is_prime==true)nb++;
}
#include <iostream>
#include <conio.h> cout<<"The number of prime number lesser or equal to "
<<N<<" is "<<nb<<endl;
using namespace std;
return 0;
void main() { }
int num, i; /* ex 16:
bool prime = true; Write a program that asks the user to type an integer N and
cout << "Enter a number: "; that writes the number
cin >> num; of prime numbers lesser or equal to N.
for (i = num-1; i > 1 && prime == true; i--) {
if (num%i == 0)
prime = false; #include <iostream>
} using namespace std;
if (prime == true)
cout << "Prime number"; int x;
EXERCISE 18
int main () {
cout << "Please enter a number and I will show you all
prime numbers between that number and zero.\n"; Write a program that asks the user to type the value of N and writes this picture :
cin >> x;
for ( int i = x; i > 1; --i) { N=1
if (i%2 != 0 && i%3 != 0 && i%5 != 0 && *
i%7!=0) N=2
cout << i << ", "; **
} *
cout << "7, 5, 3, 2, and 1 are all prime numbers N=3
between 1 and " << x << "."; ***
} **
*

and so on...

EXERCISE 17 [show ▼]Solution

Write a program that asks the user to type the value of an integer N and compute the N-st
prime number.
EXERCISE 19
Solution
#include<iostream>
using namespace std; Write a program that asks the user to type the value of N and display an output like the
following:
int main()
{ N=1
int N,i=1,nb=0,d; *
bool is_prime; N=2
**
cout<<"Type the value of N : ";cin>>N; *
N=3
while(nb<N) ***
{ **
i++; *
is_prime=true;
d=2;
while(is_prime && d*d<=i) and so on ...
if(i%d==0)is_prime=false; else d++;
Solution
if(is_prime==true)nb++; #include<iostream>
} using namespace std;
cout<<"Le N-st prime number is "<<i<<endl; int main()
{
return 0; int i,j,N;
}
cout<<"Type the value of N : ";cin>>N;
}
for(i=1; i<=N; i++)
{
for (j=1; j<i; j++)
cout<<" "; b) Write a program that asks the user to type a value M and computes the value of a from 2
for (j=1; j<=N+1-i; j++) and M that maximizes the value of N. This value is called A. The program must write the value
cout<<"*"; of A and N.
cout << endl;
} Solution
return 0; #include<iostream>
} using namespace std;

int main()
{
int a,n,u,M,amax,nmax;
cout<<"Type the value of M : ";cin>>M;
amax=2;
nmax=2;
EXERCISE 20
for(a=3;a<=M;a++)
u(n) is defined with: {
n=0;
u=a;
• u(0) = a (a is an integer) while(u!=1)
• if u(n) is even then u(n+1)=u(n)/2, else u(n+1)=3*u(n)+1 {
if(u%2==0)u=u/2; else u=3*u+1;
n++;
Conjecture: For all value of a, there exists a value N such that u(N)=1. }
if(n>nmax){amax=a;nmax=n;}
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. cout<<"The value of A is :"<<amax<<endl;
cout<<"The value of N is :"<<nmax<<endl;

Solution return 0;
#include<iostream> }
using namespace std;

int main() EXERCISE 21


{
int a,n,u; Request the user to type numbers, each time printing its triple, until the user enters -999.
cout<<"Type the value of a : ";cin>>a;
n=0;
u=a; Solution

while(u!=1) Example 1:
{
if(u%2==0)u=u/2; else u=3*u+1;
n++; #include <iostream>
cout<<"u("<<n<<")="<<u<<endl; using namespace std;
}
return 0; int main() {
for (int input; input != -999;) { posCount++;
cout << "Enter a number (-999 to quit): "; } while (input > 0);
cin >> input;
if (input != -999) cout << "Total number of positives: " << posCount;
cout << "Tripled: " << input * 3 << endl; getch();
}
return(0);
return(0); }
}

Example 2:
EXERCISE 23
#include <iostream>
using namespace std;
Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print
int main() { the average.
int input;
[hide ▲]Solution
cout << "Enter a number (-999 to quit): "; #include <iostream>
cin >> input; #include <conio.h>

while (input != -999) { using namespace std;


cout << "Tripled: " << input * 3 << endl;
cout << "Enter a number (-999 to quit): "; int main()
cin >> input; {
} int divisor(0);
double input, sum(0);
return(0);
} do {
cout << "Enter a positive number (less than 1 to quit):
";
EXERCISE 22
cin >> input;
divisor += (input > 0); // Only adds if number doesn't
Request the user to type positive numbers until either a zero or a negative is typed, and then quit the program
show the user how many positives were typed in. sum += input;
} while (input >= 1);
Solution cout<< "The average was: " << sum/divisor;
#include <iostream>
getch();
#include <conio.h>
return(0);
using namespace std;
}
int main() {
int posCount(-1), input; EXERCISE 24

do {
cout << "Enter a positive number (0 or less to quit): Request the user to type numbers, or type 0 to stop. Show how many numbers were between
"; 100 and 200 (both included).
cin >> input;
Solution years++;
#include <iostream> }
#include <conio.h>
int main() cout << "It would take " << years << " years for country A
{ to surpass country B.";
int input, numberBetween(0); getch();
const short int minNum(100), maxNum(200);
do { return(0);
cout << "Enter a number (0 to quit): "; }
cin >> input;
if (input >= minNum && input <= maxNum)
numberBetween++;
Alternative Solution:
} while (input != 0);

cout << "Numbers between " << minNum << " and " << maxNum #include <iostream>
<< ": " << numberBetween;
getch(); int main()
{
return(0); int A = 50000000;
} signed int B = 70000000;
int i = 0;
for (; B > A; i++) {
A = A * 1.03;
B = B * 1.02;
EXERCISE 25 }
std::cout << i << " years.";
std::cin.get();
The country A has 50M inhabitants, and its population grows 3% per year. The country B, return 0;
70M and grows 2% per year. Tell in how many years A will surpass B. }

Solution
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int countryA(50000000), countryB(70000000);
const float aRate(1.03), bRate(1.02);
short int years(0);

cout << "With a population of " << countryA << " and a
growth rate of " << aRate*100-100 << "% for country A...\n"
<< "And a population of " << countryB << " and a
growth rate of " << bRate*100-100 << "% for country B...\n";

while (countryA <= countryB)


{
countryA *= aRate;
countryB *= bRate;

You might also like