c primer plus 6编程练习
1、
#include <stdio.h> #define NUM 26 int main(void) { char letters[NUM]; int i; for(i = 0; i < NUM; i++) { letters[i] = 'a' + i; } for(i = 0; i < NUM; i++) { printf("letters[%d] = %c.\n", i, letters[i]); } return 0; }
#include <stdio.h> int main(void) { const int NUM = 26; int i; char letters[NUM]; char ch; for(ch = 'a', i = 0; i < NUM; ch += 1, i++) { letters[i] = ch; } for(i = 0; i < NUM; i++) { printf("letters[%d] = %c.\n", i, letters[i]); } return 0; }
#include <stdio.h> int main(void) { char alphabet[26]; int i; char c = 'a'; for(i = 0; i < 26; i++, c++) { alphabet[i] = c; } for(i = 0; i < 26; i++) printf("index[%d]: %c\n",i, alphabet[i]); return 0; }
2、
#include <stdio.h> int main(void) { int i, j; for(i = 1; i <= 5; i++) { for(j = 1; j <= i; j++) { printf("%c", '$'); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i = 1; while(i <= 5) { int j = 1; while(j++ <= i) { printf("%c", '$'); } printf("\n"); i++; } return 0; }
#include <stdio.h> int main(void) { int i = 1; int j; while(i <= 5) { for(j = 1; j <= i; j++) { printf("%c", '$'); } printf("\n"); i++; } return 0; }
#include <stdio.h> int main(void) { int i = 1; int j; do { for (j = 1; j <= i; j++) { printf("%c", '$'); } printf("\n"); } while(++i <= 5); return 0; }
3、
#include <stdio.h> int main(void) { int i, j; for(i = 1; i <= 6; i++) { for(j = 1; j <= i; j++) { printf("%c", 'F' + 1 - j); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch; for(i = 1; i <= 6; i++) { for(ch = 'F', j = 1; j <= i; ch--, j++ ) { printf("%c", ch); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch; for(i = 1; i <= 6; i++) { ch = 'F'; for(j = 1; j <= i; j++) { printf("%c", ch--); } printf("\n"); } return 0; }
4、
#include <stdio.h> int main(void) { int i, j; char ch; for(i = 1; i <= 6; i++) { ch = 'A' + i * (i - 1) / 2; for(j =1; j <= i; j++) { printf("%c", ch++); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch = 'A'; for(i = 1; i <= 6; i++) { for(j = 1; j <= i; j++) { printf("%c", ch + j - 1); } ch += j - 1; printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch = 'A'; for(i = 1; i <= 6; i++) { for(j = 1; j <= i; ch++, j++) { printf("%c", ch); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch = 'A'; for(i = 1; i <= 6; i++) { for(j = 1; j <= i; j++) { printf("%c", ch++); } printf("\n"); } return 0; }
5、
#include <stdio.h> int main(void) { int i, j; char ch; int end; printf("please input an letter: "); scanf("%c", &ch); end = 26 - ('Z' - ch); for(i = 1; i <= end; i++) { for(j = 1; j <= end - i; j++) { printf(" "); } for(ch = 'A', j = 1; j <= i; ch++,j++) { printf("%c", ch); } for(ch = 'A' + i - 2, j = 1; j < i; ch--, j++) { printf("%c", ch); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j; char ch; int end; printf("please input an letter: "); scanf("%c", &ch); end = 26 - ('Z' - ch); for(i = 1; i <= end; i++) { for(j = 1; j <= end - i; j++) { printf(" "); } for(ch = 'A', j = 1; j <= i; ch++,j++) { printf("%c", ch); } for(ch -= 2, j = 1; j < i; ch--, j++) { printf("%c", ch); } printf("\n"); } return 0; }
#include <stdio.h> int main(void) { int i, j, end; char ch; printf("please input an upper letter: "); scanf("%c", &ch); end = ch - 'A' + 1; for(i = 1; i <= end; i++) { for(j = 1; j <= end - i; j++) { printf(" "); } for(ch = 'A'; j <= end; j++) { printf("%c", ch++); } for(ch -= 2, j = 1; j < i; j++) { printf("%c", ch--); } printf("\n"); } return 0; }
6、
#include <stdio.h> int main(void) { int start, stop; int i; printf("please input the start: "); scanf("%d", &start); printf("please input the stop: "); scanf("%d", &stop); printf(" num square cubic.\n"); for(i = start; i <= stop; i++) { printf("%5d %5d %5d.\n", i, i * i, i * i * i); } return 0; }
#include <stdio.h> int main(void) { int start, end; printf("please input the start number: "); scanf("%d", &start); printf("please input the end number: "); scanf("%d", &end); printf(" ori square cubic\n"); int i; for(i = start; i <= end; i++) { printf("%6d %10d %10d.\n", i, i * i, i * i * i); } return 0; }
7、
#include <stdio.h> #include <string.h> int main(void) { char word[120]; int len; int i; printf("please input an word: "); scanf("%s", &word); len = strlen(word); for(i = len - 1; i >= 0; i--) { printf("%c\n", word[i]); } return 0; }
#include <stdio.h> #include <string.h> int main(void) { char word[30]; int i; printf("please enter the words: "); scanf("%s", &word); i = strlen(word); for(i -= 1; i >= 0; i--) { printf("%c\n", word[i]); } return 0; }
8、
#include <stdio.h> int main(void) { float num1, num2; printf("please input two float number: "); while(scanf("%f %f", &num1, &num2)) { printf("result: %.2f.\n", (num1 - num2) / (num1 * num2)); printf("continue to input(q to quit): "); } return 0; }
9、
#include <stdio.h> double func(double x, double y); int main(void) { double num1, num2; double result; printf("please input two float nums: "); while(scanf("%lf %lf", &num1, &num2) == 2) { result = func(num1, num2); printf("result: %.2f.\n", result); printf("continue to enter: "); } return 0; } double func(double x, double y) { double output; output = (x - y) / (x * y); return output; }
10、
#include <stdio.h> int main(void) { int num1, num2; printf("please input num1: "); scanf("%d", &num1); printf("please input num2: "); scanf("%d", &num2); while(num1 < num2) { printf("the lower and upper is: %d and %d.\n", num1, num2); int i, sum = 0; for(i = num1; i <= num2; i++) { sum += i * i; } printf("the sum of the square %d to %d is %d.\n", num1 * num1, num2 * num2, sum); printf("please input num1: "); scanf("%d", &num1); printf("please input num2: "); scanf("%d", &num2); } return 0; }
11、
#include <stdio.h> #define NUM 8 int main(void) { int num[NUM]; int i; for(i = 0; i < NUM; i++) { printf("num[%d]: ", i); scanf("%d", &num[i]); } printf("-------------\n"); for(i = NUM - 1; i >= 0; i--) { printf("num[%d] = %d.\n", i, num[i]); } return 0; }
12、
#include <stdio.h> int main(void) { int length; double sum = 0.0; int i; printf("please input the length: "); scanf("%d", &length); while(length > 0) { sum = 0.0; for(i = 1; i <= length; i++) { sum = sum + 1.0/i; } printf("the sum for 1.0 + ... + 1.0/%d.0 is %lf\n", length, sum); sum = 0.0; for(i = 1; i <= length; i++) { if(i % 2 == 0) { sum = sum - 1.0/i; } else { sum = sum + 1.0/i; } } printf("the sum of 1.0 - 1.0/2.0 + ...- 1.0/%d.0 is %lf\n", length, sum); sum = 0.0; for(i = 1; i <= length; i++) { if(i % 2 != 0) { sum = sum + 2 * 1.0/i; } } printf("sum of the two sequence %d: %lf\n", length, sum); printf("length(0 to quit): "); scanf("%d", &length); } return 0; }
13、
#include <stdio.h> #define NUM 8 int main(void) { int array1[NUM]; int i, tmp = 2; for(i = 0; i < NUM; i++) { array1[i] = tmp; tmp *= 2; } i = 0; do { printf("array1[%d] = %d.\n", i, array1[i] ); i++; } while(i < NUM); return 0; }
#include <stdio.h> #define NUM 8 int main(void) { int array1[NUM]; int i; array1[0] = 2; for(i = 1; i < NUM; i++) { array1[i] = array1[i - 1] * 2; } i = 0; do { printf("array1[%d] = %d.\n", i, array1[i]); i++; } while(i < NUM); return 0; }
14、
#include <stdio.h> #define NUM 8 int main(void) { double array1[NUM], array2[NUM]; int i, j; for(i = 0; i < NUM; i++) { printf("array1[%d] = ", i); scanf("%lf", &array1[i]); } for(i = 0; i < NUM; i++) { for(j = 0; j <= i; j++) { array2[i] += array1[j]; } } printf("array1: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array1[i]); } printf("\narray2: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array2[i]); } return 0; }
#include <stdio.h> #define NUM 8 int main(void) { double array1[NUM], array2[NUM]; int i; for(i = 0; i < NUM; i++) { printf("array1[%d] = ", i); scanf("%lf", &array1[i]); } array2[0] = array1[0]; for(i = 1; i < NUM; i++) { array2[i] = array2[i - 1] + array1[i]; } printf("array1: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array1[i]); } printf("\narray2: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array2[i]); } return 0; }
#include <stdio.h> #define NUM 8 int main(void) { double array1[NUM], array2[NUM]; int i, j; for(i = 0; i < NUM; i++) { printf("array1[%d]: ", i); scanf("%lf", &array1[i]); } for(i = 0; i < NUM; i++) { array2[i] = 0; for(j = 0; j <= i; j++) { array2[i] += array1[j]; } } printf("array1: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array1[i]); } printf("\narray2: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array2[i]); } return 0; }
#include <stdio.h> #define NUM 8 int main(void) { double array1[NUM], array2[NUM]; int i; for(i = 0; i < NUM; i++) { printf("array1[%d]: ", i); scanf("%lf", &array1[i]); } array2[0] = array1[0]; for(i = 1; i < NUM; i++) { array2[i] = array2[i - 1] + array1[i]; } printf("array1: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array1[i]); } printf("\narray2: "); for(i = 0; i < NUM; i++) { printf("%5.2f ", array2[i]); } return 0; }
#include <stdio.h> int main(void) { double first[8], second[8]; int i, j; printf("enter 8 data to first array: "); for(i = 0; i < 8; i++) { scanf("%lf", &first[i]); } for(i = 0; i < 8; i++) { double sum = 0; for(j = 0; j <= i; j++) { sum = sum + first[j]; } second[i] = sum; } printf("all the data of two array:\n"); printf("first array: "); for(i = 0; i < 8; i++) { printf("%12lf.", first[i]); } printf("\nsecond array: "); for(i = 0; i < 8; i++) { printf("%12lf.", second[i]); } return 0; }
15、
#include <stdio.h> #include <string.h> int main(void) { char line[128]; int i, len; printf("please input an string: "); scanf("%s", line); len = strlen(line); for(i = len - 1; i >= 0; i--) { printf("line[%d] = %c.\n", i, line[i]); } return 0; }
#include <stdio.h> int main(void) { char data[256]; printf("enter the char in a line: "); int i = 0; do { scanf("%c", &data[i]); } while(data[i] != '\n' && ++i); printf("the reverse char of the data: "); for(i--; i >= 0; i--) { printf("%c", data[i]); } printf("\ndone!\n"); return 0; }
16、
#include <stdio.h> int main(void) { double sumdap = 100.0, sumdei = 100.0; int i; for(i = 0; sumdap >= sumdei; i++) { sumdap += 100 * 0.1; sumdei += sumdei * 0.05; } printf("year: %d.\n", i); printf("sumdap: %.2f\n", sumdap); printf("sumdei: %.2f\n", sumdei); return 0; }
#include <stdio.h> int main(void) { double sumdap = 100.0, sumdei = 100.0; int i = 0; while(sumdap >= sumdei) { sumdap += 100 * 0.1; sumdei += sumdei * 0.05; i++; } printf("year: %d.\n", i); printf("sumdap: %.2f\n", sumdap); printf("sumdei: %.2f\n", sumdei); return 0; }
#include <stdio.h> int main(void) { double sumdap = 100.0, sumdei = 100.0; int i = 0; do { sumdap += 100.0 * 0.1; sumdei += sumdei * 0.05; i++; } while(sumdei < sumdap); printf("year: %d.\n", i); printf("sumdei: %.2f\n", sumdei); printf("sumdap: %.2f\n", sumdap); return 0; }
#include <stdio.h> int main(void) { float daphne, deirdre; daphne = deirdre = 100.0; int year = 0; do { daphne = daphne + 100 * 0.1; deirdre = deirdre + deirdre * 0.05; year++; } while((deirdre - daphne) < 0); printf("%d years later. \n daphne = %f.\ndeirdre = %f.\n", year, daphne, deirdre); return 0; }
17、
#include <stdio.h> int main(void) { double sumchu = 100; int i; for(i = 0; sumchu > 0; i++) { sumchu += (sumchu * 0.08) - 10; } printf("year: %d.\n", i); printf("sumchu: %f.\n", sumchu); return 0; }
#include <stdio.h> int main(void) { double sumchu = 100; int i = 0; do { sumchu += sumchu * 0.08 - 10; i++; } while(sumchu > 0); printf("year: %d.\n", i); printf("sumchu: %f\n", sumchu); return 0; }
18、
#include <stdio.h> int main(void) { int a = 5; int i; for(i = 1; a < 150 ; i++) { a = (a - i) * 2; printf("%d week sum: %d.\n",i, a); } return 0; }
#include <stdio.h> int main(void) { int rabnud = 5; int weeks = 1; while(rabnud < 150) { printf("at %d weeks, firend: %d.\n", weeks, rabnud); rabnud = 2 * (rabnud - weeks++); } return 0; }