Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Printing a float in c

I have developed a program that is supposed to a value between -1 to 1  with decimal point.

Every thing seem to work fine only that it prints 0 for all values.

Problem seems to be with this line

printf("\n Correction Coffecient  = %f", (numerator/denominator));

Thanks
#include <stdio.h>
#include <conio.h>
#include <math.h>


main()
   {
int odds[20], i,j,cn,n, prn[20];

long sumxy, sumx, sumy, sumxsqr, sumysqr, denominator, numerator;
float cc;
unsigned long int d1,d2;
 cn=0;
 i=2;

  /* start at 2 for prime numbers becauase 1 is not */

 /*======== to get the prime numbers ========*/

 while (cn<20)
     {
       for(j=2; j<=i; j++)
      {
       if (i % j==0)
	    {
	     if (j==i)
	      {
	     prn[cn] = i;
	     cn++ ;
	      }
	     else{
	      break;
	      }
	    }
      }
	  i++;
	  }


   clrscr();
	   printf("Prime numbers are below");
	   printf("\n=======================");
	   for (n=0; n<20; n++)
	   {
	   printf("\nPrime number: %d ie = %d", n+1, prn[n]);
	   }


/* odd numbers */

cn=0;
i=1;
  printf("\n\nOdd numbers below");
  printf("\n==================");
while (cn<20)
 {
   for(j=i; j<=i; j++)
     {
      if(j%2==1)
       {
       printf("\nOdd number %d is %d",cn+1,j);
       odds[cn]=j;
       cn++;
       }
     }
     i++;

    }


    /*getting the correlation coefficient*/

    sumxy=0;sumx=0; sumy=0; sumxsqr=0; sumysqr=0;numerator=0;denominator=0;d1=0;d2=0;
    for(i=0;i<20;i++)
     {
     sumxy+=(prn[i]*odds[i]);
     sumx+=prn[i];
     sumy+=odds[i];
     sumxsqr+=(prn[i]*prn[i]);
     sumysqr+=(odds[i]*odds[i]);
     }


     numerator=20*sumxy-(sumx*sumy);
     denominator=sqrt(20*sumxsqr-(sumx*sumx))*sqrt(20*sumysqr-(sumy*sumy));

     cc=20/30;  /* (numerator/denominator)*100;*/

     printf("\n sumxy =%d",sumxy);
     printf("\n sun x = %d", sumx);
     printf("\n sum y = %d", sumy);
     printf("\n sum x squared = %d", sumxsqr);
     printf("\n sum y squared = %d", sumysqr);
     printf("\n Numerator = %ld", numerator);
     printf("\n Denominator = %ld", denominator);


   printf("\n Correction Coffecient  = %f", (numerator/denominator));

    getch();

Open in new window

0
Anthony Matovu
Asked:
Anthony Matovu
1 Solution
 
Infinity08Commented:
numerator and denominator are long's, so the division is an integer division.

You'll need to cast at least one of the operands for the division to a float, in order to force floating point division :

        printf("\n Correction Coffecient  = %f", ((float) numerator/denominator));
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now