C++ PROGRAMMING EXAMPLES

Here some examples are revised and are related to c++ programming fundamental and basic concepts . These examples are useful and helpful for programming beginners for software developers and for software engineers.


Example..... of find larger number of two or more in c++ program ?

#include <iostream>
using namespace std;
float larger(float  a, float b);
int main ()

{
   float num1,num2;
cout<<"enter two numbers";
cin>>num1>>num2;
cout<<"larger is "<<larger(num1,num2);
cout<<endl;
return 0;
}
float  larger(float a,float  b)
{
float  larger;
if (a>=b)
larger=a;
else
larger=b;
return larger;
}





EXAMPLE .....  Find power of any two numbers ?

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double u,v;
u=4.2;
v=3.0;
cout<< u<<"to the power of "<<v<<"="<<pow(u,v)<<endl;
return 0;
}



EXAMPLE......  SUM of two numbers by function ?

#include <iostream>
using namespace std;
int sum(int a,int b)
{
    int sum;                                  
sum=a+b;
return sum;
}
  int main ()
{

int num1,num2;
cout<<"enter two numbers";
cin>>num1>>num2;
cout<<"sum is "<<sum(num1,num2);

return 0;
}



EXAMPLE...... Find uppercase of any alpahabatic ?

#include <iostream>
using namespace std;
int main ()
{
cout<<"uppercase a is "<<static_cast<char>(toupper('a'))<<endl;
return 0;
}



EXAMPLE..... SUM of two numbers of type double ?

#include <iostream>
using namespace std;
double sum(double  a, double b);
int main ()

{
  double num1,num2;
cout<<"enter two numbers;"<<endl;
cin>>num1>>num2;
cout<<"sum is "<<sum(num1,num2);
cout<<endl;
return 0;
}
double sum(double a,double  b)
{
double  sum;
sum=a+b;
return sum;
}



EXAMPLE.... sum of 5. Hint .5+4+3+2+1.

#include <iostream>

using namespace std;
int fun(int number);
int main ()
{
int number=5;
cout<<"sum"<<fun(number)<<endl;
return 0;
}
int fun(int number)
{
if (number==1)
{
return 1;
}
int result=fun(number-1);
return number+result;
}



EXAMPLE...... Find power of 5.0 to 4 and its application ?

#include <iostream>
#include <cmath>

using namespace std;
int main ()
{
double u,v;
u=4.2;
v=3.0;
cout<<u<<"to the power of"<<v<<"="<<pow(u,v)<<endl;
cout <<"5.0 to the power of 4="<<pow(5.0,4)<<endl;
u = u + pow(3.0,3);
cout<<" u = " <<u<<endl;

return 0;
}


EXAMPLE......... compare two numbers or variables?

#include <iostream>
using namespace std;
int compare(int a,int b);
int main ()

{
int num1,num2;
cout<<"enter two numbers";
cin>>num1>>num2;
cout<<"larger is "<<compare(num1,num2);
cout<<endl;
return 0;
}
int compare(int a,int b)
{
int larger;
if (a>b)
larger=a;
else
larger=b;
return larger;
}




EXAMPLE..... find power in recursion or in factorial ?

#include <iostream>
using namespace std;
int fun(int number,int p);
int main ()
{
int number=2;
int p=3;
cout<<fun(number,p)<<endl;
return 0;
}
int fun(int number,int p)
{
if (p==0)
{
return 1;
}
int result=fun(number,p-1);
return number*result;
}




EXAMPLE...... related to while loop?

#include <iostream>
using namespace std;
int main ()
{
     int i=0;
while (i<=20)
{
cout<<i<<" ";
i=i+5;
}
cout <<endl;
}


EXAMPLE...... Find sum of two numbers in a simple way ?

#include <iostream>
using namespace std;
int sum(int a,int b)
{
   int sum;
   sum=a+b;
   return sum;
}
int main ()
{
int z;
z=sum(5,3);
cout<<"the result is"<<z;
return 0;
}



EXAMPLE...... A simple loop ?

#include <iostream>
using namespace std;
int main ()
{
for (int n=10;n>0;n--)
cout<<n<<endl;
return 0;
}




EXAMPLE..... of function overloading ?

#include <iostream>
using namespace std;
double sum(int  a, int b)


{
  double sum;
  sum=a+b;

return sum;
}
int subtraction(double a ,double  b)
{
int subtraction;
subtraction=a-b;
return subtraction;
}
int main ()
{
double x=2.0,y=3.0;
cout<<"sum is "<<sum(x,y);
cout<<endl;
   int m=2,n=1;
   cout<<"subtraction is "<<subtraction(m,n);
   cout<<endl;
   return 0;
}





EXAMPLE...... take an integer and find its double ?

#include <iostream>
using namespace std;
int main (){
int i;
cout<<"please enter an integer:"<<endl;
cin >>i;
cout<<"The integer is "<<i<<"and its double is "<<i*2;
return 0;
}




EXAMPLE..... find sum of two numbers in a high manner ?

#include <iostream>
using namespace std;
int main ()
{
int num1,num2,sum;
cout<<"enter two numbers:"<<endl;
cin>>num1>>num2;

sum=num1+num2;
cout<< "sum of "<<num1<<"and"<<num2<<"="<<sum<<endl;
return 0;
}





EXAMPLE....... of reference . value pass by reference ?

#include <iostream>
using namespace std;
void funone(int   a, int &b);
int main ()

{
   int num1,num2;
   num1=10;
   num2=15;
   cout<<"inside main:num1="<<num1<<",num2="<<num2<<endl;
funone(num1,num2);
cout<<"after funone:num1="<<num1<<",num2="<<num2<<endl;

return 0;
}
void  funone(int  a,int  &b)
{
int one;
one=a;
a++;
b=b*2;
cout<<"inside funone:a="<<a<<",b="<<b<<",and one="<< one<<endl;
}






EXAMPLE........ find power ,and absolute value ?

#include <iostream>


using namespace std;
int main ()
{
int x;
double u,v;
cout<<"uppercase a is "<<static_cast<char>(toupper('a'))<<endl;
u=4.2;
v=3.0;
cout<<u<<"to the power of"<<v<<"="<<pow(u,v)<<endl;
cout <<"5.0 to the power of 4="<<pow(5.0,4)<<endl;
u = u + pow(3.0,3);
cout<<" u = " <<u<<endl;
x=-15;
cout<<"absolute value of"<<x<<"="<<abs(x)<<endl;

return 0;
}





EXAMPLE....... find area of circle ?

#include <iostream>
using namespace std;
int main ()
{
double radius,area_circle;
cout <<"enter radius of circle:"<<endl;
cin>>radius;
area_circle= 3.14 * radius * radius;
cout<<"area_circle:="<<area_circle<<endl;
return 0;
}





EXAMPLE...... compare two numbers in a high manner ?

#include <iostream>
using namespace std;
int compare(int a,int b)
{
    int larger;                                  
if (a>b)
larger=a;
else
larger=b;
return larger;
}
  int main ()
{

int num1,num2;
cout<<"enter two numbers";
cin>>num1>>num2;
cout<<"larger is "<<compare(num1,num2)<<endl;

return 0;
}