Chapter 6 programming exercises and chapter 6 Programming
1. Write a program, create an array with 26 elements, and store 26 lowercase letters in it. And let the program
Displays the content of the array.
#include<stdio.h>#define SIZE 26int main(void){ int letter[SIZE]; int i; for(i=0;i<SIZE;i++){ letter[i] = 'a'+i; } for(i=0;i<SIZE;i++){ printf("The ASCII value for %c is %d \n",letter[i],letter[i]); } return 0;}
2. Use nested loops to generate the following patterns:
$
$
$
$
$
#include<stdio.h>int main(void){ int i,j; for(i=0;i<5;i++){ for(j=0;j<=i;j++){ printf("$"); } printf("\n"); } return 0; }
3. Use nested loops to generate the following patterns:
F
FE
FED
FEDC
FEDCB
FEDCBA
#include<stdio.h>int main(void){ char letter[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int i,j; for(i=5;i>=0;i--){ for(j=5;j>=i;j--){ printf("%c",letter[j]); } printf("\n"); } /* for(i=0;i<6;i++){ for(j=5;j>=i;j--){ printf("%c",letter[j]); } printf("\n"); } return 0; //output: FEDCBA FEDCB FEDC FED FE F*/}
4. Let the program ask the user to enter an uppercase letter and generate a pyramid pattern like the following using nested loops:
A
ABA
ABCBA
ABCDCDA
ABCDEDCBA
This pattern is extended to characters entered by the user. For example, the front pattern is generated when E is input. Tip: Use 1
External loops are used to process rows. Each row uses three internal loops, one processing space, one printing letter in ascending order, and the other processing space.
Print letters in descending order. If your system does not use ASCII or similar encoding to represent letters in strict numerical order, see
The suggestions in Exercise 3.
# Include <stdio. h> int main (void) {char letter [26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char input_let; int I, j, k; // A: 65, Z: 90; printf ("enter an uppercase letter:"); scanf ("% c", & input_let);/* for (I = 0; I <input_let-65 + 1; I ++) {for (j = 0; j <input_let-65-i; j ++) {printf ("") ;}for (j = 0; j <= I; j ++) {printf ("% c", 'A' + j);} for (k = I-1; k> = 0; k --) {printf ("% c", 'A' + k);} printf ("\ n");} */for (I = 0; I <input_let-65 + 1; I ++) {for (j = 0; j <input_let-65-i; j ++) {printf ("") ;}for (j = 0; j <= I; j ++) {printf ("% c", letter [j]);} for (k = I-1; k> = 0; k --) {printf ("% c", letter [k]);} printf ("\ n");} return 0 ;}
5. Write a program to print a table. each row of the table contains an integer, its square and its cube. Require users
The upper and lower limits of the input table. Use a for loop.
# Include <stdio. h> int main (void) {int I, n1, n2; printf ("enter a minimum value and a maximum value. The program will list their squares and cubes :"); scanf ("% d", & n1, & n2); printf ("number | square | cube \ n"); for (I = n1; I <= n2; I ++) {printf ("% d | % d \ n", I, I * I, I * I);} return 0 ;}
6. Write a program to read a word into a character array and print the word in reverse order. Tip: Use strlen () (
Chapter 4) Calculate the index of the last character in the array.
# Include <stdio. h> # include <string. h> int main (void) {char word [40]; int I, length; printf ("Enter the English word \ n"); scanf ("% s ", & word); length = strlen (word); for (I = length-1; I> = 0; I --) {printf ("% c ", word [I]);} return 0 ;}
7. Write a program that requires two floating point numbers to be input, and then print the result of dividing the difference value by the product of the two.
The program processes each pair of input values cyclically before you type a non-numeric input.
# Include <stdio. h> int main (void) {float n1, n2; printf ("enter two numbers:"); while (scanf ("% f", & n1, & n2) = 2) {printf ("max_float-min_float)/(max_float * min_float) = % f \ n", (n1-n2)/(n1 * n2 ));} return 0 ;}
8. Modify Exercise 7 to use a function to return the calculated value.
# Include <stdio. h> float calc (float n1, float n2); int main (void) {float n1, n2, result; printf ("enter two numbers :"); while (scanf ("% f", & n1, & n2) = 2) {result = calc (n1, n2); printf ("max_float-min_float) /(max_float * min_float) = % f \ n ", result);} return 0;} float calc (float n1, float n2) {return (n1-n2) /(n1 * n2 );}
9. Write a program that requires the user to enter a lower-limit integer and an upper-limit integer, and then calculate each
The sum of the square of an integer. The result is displayed. The program will prompt the user to enter the lower and upper integers and show the answer,
Until the upper limit integer entered by the user is equal to or less than the lower limit integer. The running result example should be as follows:
Enter lower and upper integer limits: 5 9
The sums of the squares from 25 to 81 is 255
Enter next set of limits: 3 25
The sums of the squares from 9 to 625 is 5520
Enter next set of limits: 5 5
Done
#include<stdio.h>int main(void){ int n1,n2,i,count; printf("Enter lower and upper integer limits:"); scanf("%d%d",&n1,&n2); while(n2>n1){ count = 0; for(i=n1;i<=n2;i++){ count += i*i; } printf("The sums of the squares from %d to %d is %d .\n",n1*n1,n2*n2,count); printf("Enter next set of limits:"); scanf("%d%d",&n1,&n2); } printf("done"); return 0;}
10. Write a program to read eight integers into an array and print them in reverse order.
#include<stdio.h>int main(void){ int i,num,num_arr[8]; printf("Enter 8 integers:"); i=0; while(i<8){ scanf("%d",&num); num_arr[i] = num; i++; } for(i=8-1;i>=0;i--){ printf("%3d",num_arr[i]); } return 0;}
11. Consider the two infinite sequences:
1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 +...
1.0-1.0/2.0 + 1.0/3.0-1.0/4.0 +...
Write a program to calculate the sum of the two sequences that are constantly changing until a certain number of times is reached. Allow the user to enter this time Interactively
Number. Look at the sum of 20, 100, and 500. Does each sequence seem to have to converge to a specific value? Tip: odd
The value of-1 is-1, and the value of-1 is 1.
# Include <stdio. h> int main (void) {int I, num; double count1, count2, count; int temp = 1; count1 = 0; count2 = 0; printf ("enter an integer:"); scanf ("% d", & num); for (I = 1; I <= num; I ++) {count1 + = 1.0/I; count2 + = 1.0/I * temp; temp * =-1; count + = count1 + count2 ;} printf ("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 +... % d = % f \ n ", num, count1); printf (" 1.0-1.0/2.0 + 1.0/3.0-1.0/4.0 +... % d = % f \ n ", num, count2); printf (" 1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 +... % d + 1.0-1.0/2.0 + 1.0/3.0-1.0/4.0 +... % d = % f ", num, num, count); return 0 ;}
12. Write a program, create an int array of eight elements, and set the elements to the first 8 power of 2, and then
Print their values. Use the for loop to set values. To change the value, use the do while loop to display the values.
#include<stdio.h>#define SIZE 8int main(void){ int i,num_arr[SIZE]; int num = 1; for(i=0;i<=SIZE;i++){ num *= 2; num_arr[i] = num; } i=0; do{ printf("%d\n",num_arr[i]); i++; }while(i<8); return 0;}