Function to evaluate exponent - c

I wrote the following code in c to evaluate exponent of a number without using the math library
#include <stdio.h>
float powr(float,int);
int main(){
float a;
int b;
printf("Enter base and exponent a^b: ");
scanf("%.2f %d",&a,&b);
float p=powr(a,b);
printf("%.2f",p);
return 0;
}
float powr(float x,int y){
float r=1;
for(int i=1;i<=y;i++){
r=r*x;
}
return(r);
}
but no matter what base and exponent I input, the ouput always comes out to be 1.00. I can't find any mistake in this program and I tried running the powr function algorithm inside main() in a seperate program and it works.

In scanf() it onlys accepts field-width format, but no precision, see here.
You can just input the value, like this:
scanf("%f %d",&a,&b);
Also, you should always check the return value of scanf(). Something like this:
numOfItems = scanf("%.2f %d",&a,&b);
if(numOfItems != 2) // uh-oh
{
printf("Error while input!");
}

Related

C program printing numbers with variable precision

Do you know how a number can be rounded up and printed with a precision that is not fixed by some natural number, but by some variable? If the user needs to enter how many decimal places to round, how to solve it?
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.2f", var);
return 0;
}
Here's simple approach that would work in your case:
You just need to put * before f, and that's it.
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.*f",r, var);
return 0;
}
You can put a * in place of the precision, in which case you can specify an int as the precision.
printf("%.*f", r, var);

Add floating point numbers 23.54 and 33.22 with just integral value like 23+33

There are two floating point numbers 23.54 and 33.22 have to make a program to add them with just left side integral value like 23+33=56.
Here's the code that I tried:
int sum;
sum=(int)num1+(int)num2;
printf("%d",sum);
or
printf("%d",(int)num1+(int)num2);
The datatype of num1 is float and I'm using (int) to typecast to integer type.
Since we typecast the datatype this is called explicit typecasting!
Another option would be to define a function that explicitly names the types used, for example:
static int int_sum(int a, int b) {
return a + b;
}
which could be used as:
#include <stdio.h>
int main()
{
float a, b;
if (scanf("%f%f", &a, &b) == 2) {
printf("%d\n", int_sum(a, b));
}
return 0;
}
note that it's good practice to check that scanf successfully parsed the values it was supposed to before using them. You might instead want to return something else, or maybe print an error message, if the user didn't supply valid input.
#include <stdio.h>
int main(){
float sum;
float num1;
float num2;
printf("first number:");
scanf("%f",&num1);
printf("second number:");
scanf("%f",&num2);
sum=(int)num1+int(num2);
printf("sum is %.2f",sum);
}

Double Exponent from user input

So my question relates to double, I am trying to get an input from the user in decimal point for any value and its exponent also in decimal point to display the result after calculation in another function where the variables will pass values as double and I have used double the output as well but the end result is 1.00000 even though I have used the output specifier as %lf%.
#include <stdio.h>
double pwra (double, double);
int main()
{
double number, power, xx;
printf("Enter Number: ");
scanf("%lf", &number);
printf("Enter Number: ");
scanf("%lf", &power);
xx=pwra (number,power);
printf("Result: %lf", xx);
return 0;
}
double pwra (double num, double pwr)
{
int count;
int result = 1;
for(count=1;count<=pwr;count++)
{
result = result*num;
}
return result;
}
You have used the wrong type for result in the pwrs() function.
Change:
int result = 1;
to:
double result = 1.0;
Note that this type of simple mistake is easily identified if you learn to use your debugger. Further reading: How to debug small programs.
Also note that pwr should be an int, not a double, since your function only works with integer exponents.

How i can handle garbage?

I am novice to C. I am trying to write a program to calculate the Hypotenuse, but when I run it I get garbage values and I don't know how to handle it.
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%f",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%f",&p);
printf("The hypotenuse of the triangle is %3.3f",Hypotenuse(b,p));
return 0;
}
problem is in your scanf statements.
As a thumb rule you should always use "%lf" for doubles.
This works perfectly:
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%lf",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%lf",&p);
printf("The hypotenuse of the triangle is %3.3lf",Hypotenuse(b,p));
return 0;
}
So, the best way to see what is going on in your code or where u get garbage, after all scanf print the value
ex:
double a;
scanf("%f",&a);
printf("a=%f",a); //OR
printf("a=%3.3f",a);
and you can easy see if the problem is in your scanf or somewhere else..

How do you return a value from a called function to the main?

I use the return command then try to print the value from the main. It returns a value of zero (0).
This program is about temperature conversion from Celsius to Fahrenheit.
Also how can you use a rounding function to round the answer to an integer so it is not a floating point number with decimals.
#include <stdio.h>
int Cel_To_Fah(int a, int b); // function declaration
int main (void)
{
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
Cel_To_Fah(a,b); // function call
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(a,b)
{
b=1.8*a+32;
return b;
} // Cel_To_Fah
You just have to use the assignment operator:
b = Cel_To_Fah(a);
Your program has a lot of problems, though, including your Cel_To_Fah function not having a correct signature. You probably want something like:
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
You should probably get a good beginner C book.
No need of second argument to function(b).
You can do this by...
#include<stdio.h>
int Cel_To_Fah(int a); // function declaration, as it returns a values;
int main (void)
{
int a; int b;
printf(" Enter temperatrure: ");
scanf("%d", &a);
b = Cel_To_Fah(a); /* the returned value is stored into b, and as b is an integer so it is automatically rounded. no fraction point value can be stored into an integer*/
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
there are several issues. First you need to use float, not int, so that you can have values with a decimal point. otherwise your calculations will come out wrong. Also use 32.0 instead of 32 for the same reason.
Second, you need to understand that the a and b in your function are NOT the same as the a and b in main. They have the same name but are not in the same "scope". So changing the one in your function doesn't affect the one in main. That's why in main you have to say b=Cel... so that b in main will get the returned value.
finally, in c, you're supposed to put your functions above/before main. Otherwise it's technically not defined "yet", though some modern compilers will fix that for you. Read about function prototypes.
Since your function Cel_To_Fah(a,b); is returning a value (int type), you must have to assign it to a variable of its return type (int type).
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b = Cel_To_Fah(a); // function call
printf("The temperature is: %d\n", b);
and your function should be
int Cel_To_Fah(a)
{
int b = 1.8*a+32;
return b;
} // Cel_To_Fah
And do not forget to change your function prototype to
int Cel_To_Fah(int a);
I saw two issues in your code. Firstly, it is variable type. I assume that you want Celsius as integer; but Fahrenheit = 1.8*Celsius+32 should be float. Therefore b should be float.
Secondly, you should not return a value from a function via its input parameters (unless you learn pointer or call by ref). I rewrite your code as following:
include<stdio.h>
float Cel_To_Fah(int a); // function declaration
int main (void)
{
int a;
float b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b=Cel_To_Fah(a); // function call
printf("The temperature is: %.2f\n", b); //showing 2 decimal places
return 0;
} // main
float Cel_To_Fah(int a)
{
float b;
b=1.8*(float)a+32; //cast a from int to float
return b;
} // Cel_To_Fah

Resources