# ch04 - Chapter 4 Exercises Solutions 4.1 int power(int x...

This preview shows pages 1–3. Sign up to view the full content.

Chapter 4 Exercises Solutions 4.1 int power (int x, char n) { int prod, i; prod = 1; if (x == 1 || n == 0) return 1; else { for (i = 1; i <= n; i++) prod = prod * x; } return prod; } 4.2 /* count blanks, digits, letters, newlines, and others. */ void count_char (char *c) { int blank_cnt = 0, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while (!(*c)) { if (*c == ‘ ‘) ++blank_cnt; ++digit_cnt; ++letter_cnt; else if (*c == ‘\n’) ++nl_cnt; else ++other_cnt; c++; } printf(“%10s%10s%10s%10s%10s\n\n”,“blanks”,“digits”,“letters”,“lines”,“others”); printf(%10d%10d%10d%10d%10d\n\n”,blank_cnt,digit_cnt,letter_cnt,nl_cnt, other_cnt); } 4.3 int test8 (int x) { if (!(x % 8)) return 1; else return 0; } 4.4 int gcd (int m, int n) 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
{ int i, limitj, temp; if (m == 1 || n == 1) return 1; if (m >= n) limit = n; else limit = m; temp = 1; for (i = 2; i <= limit; i++)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

ch04 - Chapter 4 Exercises Solutions 4.1 int power(int x...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online