You are on page 1of 6

#include <stdio.

h> /* a function taking two double numbers and returning the larger number of the two */ /* this function will return a double */ double bigger (double n1, double n2) { double big; if (n1>n2) big = n1; else big = n2; return (big); }

int main (void) { double x, y, result; printf ("Enter a real number: "); scanf ("%lf", &x); printf ("Enter a real number: "); scanf ("%lf", &y); /* calling the function (2 arguments) */ result = bigger (x,y); /* printing the report */ printf ("The bigger number is %lf.\n", result); return (0); } ******************** ********** ************ #include <stdio.h> /* a void function returns nothing */ void stars2 (int n) { int i; /* a loop displaying a star at each iteration */ for (i=1; i<=n; ++i) { printf ("*");

} /* change line after each series */ printf ("\n"); } int main (void) { int a; a=10; /* the argument may be a constant, a variable or an expression */ stars2 (20); stars2 (a); stars2 (a+2); return (0); }

#include <stdio.h> /* this function will return an integer */ int factorial (int n) { int i, product; product = 1; /* initialization */ /* computes n*n-1... */ for (i=n; i>1; i=i-1) { product = product * i; } /* the value that goes out */ return (product); }

int main (void) { int a, result; printf ("Enter an integer number: "); scanf ("%d", &a); /* calling the function */ result = factorial (a);

/* printing the report */ printf ("The factorial of %d is %d.\n", a, result); return (0); }

Enter an integer number: 13 The factorial of 13 is 1932053504 #include <stdio.h> #include <math.h> /* function returns true if n is even. */ /* it returns false if n is odd */ int even (int n) { return (n%2==0); }

/* function returns true if n is prime */ int prime (int n) { int divisor, i; /* looking for a divisor. if found, it is not a prime number */ divisor = 0; /* eliminating even numbers except 2 */ if (even (n)) { if (n == 2) divisor = 0; else divisor = 2; } else { if (n == 1) divisor = 0; /* 1 is a prime number */ else /* trying to divide number by 3,5,7,... */ /* to find a divisor until we reach n-1 */ for (i = 3; i < n; i = i + 2) { if (n % i == 0) divisor = i; } }

/* if there is a divisor (not zero) then NOT prime */ return (!divisor); }

int main (void) { int x; printf ("Enter a positive integer number: "); scanf ("%d", &x); /* testing for prime and printing the report */ if (prime (x)) printf ("%d is a prime number.\n", x); else printf ("%d is not a prime number.\n", x); return (0); }

Enter a positive integer number: 59 59 is a prime number. /* The fuel tank program */ #include <stdio.h> int main (void) { double capacity, supply, pumped; /* in liters */ /* initialize capacity of tank */ capacity = 5000.0; /* ask user for initial supply already in tank */ printf ("Enter the the initial supply: "); scanf ("%lf", &supply); /* the program loops until supply falls below 10% */ while (supply > capacity * 0.10) { /* ask user for quantity removed or delivered */ printf ("\nEnter the amount delivered(+)/removed(-): "); scanf ("%lf", &pumped); supply = supply + pumped; /* test so that you don't pump more when tank is empty */ if (supply < 0.0) supply = 0.0; /* test so that you don't fill more when tank is full */ if (supply > capacity)

supply = capacity; } printf ("\nSupply below 10%% (%.2lf l remaining)\n", supply); return (0); }

/* Integer division */ #include <stdio.h> int main (void) { int a, b, c; double x, y, z, w; a = 10; b = 20; /* dividing two integers */ z = a / b; c = a / b; printf ("The value of z is %5.3lf ", z); printf ("and the value of c is %d\n", c); /* converting (casting) one operand before the division*/ x = (double)a / b; printf ("The value of x is %5.3lf\n", x); /* casting the quotient after the division*/ y = (double) (a / b); printf ("The value of y is %5.3lf\n", y); /* casting both operands before the division*/ w = (double)a / (double)b; printf ("The value of w is %5.3lf\n", w); return (0); } Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments

The value of z is 0.000 and the value of c is 0 The value of x is 0.500

The value of y is 0.000 The value of w is 0.500

You might also like