First, the contents of the experiment
1, the experimental requirements: Write a program, enter an integer x, according to the output of the corresponding Y value.
Code:
#include <stdio.h>intMain () {inti,x,y=0; printf ("Please enter the value of x: \ n"); scanf ("%d",&x); if(x<1) {printf ("error:\n"); } Else { if(%2!=0) { for(i=1; i<=x;i+=2) {y=y+i; } } Else { for(i=2; i<=x;i+=2) {y=y+i; }}} printf ("y=%d", y); return 0;}
Program Run Result:
2, the experiment request: The programming asks 1-1/2+1/3-1/4+1/5-... +1/99-1/100, the result retains two decimal places.
Code:
#include <stdio.h>intMain () {intx; floatA=1, y; Y=0; for(x=1; x<= -; x + +) { if(%2==1) {y=y+a/x; } Else{y=y-a/x; }} printf ("%.2f:\n", y); return 0;}
Program Run Result:
3, the experiment Request: output 2000 to 3000 all leap year's era name, each output 10 era name to change one line. Finally count the total number of leap years.
Code:
#include <stdio.h>intMain () {intb; b=0; for(a= -; a<= the; a++) { if(a%4==0&&a% -!=0|| a% -==0) {printf ("%5d", a); b=b+1; if(b%Ten==0) {printf ("\ n"); }}} printf ("there are%d leap years in total", B); return 0;}
Program Run Result:
4, the experiment requires: Enter a real number x and an integer m, calculate XM, not allowed to call the POW () function.
Code:
#include <stdio.h>intMain () {floatx,y=1; intM,i; printf ("Please enter an integer and a real number: \ n"); scanf ("%d%f",&m,&x); for(i=1; i<=m;i++) {y=y*x; } printf ("%f", y); return 0;}
Program Run Result:
5, the experiment requires: Enter a string of characters, respectively, the number of letters, spaces, numbers and other characters.
Code:
#include <stdio.h>intMain () {intN1,n2,n3,n4; CharC; N1=0; N2=0; N3=0; N4=0; printf ("Please enter a string of characters: \ n"); while((C=getchar ())! ='\ n') { if(c<='Z'&&c>='a'|| c<='Z'&&c>='A') {N1++; } Else if(c==' ') {n2++; } Else if(c>='0'&&c<='9') {N3++; } Else{N4++; }} printf ("Letter =%d, Space =%d, digital =%d, other =%d", N1,N2,N3,N4); return 0;}
Program Run Result:
6, the experiment requires: Enter a number of numbers (positive and negative), enter 0 end, calculate the average of the positive and negative values, respectively,
Code:
#include <stdio.h>intMain () {intx,n1=0, n2=0; floatsum1=0, sum2=0; scanf ("%d",&x); while(x!=0) { if(x>0) {sum1=sum1+x; N1++; } Else if(x<0) {sum2=sum2+x; N2++; } scanf ("%d",&x); } printf ("positive average:%.2f negative mean:%.2f", sum1/n1,sum2/n2); return 0;}
Program Run Result:
7, the experimental requirements: output 1000 of all primes, 10 per line, and finally output a total number of prime numbers. (aligned per column)
Code:
#include <stdio.h>#include<math.h>intMain () {intn=0, I,x,y,z; for(i=2; i<= +; i++) {y=1; Z=sqrt (i); for(x=2; x<=z;x++) { if(i%x==0) {y=0; Break; } } if(y==1) {n++; if(n%Ten==0) {printf ("\ n"); } Else{printf ("%4d", i); } } } return 0;}
The results of the program run:
8. Experimental requirements: Print the following graphic
Code:
#include <stdio.h>intMain () {inti,j; for(i=1; i<=5; i++) { for(j=1; j<=i-1; j + +) printf (" "); for(j=1; j<= One-2*i;j++) printf ("*"); printf ("\ n"); } return 0;}
Program Run Result:
Ii. Summary of knowledge points in this course
1. For (the cyclic variable assigns the initial value; the cyclic condition; the cyclic variable adds value)
{Circular Body Statement}① semicolon cannot be omitted; ② is applicable to cyclic problems known as cycles;
2. while (loop condition)
{The Loop body statement}① the loop body may not be executed once, and the ② loop body should have the action of making the value of the expression tend to be false, thus exiting the loop; ③ applies to cycles known to be controlled by a given condition
3. Do
{loop body statement;}
while (loop condition); ① loop body must be executed at least once
4. Break
① is generally used with an if in a looping statement; The ②break statement cannot be used in a loop statement or in any other statement other than the switch statement;
5, continue
Used in conjunction with if, can only be used in the loop statement;
Iii. Summary of the experiment
1, pay attention to the writing of semi-colon, do...while sentence in the following need to add a semicolon;
2, the open square root expression is sqrt ();
3, judge whether the expression of the character is ((C=getchar ())! = ' \ n ');
4. In the loop body, printf does not need to enlarge brackets;
5, pay attention to the conditions of each cycle statement trial.
Fourth time assignment