plz see the "C" coding.tel watz wrong?

Status
Not open for further replies.

tweety_bird_bunny

Journeyman
i did following coding in devc++ ver4..... it is taken from kanetkar's Let Us See book....... but it doesn't seem to work....it should recieve a float value & return its square.... no matter what i enter it gives its square as 0.000000.....
whats going wrong any suggestions.......
if it works fine on ur sys plz tel me ur compiler name....


main()
{
float square();
float a,b;
printf("plz enter the float number\n");
scanf("\n%f",&a);
b=square(a);
printf("\nsquare is %f",b);


}
float square(float x)
{
float y;
y=x*x;
return(y);
}
 

siriusb

Cyborg Agent
The code shud work. But there are a few modifications that I would suggest.
Put the square function above the main function or use a function declaration above main.
 

Thor

Ambassador of Buzz
C problrm solved

Did u include stdio.h ??
Code:
#inclue<stdio.h>
=====================
Code:
 b=square(a)

for the abv code to work float square();
must become
Code:
float square(float);

Now
the complete program :
Code:
#include<stdio.h>
#include<conio.h>
main()
{
float square(float);
float a,b;
printf("plz enter the float number\n");
scanf("\n%f",&a);
b=square(a);
printf("\nsquare is %f",b);
getch();
}
float square(float x)
{
float y;
y=x*x;
return(y);
}

IMPORTANT : I had to put
Code:
#include<conio.h>
and
Code:
getch();
because I use Borland C++ , without it the output screen blinks out in a ziffy and doesn't allow me to see the output. If u use DOS v compiler u can do without those 2 lines of Code too.
 

deathvirus_me

Wise Old Owl
The float() function is declared as
float square();

Thats a no argument function .... it will only return a value ..

U've now defined the function as
float square(float x)

So even if u pass a value to the function x will be zero because u've declared the function differently ...

It should be declared as float square(float)


here's the complete program :

Code:
#include <stdio.h>
#include <conio.h>
main()
{
   float square(float);
   float a;
   clrscr();
   printf("plz enter the number :");
   scanf("%f",&a);
   printf("\n\n Square is %f",square(a));
   return 0;
}
float square(float x)
{
   return(x*x);
}
 
Status
Not open for further replies.
Top Bottom