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);
Related
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!");
}
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);
}
Could you tell me why this did not work?
I have to show Pi number from Basel problem, but I don't know why the program shows the same number all the time, despite the fact I choose different 'numbers'.
Thanks a lot!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi 3.14159
int main()
{
int number;
printf("max number: ");
scanf("%d",&number);
float euler;
float sum=0;
for(int i=1; i<number;i++)
{
sum=sum + (1/(i*i));
}
euler=sqrt(6*sum);
printf("Euler: %lf \n",euler);
printf("Pi from math library = %f",Pi);
return 0;
}
The problem is that the expression (1/(i*i)) is calculated using integer mathematics. In other words, the result is always an integer, and for any value of i greater than 1, the result will always be 0.
The simplest fix is to rewrite that expression as (1.0f/(i*i)). This will square i, convert the result to a float, and then do the division.
the posted code is mixing integer division with double literal and is expecting the result to be a float
The following proposed code :
cleanly compiles
performs the implemented functionality
and now, the proposed code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// define a float value rather than a double value
#define Pi 3.14159f
int main( void )
{
int number;
printf("max number: ");
scanf("%d",&number);
float euler;
float sum=0;
for(int i=1; i<number;i++)
{
// perform float math rather than integer math
sum=sum + (1.0f/(float)(i*i));
}
euler=sqrtf(6*sum); // < note float version rather than double version
printf("Euler: %lf \n",euler);
printf("Pi from math library = %f",Pi);
return 0;
}
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.
I wanted to print a number to variable number of decimal places in C.
I have written the code
#include<stdio.h>
main()
{ int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.af", a, 2.554648);
return 0;
}
Use * in printf() to mark how many decimal places you want:
#include <stdio.h>
int main(void)
{
int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.*f", a, a, 2.554648);
return 0;
}
You need the * format specifier. Here is a short example(see in ideone):
#include <stdio.h>
int main(void) {
int a = 5;
double temp = 5.0 / 7;
printf("%.*f",a, temp);
return 0;
}