IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CAdvanced Functions : Fraction Evaluation
<06.11>
#include <stdio.h> void scan_fraction(int *nump, int *denomp) { char slash; int status; int error; char discard; do { // no errors detected yet error = 0; // get a fraction from the user printf("Enter a common fraction as two integers separated "); printf("by a slash > "); status = scanf("%d %c %d",nump, &slash, denomp); // validate the fraction if (status < 3) { error = 1; printf("Invalid. Please read directions carefully\n"); } else if (slash != '/') { error = 1; printf("Invalid fraction (no slash)"); } else if (*denomp <= 0) { error = 1; printf("Invalid. Denominator must be positive\n"); } // discard extra input characters do { scanf("%c", &discard); } while (discard != '\n'); } while (error); } int main() { int numerator, denominator; double real_number; scan_fraction (&numerator, &denominator); real_number = (double)numerator / denominator; printf ("%lf", real_number); return (0); }
Hergestellt in Deutschland / Made in Germany
Enter a common fraction as two integers separated by a slash > 6/8 0.750000
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland