-->




C plus plus assignment help service

power function code using pointers c++

Leave a Comment
Write a program which calculates the power of a number using pointers. User will input two numbers and program will calculate power. First number will be base and second number will be the power.
program prototypes
power function pointers example c++
functions prototypes
Program has been tested on codeblocks c++ complier.
see also: pointers multiplication example, pointers addtion example code

program source code:


#include <iostream>
using namespace std;


int Power(int *, int *);
float Power1(int *, int *);
double Power2(int *, int *);

int main()

{
    int a,b;

 cout<<"Enter 1st Value:";
 cin>>a;

 cout<<"Enter 2nd Value:";
 cin>>b;

 cout<<endl <<endl;

 cout<<"...............Power.........." <<endl <<endl;

 cout<<"The Power of these numbers is:"<<Power(&a,&b)<<endl;
 cout<<"The Power of these numbers is:"<<Power1(&a,&b)<<endl;
 cout<<"The Power of these numbers is:"<<Power2(&a,&b)<<endl;

 cout<<endl <<endl;

 return 0;

}


int Power(int *x, int *y)

{
 int P=1;
 for(int i=0;i<*y;i++)
 {
  P=(P * *x);
 }
 return P;
}

float Power1(int *x, int *y)

{
 float P=1;
 for(int i=0;i<*y;i++)
 {
  P=(P * *x);
 }
 return P;
}

double Power2(int *x, int *y)

{
 double P=1;
 for(int i=0;i<*y;i++)
 {
   P=(P * *x);
 }
 return P;
}

program input output
power function code example pointers c++
find more example here: c++ examples code
Read More...

pointer multiplication c++ example source code

Leave a Comment
Write a program to multiply two numbers using pointers. Make function for multiplication take input from user. There should be three function each should return a different data type int, float and double.

Prototypes for functions are
simple pointer multiplication example c++ code



This program has been tested on c++ codeblocks IDE
C++ source code:
see also: pointer addition example source code

#include <iostream>
using namespace std;

int Mult(int *, int *);
float Mult1(int *, int *);
double Mult2(int *, int *);

int main(){

    int a,b;

 cout<<"Enter 1st Value:";
 cin>>a;
 cout<<"Enter 2nd Value:";
 cin>>b;
 cout<<"...............Multiplication.........." <<endl <<endl;

 cout<<"The integer Multiplication of these numbers is:"<<Mult(&a,&b)<<endl;
 cout<<"zThe float Multiplication of these numbers is:"<<Mult1(&a,&b)<<endl;
 cout<<"The double Multiplication of these numbers is:"<<Mult2(&a,&b)<<endl;

 cout<<endl <<endl;
 return 0;
}

int Mult(int *x, int *y){
 int Prod;
 Prod=(*x * *y);
 return Prod;
}

float Mult1(int *x, int *y){
 float Prod;
 Prod=(*x * *y);
 return Prod;
}

double Mult2(int *x, int *y){
 double Prod;
 Prod=(*x * *y);
 return Prod;
}

Program input output
c++ simple pointer multiplication example source code


Find more: c++ programming simple examples

Read More...

c++ pointer addition example

Leave a Comment
Write a program which calculates addition of two numbers using pointers. program has three functions which receives two pointers reference. Three functions returns int, float and double sum of numbers. This c++ tutorial use the following concepts.


  • pointers in c++
  • functions
  • passing reference to function

This code has been tested on codeblocks c++ compiler

C++ source code
#include <iostream>
using namespace std;

int Sum(int *, int *);
float Sum1(int *,int *);
double Sum2(int *, int *);


int main()

{
    int a,b;


 cout<<"Enter 1st Value:";
 cin>>a;

 cout<<"Enter 2nd Value:";
 cin>>b;

    cout<<"...............ADDITION.............." <<endl <<endl;

 cout<<"The integer sum of these numbers is:"<<Sum(&a,&b)<<endl;
 cout<<"The float sum of these numbers is:"<<Sum1(&a,&b)<<endl;
 cout<<"The double sum of these numbers is:"<<Sum2(&a,&b)<<endl;

 cout<<endl <<endl;

 cout<<endl <<endl;


 return 0;

}


int Sum(int *x, int *y )

{
 int sum;
 sum=(*x + *y);
 return sum;
}

float Sum1(int *x,int *y )

{
 float sum;
 sum=(*x + *y);
 return sum;
}

double Sum2(int *x, int *y )
{
 double sum;
 sum=(*x + *y);
 return sum;

}



Input output of code
c++ pointer example to add two numbers

addition functions
two number additions using pointers c++
Find more here: c++ examples with output
github source
Read More...

c++ program to count spaces in a string

Leave a Comment
Write a program which calculate number of spaces in a string using pointer. Make a separate function which takes a pointer and using for loop it calculates all spaces.
This example covers the concept of 
  • pointers in c++
  • functions
  • loop
C++ source code

#include <iostream>
using namespace std;
void Calculate_Spaces_alpha(char *);
int main()
{
 char string [] = "This string contains four spaces";
 cout <<"String is "<<"\n%----------------------%\n" << string << endl;
 char *str = string;
 Calculate_Spaces_alpha(str);
 return 0;
}

void Calculate_Spaces_alpha(char *ptr)
{
 int spaces = 0;
 int characters = 0;
 for ( ; ; ptr++)
 {
  if (*ptr == ' ')
   spaces++;

  if (*ptr != '\0')
   characters++;
  else
   break;
 }
 cout << "\nIn the String: "<< endl << "Characters:  "<< characters << endl <<"Spaces:  " << spaces << endl;
}


This code has been tested on c++ Code blocks compiler
code view


see more examples here: C++ Example Source Codes
Read More...