Thursday, July 30, 2009

A c++ program help?

c++ program to create functions that calculate the value of x^n. Assume that x=|=0, n=|=0.





a fully structured program would help me finer.





thanks in advance

A c++ program help?
double power(double x,double n)


{


if(n==0)return(1);


return(x*power(x,n-1));


}





void main()


{


double x,n;


scanf("%f",x);


scanf("%f",n);


printf("The answer is:%f",power(x,n));


}
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;math.h%26gt;


void cal(int a, int b)


{


int j;


j = pow(a,b);


cout%26lt;%26lt;"The Answer is : "%26lt;%26lt;j%26lt;%26lt;endl;


}


void main()


{


clrscr();


int i;


int n;


cout%26lt;%26lt;"Enter the value for X :"%26lt;%26lt;endl;


cin%26gt;%26gt;i;


while(i==0)


{


cout%26lt;%26lt;"Plz Enter a non Zero value"%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter a Value For X :"%26lt;%26lt;endl;


cin%26gt;%26gt;i;


}


cout%26lt;%26lt;"Enter the value for N :"%26lt;%26lt;endl;


cin%26gt;%26gt;n;


while(n==0)


{


cout%26lt;%26lt;"Plz Enter a non Zero value"%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter the Value for N :"%26lt;%26lt;endl;


cin%26gt;%26gt;n;


}


cal(i,n);


}


























Hope it Helps you...............♪♪♪♪♪♪♪♪♪
Reply:It's So Easy Just Tell Me


unleasheddiego@yahoo.com
Reply:I dont know that whether you want to find x^n or x raised to n.





So i have enclosed the program to find x raised to n program. If you want the other program mail me at m_gopi_m@yahoo.co.in





/*





program to find the power of a given number.


note: instead of the pow() function we can also use


the pow() function of %26lt;math.h%26gt;





*/





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;





double pow(double,signed int);





void main()


{


double num,ans;


signed int deg;





clrscr();


cout%26lt;%26lt;"\n\n Enter a number: ";


cin%26gt;%26gt;num;





cout%26lt;%26lt;"\n Enter the degree to be raised: ";


cin%26gt;%26gt;deg;





ans=pow(num,deg);


cout%26lt;%26lt;"\n\n\n "%26lt;%26lt;num%26lt;%26lt;" raised to the power of "%26lt;%26lt;deg%26lt;%26lt;" is "%26lt;%26lt;ans;


getch();


}





double pow(double a,signed int b)


{


signed int i;


double power=1;





for(i=0;i%26lt;b;i++)


power*=a;





return(power);


}
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int x,n,a;


clrscr();


printf("enter the value of x");


scanf("%d",%26amp;x);


printf("enter the value of n");


scanf("%d",%26amp;n);


a=x*n;


printf("the value of a is %d",a);


getch();


}
Reply:You can create a function called in your class


public int findSquare(int x ,int n)


{


int i=0,result=1;


for(i=0;i%26lt;n;i++)


result=result*x;


return result;


}





// since you already assumed that x != 0 and n != 0 or else


// you can check to see if the values of x and n are 0 .if not


// proceed with the code . if x==0 then return 0 else if n ==0


// and x !=0 then return 1 .
Reply:#include%26lt;iostream%26gt;





int main(){





std::cout %26lt;%26lt; " X = " %26lt;%26lt; '\n';


int x;


std::cin%26gt;%26gt;x;


std::cout %26lt;%26lt; "n= " %26lt;%26lt; '\n';


int n;


std::cin%26gt;%26gt; n;





int ans = x^n;





std::cout %26lt;%26lt; "Answer: " %26lt;%26lt; ans %26lt;%26lt; '\n';








return 0;


};


No comments:

Post a Comment