1. Write the program, enter an integer x, and follow the output to the corresponding Y-value.
2. Programming for 1-1/2+1/3-1/4+1/5-... +1/99-1/100, results reserved two decimal places.
#include <stdio.h>int main () {float x=0,y=0,i,g,s;printf ("1-1/2+1/3-1/4+1/5-... +1/99-1/100, result reserved two decimal places \ n"); for (i =1;i<=99;i+=2) {x=x+1/i;} for (g=2;g<=100;g+=2) {y=y+ ( -1/g);} s=x+y;printf ("Result is%.2f", s); return 0;}
3. Output era name of all leap years from 2000 to 3000, 10 era name for each output line. Finally count the total number of leap years.
#include <stdio.h>int main () {int n=0,i,m=0;printf ("outputs the era name of all leap years from 2000 to 3000, with each output 10 era name in a line. Finally count the total number of leap years. \ n "); for (i=2000;i<=3000;i++) {if (i%4==0&&i%100!=0| | i%400==0) {printf ("%d", I), n++;m++;if (n%10==0) {printf ("\ n");}}} printf ("%d\n", i);p rintf ("There are altogether%d leap years", m); return 0;}
4. Enter a real number x and an integer m to calculate XM and do not allow the POW () function to be called.
#include <stdio.h>intMain () {intM,i; floatx,s=1.0; printf ("Enter a real number x and an integer m:\n"); scanf ("%f%d",&x,&m); for(i=1; i<=m;i++) {s=s*x; } printf ("The result is%f", s); return 0;}
5. Enter a string of characters, respectively, to count the number of letters, spaces, numbers, and other characters.
#include <stdio.h>intMain () {Charar; intword=0, space=0, num=0, other=0; printf ("enter a string of characters to count the number of letters, spaces, numbers, and other characters. \ n"); scanf ("%c",&ar); while(ar!='\ n') { if(ar>='a'&&ar<='Z'|| ar>='A'&&ar<='Z') {Word++; } Else if(ar==' ') {Space++; } Else if(ar>='0'&&ar<='9') {num++; } Else{ Other++; } scanf ("%c",&ar); } printf ("there are%d letters \ n", Word); printf ("space has%d \ n", space); printf ("number is%d \ n", num); printf ("other characters have%d \ n", other); return 0; }
6. Enter a batch number (positive and negative), enter 0 to end, calculate the average of the positive and negative values, respectively,
#include <stdio.h>intMain () {floatn,nx=0, ny=0, x=0, y=0, Sx,sy; printf ("Enter the number of batches (positive and negative), enter 0 to end, and calculate the average of the positive and negative values respectively. \ n"); scanf ("%f",&N); while(n!=0) { if(n>0) {NX=nx+N; X++; SX=nx/x; } Else{NY=ny+N; Y++; Sy=ny/y; } scanf ("%f",&N); } printf ("positive and for%f", SX); printf ("negative numbers and for%f", SY); return 0;}
、
7. Output 1000 of all primes, 10 per line, and the final output of the total number of primes. (aligned per column)
#include <stdio.h>intMain () {intA,b,c=0; printf ("output 1000 of all primes, 10 per line, and finally output a total number of primes. (aligned per column) \ n"); for(a=2; a<= +; a++) { for(b=2; b<a;b++) { if(a%b==0) Break; } if(b>=a) {printf ("%03d", a); C++; if(c%Ten==0) {printf ("\ n"); }}} printf ("\ n There's a total of%d primes", c); return 0;}
8. Print the following graphic
#include <stdio.h>intMain () {inti,g,n,m; for(i=1; i<=5; i++) { for(g=1; g<=i-1; g++) {printf (" "); } N= One-2*i; for(m=1; m<=n;m++) {printf ("*"); } printf ("\ n"); } return 0;}
Summary of Knowledge points:
1:break: Used only for loop statements and switch statements, and is typically used with if for loop statements.
Continue: Skip the remainder of this loop, and make the next loop decision directly.
2: The cumulative definition and the variable value should be 0, the multiplicative variable initial value should be 1.
The 3:for loop applies to the variable as a numerical change.
The while loop has a wide range of applications.
The Do While loop performs a loop before judging whether the loop executes and has certain usage restrictions. is similar to while.
4: Loop inline, pay attention to the logical connection between loops.
Experiment Summary:
1: Consider converting a problem into multiple loops to simplify the problem
2: Note the position of some statements that do not participate in inline loops when looping inline.
3: Take the address, semicolon and the like small detail ....
4: Judgment statement, priority to use concise.
5: Circulation conditions attention to the problem and the matching of their own ideas
Fourth time assignment