範例:華氏 攝氏 #include /* print fahrenheit-celsius table for fahr = 0, 20,..., 300 */...
TRANSCRIPT
範例:華氏攝氏59 ( 32)C F
#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
main(){
int fahr, celsius;
for(fahr = 0; fahr <= 300; fahr += 20) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius);}
}
#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
main(){
int fahr, celsius;
for(fahr = 0; fahr <= 300; fahr += 20) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius);}
}
範例:本利和求算/* CompoundInterest.c */#include <stdio.h>#include <math.h> main(){ double amount; /* amount on deposit */ double principal = 1000.0; /* starting principal */ double rate = .05; /* annual interest rate */ int year; /* year counter */
/* output table column head */ printf( "%4s%21s\n", "Year", "Amount on deposit" );
/* calculate and output amount on deposit for each of ten years */ for ( year = 1; year <= 10; year++ ) {
/* calculate new amount for specified year */ amount = principal * pow( 1.0 + rate, year );
/* output one table row */ printf( "%4d%21.2f\n", year, amount ); } /* end for */}
/* CompoundInterest.c */#include <stdio.h>#include <math.h> main(){ double amount; /* amount on deposit */ double principal = 1000.0; /* starting principal */ double rate = .05; /* annual interest rate */ int year; /* year counter */
/* output table column head */ printf( "%4s%21s\n", "Year", "Amount on deposit" );
/* calculate and output amount on deposit for each of ten years */ for ( year = 1; year <= 10; year++ ) {
/* calculate new amount for specified year */ amount = principal * pow( 1.0 + rate, year );
/* output one table row */ printf( "%4d%21.2f\n", year, amount ); } /* end for */}
本利和求算公式 a(n) = p(1 + r)n
pow(1+r, n)
Exercises:
範例#include <stdio.h>main(){ int number, numberRead;
printf("Enter a nonnegative number:"); scanf("%d", & numberRead);
number = numberRead;
while (number % 5 == 0 && number > 0) number = number / 5;
printf("Removing factor of 5 from %d, it becomes %d.\n", numberRead, number);
}
#include <stdio.h>main(){ int number, numberRead;
printf("Enter a nonnegative number:"); scanf("%d", & numberRead);
number = numberRead;
while (number % 5 == 0 && number > 0) number = number / 5;
printf("Removing factor of 5 from %d, it becomes %d.\n", numberRead, number);
}
從一非負的整數中移除因子 5