Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to get an answer in the form of x-integer and than take it's power till n
For example
If a=6
and power=3
Than the program should give
Me output in this form
X-6
(X-6)^2
(X-6)^3

What I have tried:

The code I tried making is this
#include<stdio.h>
 int main() {
 int a,n;
char x; 

printf("enter value of a \n",a);
scanf("%d",&a); 


printf("Value of x is x %c\n",x);
n=x-a;
printf("%c-%d=%c-%d",x,a,n);

 return 0;














 }
Posted
Updated 16-Jul-22 8:27am

 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Start by thinking about what you have to do: read in three values: x, a, and n. Then subtract a from x and store the result in x. Create a power value, and set it to 1. Then add a loop - as I told you you would need three hours ago - to iterate between 1 and n. Each time round the loop, multiply power by x and print it. Simple!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Why is x a char data type ?

If you want to calculate this concretely, you need three variables for the calculation.

The question here would be if all three are read in, or where else do they come from ?

If you don't read in X, you could only output the formula without a concrete result. If you have all three values you can calculate the result. The output of the power seems to be calculated and output step by step. For this you need a loop that runs from 1 ... n.

The result for a power would be calculated stepwise like this:
result = (X - a) * (X - a) * (X - a) ...

Since with (x - a) always the same comes out one needs to calculate it only once with u = x - a. This results in
result = (u) * (u) * (u) ...

At each step, the result of a bracket is calculated and multiplied by the previous result.
C
int a, x, n;

puts("Solver for (x - a) ^ n");

printf("Enter value of a: ");
scanf("%d", &a);

...

int u = x - a;
int result = 1;

for (int i=1; i <= n; i++) {
...
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900