Using pow() to power tow variables - c

I have 2 variables and after i gave those number to computer it power them
like:
And I want to do this: a^b. Then print it:
int a ;
int b ;
scanf ("%d" , &a);
scanf ("%d" , &b);

man pow says:
double
pow(double x, double y);
...
The pow() functions compute x raised to the power y.
You need to include math.h.
In code it would look like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a, b;
if(scanf("%d", &a) != 1) {
fprintf(stderr, "wrong input for a");
exit(1);
}
if(scanf("%d", &b) != 1) {
fprintf(stderr, "wrong input for b");
exit(1);
}
double result = pow(a, b);
printf("result of %d^%d=%g\n", a, b, result);
return 0;
}
Please note that scanf returns the number of input items assigned. So it makes sense to check for invalid input there.

Related

Returns a value as an output parameter

I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

Squared Equation calculator not working in C

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

Scanf double in C not giving the right value

I would like to know where is my mistake.
When scanning the parameters as doubles , and immediately printing them (for checking), the print is not giving me the values that I entered.
I tried to define them as integers and it worked, but for doubles its just giving me this: a=0.00000, b=-0.00000
take a look:
#include <stdio.h>
#include <stdbool.h>
int main()
{
double a=0,b=0,c=0;
scanf("%lf",&a);
scanf("%lf",&b);
scanf("%lf",&c);
printf("%lf %lf\n",a,b);
return 0;
}
EDIT: sorry i didnt include my whole code , this is the whole code, but it still gives me the same thing.
Written as it is, your program will accept correctly formatted input for 3 floating point values and will print the first two.
What values do you enter?
What is the precise input you type into your program?
I suspect you type extra characters: scanf stops scanning on invalid input.
You should test the return value from the scanf function calls and verify that values were actually parsed.
Incidentally, the printf format for double arguments is %f, not %lf, but this should not pose a problem as the extra l is most likely ignored.
Here is a corrected version you should try to find out where the problem lies:
#include <stdio.h>
int main(void) {
double a = 0, b = 0, c = 0;
if (scanf("%lf", &a) != 1) {
printf("invalid input for a\n");
}
if (scanf("%lf", &b) != 1) {
printf("invalid input for b\n");
}
if (scanf("%lf", &c) != 1) {
printf("invalid input for c\n");
}
printf("a=%f b=%f\n", a, b);
return 0;
}

C - Loop, <math.h>, and float

#include <stdio.h>
#include <math.h>
int main()
{
int i;
int result;
float f;
while((result = scanf("%d", &i)) != EOF)
{
scanf("%f", &f);
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
Hi,
I just began with C and I'm having a problem solving a question.
The problem is that with the user input, I need to get three sets of numbers that are floored, rounded, and ceiled. This process must be ongoing until the user stops by EOF command (Ctrl-D).
When I run my code above, input a value 3.1415, I get 0 0 1 as an output, which is wrong because it's supposed to be 3 3 4.
Any suggestions or help on how to fix the problem would be appreciated.
According to your code, you first need to input an integer value and then enter a float value.
OR, you can start accepting float value like this:
#include <stdio.h>
#include <math.h>
int main()
{
int result;
float f;
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
According to your code you need to input integer first then float, But if you enter float value first, that value read by i first and return 0 that is !=EOF, so second scanf does not wait for input, because it is inside the while loop. So always you will get 0 0 1 for all inputs!
To scan a number inside while loop use-
if (scanf("%f", &f) == 0) {
printf("Err. . .\n");
do {
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
Else scan float value first instead of int and float. Try this code-
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");

Is there a way to use scanf with the "if" and "else" statements?

I have to create and call a function from main. Then I have to call scanf to read two integers and print out the bigger one. Then I have to do another scanf, but this time with doubles instead of integers.
int main()
{
int x, y;
scanf("%d%d", &x, &y);
if (x > y)
{
printf("%d", x);
}
scanf("%lf%lf", &w, &z);
if ( w > z)
{
printf("%f", w);
}
return 0;
}
I'm not sure if I did this right and how would I check the return value to see that the scanf worked? Thanks.
how would I check the return value to see that the scanf worked?
By checking the return value of scanf().
if( scanf("%d%d", &x, &y) != 2)
{
/* input error */
}
if (x > y)
{
printf("%d", x);
}
if(scanf("%lf%lf", &w, &z) != 2)
{
/* input error */
}
if ( w > z)
{
printf("%f", w);
}
From scanf() documentation:
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in
the event of an early matching failure.
The value EOF is returned if the end of input is reached before
either the first successful conversion or a matching failure occurs.
EOF is also returned if a read error occurs, in which case the error
indicator for the stream (see ferror(3)) is set, and errno is set
indicate the error.
In your case, you can check whether scanf() has worked succesfully or not as follows,
if( scanf("%d%d", &x, &y) == 2)
{
if (x > y)
{
printf("%d", x);
}
}
else
{
printf("\nFailure");
}
scanf() returns the number of arguments successfuly read by it.
It says you're trying to call a function from main. Does that mean that your teacher wants a function besides main? In which case you want to create one as below. You will also have to do something similar only with double integers.
#include stdio.h
int bigger(void);
int main(void)
{
int larger;
larger = bigger(void);
printf("The Larger integer is: %d\n", larger);
return 0;
}
int bigger(void)
{
int x,y,z;
printf("Enter your first integer\n");
scanf("%d", &x);
printf("Enter your second integer\n");
scanf("%d", &x);
if(x > y)
{
z = x;
}
else
{
z = y;
}
return z;
}
sollution for
I have to create and call a function from main. Then I have to call scanf to read two integers and print out the bigger one. Then I have to do another scanf, but this time with doubles instead of integers.*/
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b;
printf("enter any two numbers");
scanf(" %d\n %d",&a,&b);
if(a>b){
printf("bigger number is %d",a);
}else{
printf("bigger number is %d\n",b);
}
float c,d;
printf("enter any two numbers");
scanf(" %f\n %f",&c,&d);
if(c>d){
printf("bigger number is %.2f",c);
}else{
printf("bigger number is %.2f\n",d);
}
system("pause");
return 0;`
}

Resources