Application concern Selection (If, If-else, switch) structures
Example 1:
/* C++ program for computing the roots of second degree equation*/
#include<iostream>
#include<cmath> // for using the function sqrt
using namespace std;
int main()
{ int a,b,c;
double x_1,x_2,discriminant;
cout<<"the value of coefficient a is\t";
cin>>a;
cout<<"the value of coefficient b is\t";
cin>>b;
cout<<"the value of coefficient c is\t";
cin>>c;
cout<<"\n";
discriminant=(pow(b,2))-(4*a*c);
if (discriminant>0)
x_1=(-b-sqrt(discriminant))/(2*a);
x_2=(-b+sqrt(discriminant))/(2*a);
cout<<"the roots of your equation are : "<<x_1<<"\t and \t"<<x_2<<endl;
}
else
if(discriminant==0)
x_1=(-b)/(2*a);
cout<<"You have only one root of order two is\t"<<x_1<<endl;
else
cout<<"No solutions in the set of real numbers."<<endl;
return 0;
Example2:
#include <iostream>
using namespace std;
int main ()
int x = 1; // local variable declaration:
switch (x) {
case 1 :
cout << "Hi!" << endl;
break;
default :
cout << "Hello!" << endl;
return 0;
}
Example3:
/*C++ program to print if your number is odd or even*/
#include<iostream>
using namespace std ;
int main()
int value;
cout<<"Enter a number ";
cin>>value;
switch ( value % 2 )
case 0:
cout<< "Even integer\n" ;break;
default: cout<< "Odd integer\n" ;
return 0;
Example4:
#include<iostream>
using namespace std;
int main()
char x='b';
switch(x)
{
case 'a': cout<<"case a"<<endl; break;
case 'b': cout<<"case b"<<endl; break;
default:
cout<<"wrong input"<<endl;
return 0;
Example5:
#include<iostream>
using namespace std;
int main()
int day;
cout<<"Enter the day number\n";
cin>>day;
switch(day)
case 1: cout<<"The day is Sunday\n";
break;
case 2: cout<<"The day is Monday\n";
break;
case 3: cout<<"The day is Tuesday\n";
break;
case 4: cout<<"The day is Wednesday\n";
break;
case 5: cout<<"The day is Thursday\n";
break;
case 6: cout<<"The day is Friday\n";
break;
case 7: cout<<"The day is Saturday\n";
default: cout<<"Wrong day number, try again\n";
return 0;
Example6:
#include<iostream>
using namespace std;
int main()
char sign ;
int a,b;
cout<<"Enter the first integer number a=\n";
cin>>a;
cout<<"Enter the second integer number b=\n";
cin>>b;
cout<<"Please, choose an operation\n";
cin>>sign;
switch(sign)
{
case '+': cout<<"The sum of "<<a<<"+"<<b<<"="<<a+b<<endl;
break;
case '-': cout<<"The substraction is\t"<<a-b<<endl;
break;
case '*': cout<<"The multiplication is\t "<<a*b<<endl;
break;
case '%': cout<<"The remainder is \t"<<a%b<<endl;
default: cout<<"You give a wrong operation, try again\n";
return 0 ;
Application concern repetition (while, for, do-while) structures
Example1:
// Summation of even numbers from 2 to 100 with for loop.
#include <iostream>
using namespace std;
// function main begins program execution
int main()
int sum = 0; // initialize sum
// sum even integers from 2 through 100
for ( int number = 2; number <= 100; number += 2 )
sum += number; // add number to sum
cout << "Sum is " << sum << endl; // output sum
return 0; // successful termination
} // end function main
Example2:
/*C++ program to print the factorial of a positive integer number*/
#include<iostream>
using namespace std;
int main()
int i,n;
float fact=1;
cout<<"Enter an integer n= ";
cin>>n;
for(i=1;i<=n;i++)
fact=fact*i;
cout<<"factorial of \t"<<n<<"\t is "<<n<<"! = " <<fact<<endl;
return 0;
Example3:
/*C++ program that print the root of 5 numbers*/
# include <iostream>
# include <cmath>
using namespace std;
int main()
int i;
float x;
double rootx;
const int counter=5;
cout<< "hello\n"<<endl;
cout<< "I will compute the root of\t" <<counter<< "\t numbers"<<endl;
for (i=0; i<counter ;i++)
{cout<<"Please Enter a number:";
cin>>x;
if (x<0.0)
cout<<"Your number\t"<<x<<"\t does not have a root\n";
else
rootx=sqrt(x);
cout<<"The root of \t"<<x<< "\t is:"<<rootx<<endl ;
cout <<"Good work";
return 0;
Example4:
/*C++ program to draw an upper triangular*/
#include<iostream>
using namespace std;
int main()
{
int i,j,N;
cout<<"Enter an integer N : ";cin>>N;
for(i=1;i<=N;i++)
for(j=1;j<i;j++)
cout<<" ";
for(j=1;j<=N+1-i;j++)
cout<<"*";
cout<<endl;
return 0;
Example5:
/*C++ program to draw lower triangular*/
#include <iostream>
using namespace std;
int main()
int N=0;
cout<<"Enter an intger N: ";
cin>>N;
cout<<endl<<endl;
for(int i=0;i<N;i++)
for(int j=0;j<(N-i);j++)
cout<<"*";
cout<<endl;
cout << "Touch a botton to continue ..." << endl;
return 0;
Example6:
// Summation of squared numbers from 1 to 10 using do-while structure .
#include <iostream>
#include <cmath> // for using the function power two
using namespace std;
// function main begins program execution
int main()
int sum = 0; // initialize sum
int i=1;// initialize a counter
// sum of squared integers from 1 through 10
do
{
sum=sum + pow(i,2);// add squared of number to sum
i++;
while (i<=10);
cout << "Sum is " << sum << endl; // output sum
return 0; // successful termination
} // end function main
Application concern increment-decrement operators
Example1:
#include<iostream>
using namespace std;
int main ()
int x=10;
cout<< x++<<"\n";
cout<<x<<"\n";
cout<<++x<<"\n";
cout<<x<<"\n";
cout<<x--<<"\n";
cout<<x<<"\n";
cout<<--x<<"\n";
cout<<x<<"\n";
return 0;