# scanf/printf format specifiers for double data types

Page 1 of 1

## 3 Replies - 186862 Views - Last Post: 10 November 2006 - 12:37 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20682&amp;s=337f24d588350b330b5ec12f01d232f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 intaminfan628

Reputation: 0
• Posts: 8
• Joined: 09-November 06

# scanf/printf format specifiers for double data types

Posted 09 November 2006 - 10:37 PM

``` #include<stdio.h>
#include<math.h>
double intgr(double m, int n, double A, double a, double b);
double func(double x, double , int n, double a);
double nfact(int n);
int main ()

{

double a, z, b, A, x, m, sum2;
int n;
printf("Enter the exponents (double)m and (int)n in f(x)=A*x^m/n!:");
scanf("%f %d", &m, &n);
printf("\n\n Enter the coefficient A in  f(x)=A*x^m/n!:");
scanf("%f", &A);
printf("\n\nEnter the bounds for the integration interval, a < b:");
scanf("%lf %lf", &a, &b);
printf("\n\n Integrate f(x) on [a,b]\n");
printf("m=%f\n", m);
z= intgr(m, n, A, a, b);
printf("The value of the integral is %lf\n", z);

}

double intgr(double m, int n, double A, double a, double b)

{
int trap, k, fact;
double sum2, f, del_x, x, z;
printf("Enter the number of trapezoids:");
scanf("%d", &trap);
printf("%f %lf %f %d %f \n", m, a, b, trap, A);

del_x=(b-a)/trap;
sum2=0;
f=a*pow(x,m)/fact;
sum2=-.5*f;

for(k=0; k<=trap; k++)
{

fact=1;
for(b=n; b>1; b--)
{	  fact=fact*b;}
printf("%lf=factorial\n", fact);
x=k*del_x;
f=a*pow(x,m)/fact;
sum2=sum2+f;
z=sum2;
printf("%lf = integral\n\n", z);
return z;
}

```

i am using m=3.25
n=5
A =-1.5
a=1.5
b=3.75

When printf prints m it says m=64 and A=-1.5 why is this happening? Also the integral does not print out a number value, it only prints 0.000.

I am sure there are other problems, but i need to work the others out first.

edit: modified title ~ jayman9

Is This A Good Question/Topic? 0

## Replies To: scanf/printf format specifiers for double data types

### #2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: scanf/printf format specifiers for double data types

Posted 09 November 2006 - 10:46 PM

When taking input for type double you need to use lf format specifier in the scanf statement. But when you are outputting a double you use f format specifier in the printf statements.

### #3 intaminfan628

Reputation: 0
• Posts: 8
• Joined: 09-November 06

## Re: scanf/printf format specifiers for double data types

Posted 09 November 2006 - 11:23 PM

Great, thanks, but now how come the integral and fact (factorial) loop both =0, the integral should = -0.792905

### #4 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: scanf/printf format specifiers for double data types

Posted 10 November 2006 - 12:37 AM

the following now gets fact OK
```#include<stdio.h>
#include<math.h>
double intgr(double m, int n, double A, double a, double b);
double func(double x, double , int n, double a);
double nfact(int n);
int main ()

{

double a, z, b, A, x, m, sum2;
int n;
printf("Enter the exponents (double)m and (int)n in f(x)=A*x^m/n!:");
scanf("%lf %d", &m, &n);
printf("\n\n Enter the coefficient A in  f(x)=A*x^m/n!:");
scanf("%lf", &A);
printf("\n\nEnter the bounds for the integration interval, a < b:");
scanf("%lf %lf", &a, &b);
printf("\n\n Integrate f(x) on [a,b]\n");
printf("%f %d %f %f %f \n", m, n, A,  a, b);
z= intgr(m, n, A, a, b);
printf("The value of the integral is %f\n", z);

}

double intgr(double m, int n, double A, double a, double b)

{
int trap, k, fact;
double sum2, f, del_x, x, z;
printf("Enter the number of trapezoids:");
scanf("%d", &trap);
printf("%f %f %f %d %f \n", m, a, b, trap, A);

del_x=(b-a)/trap;
printf("%f %f %f %d %f \n", m, a, b, trap, A);
sum2=0;
f=a*pow(x,m)/fact;  // ***** x and fact not initialised  ???
sum2=-.5*f;
printf("%f %f %f %d %f \n", m, a, b, trap, A);

for(k=0; k<=trap; k++)
{

fact=1;
for(b=n; b>1; b--)
{	  fact=fact*b;}
printf("%d=factorial\n", fact);
x=k*del_x;
f=a*pow(x,m)/fact;
sum2=sum2+f;
z=sum2;
printf("%f = integral\n\n", z);
return z;
}
}

```

but in the statement
f=a*pow(x,m)/fact;
x and fact have not been initialised (remember C does not initialise local variables inisde {})
initialise these and it may work