reciprocal calculator in c

Thread Starter

jonathanf

Joined Oct 31, 2010
1
Hi all,
Im wondering if I can get some pointers.
I have an assignment for basic "c" programming , a reciprocal calculator with conditions.The conditions being : if entry =0 then print "reciprocal undefined" (this bit I can get!). else (should be) print "the reciprocal = 1/(number entered) or (decimal equivelent). (this bit is frying my head).
please help. this is what I have so far

// reciprocal calculator
// by jf
// date last modified 31/10/10

// include standard input/output from header file h
#include <stdio.h>

// include standard library from header file h
#include <stdlib.h>

// main function heading
main ()

// begining of instructions
{

// declare variables and set initial value to zero
float r=0,a=0,b=0;

// print on screen enter number
printf("Enter number:",&a); (this bit works ok)

// scan for input
scanf("%f",&b); (as does this)

// manipulate variables
r=(1/b); (not sure if im doing this bit right)

// if conditions
if (b==0) printf("Reciprocal undefined\n"); (this bit also works)

// otherwise do this
else printf("the reciprocal of,b,is = 1/,b,or %f\n",&r); :)():)mad:)!!!

// pause to view answer
system("PAUSE");
}
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
Using the &, you are sending the memory address of the variable r in the printf statement.

printf

Pointers are typically used for strings.

Try this:
Rich (BB code):
#include <stdio.h>

// include standard library from header file h
#include <stdlib.h>

// main function heading
main ()

        // begining of instructions
{

        // declare variables and set initial value to zero
        float r=0,b=0;

        // print on screen enter number
        printf("Enter number:"); 

        // scan for input
        scanf("%f",&b); 

        // manipulate variables
        r=(1/b); 

        // if conditions
        if (b==0) printf("Reciprocal undefined\n"); 

        // otherwise do this
        else printf("the reciprocal of %f, 1/%f is %f\n",b,b,r);
}
Code above compiled and ran on a linux system with no errors.

The float a is not needed in the first printf, and is not used elsewhere in the program.


Output:

Rich (BB code):
Enter number:34
the reciprocal of 34.000000, 1/34.000000 is 0.029412
Enter number:0
Reciprocal undefined
Enter number:0.3333333333333333333333333333
the reciprocal of 0.333333, 1/0.333333 is 3.000000
Enter number:-1293
the reciprocal of -1293.000000, 1/-1293.000000 is -0.000773
 
Last edited:
Top