Download presentation
Presentation is loading. Please wait.
Published byDaniella Harrington Modified over 9 years ago
1
Introduction to Computers and Programming Class 12 Introduction to C Professor Avi Rosenfeld
2
Math Functions Math.h Library –Sqrt –Pow –Fabs (absolute value) –Sin, Cos, Tan, etc.
3
Square Root Example #include #define number 2.0 void main() { printf("The square root of %.2f is %.2f\n", number, sqrt(number)); }
4
Power Example #include void main() { float base, exponent; printf("Please type a base followed by its exponent\n"); scanf("%f%f",&base, &exponent); printf("%.2f to the power of %.2f is %.2f\n", base, exponent, pow(base, exponent)); }
5
Loops and Sqrt #include void main() { for (int i = 100; i > 0; i--) printf("The square root of %d is %.2f\n", i, sqrt(i)); }
6
Nested Loops Logic within Logic –If / Loops (while, do while, for) inside of others Power Programming Technique
7
Nested While Loops #include void main() { int count1 = 0; while (count1 < 5) { int count2 = 0; while(count2 < 5) { printf("This is iteration %d within the bigger iteration %d\n",count2, count1); count2++; } count1++; }
8
Nested For Loops #include void main() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) printf("*"); printf("\n"); }
9
Nested For Loops #2 #include void main() { for (int i = 0; i < 10; i++) { for (int j = 0; j <= i; j++) printf("%c", 'A'+j); printf("\n"); }
10
Deitel’s Example /* Fig. 4.6: fig04_06.c Calculating compound interest */ #include int main() { int year; double amount, principal = 1000.0, rate =.05; printf( "%4s%21s\n", "Year", "Amount on deposit" ); for ( year = 1; year <= 10; year++ ) { amount = principal * pow( 1.0 + rate, year ); printf( "%4d%21.2f\n", year, amount ); } return 0; } /*********************************************************** *************** * (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * ********************************************************* ****************/
11
Program Ideas GPA calculator 1-800-iloveny Diamond problems (see 4.16 in Deitel) Histograms (see 4.18) Drawing large letters (S, H, A) 4.11, 4.12, 4.13 in Deitel
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.