C++ 25 Exercise
C++ 25 Exercise
C++ 25 Exercise
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.
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";
#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: }
// 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;
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:
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...
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;
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>
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>
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";