Experiment One
Experimental requirements: Write the program, enter an integer x, according to the output of the corresponding Y-value.
y=
Code:
#include <stdio.h>intMain () {intx,y,a,b,sum=0; printf ("Please enter an integer \ n"); scanf ("%d",&x); if(%2==0) { for(a=2; a<=x;a=a+2) {sum=a+sum; } } Else { for(b=1; b<=x;b=b+2) {sum=b+sum; }} y=sum; printf ("%d\n", y); return 0;}
Program Run Result:
Experiment Two
Experimental requirements: Programming 1-1/2+1/3-1/4+1/5-... +1/99-1/100, the result is reserved two decimal places.
Code:
#include <stdio.h>intMain () {inti,n= -; floatsum=1; for(i=2; i<=n;i=i+1) { if(i%2!=0) {sum=sum+1.0/i; } Else{sum=sum-1.0/i; }} printf ("%.2f", sum); return 0;}
Program Run Result:
Experiment Three
Experimental requirements: Output the era name of all leap years from 2000 to 3000, with each output 10 changed by one line. Finally count the total number of leap years.
Code:
# include<stdio.h>intMain () {inti,n=0; for(i= -; i<= the; i++) { if((i%4==0&&i% -!=0)|| i% -==0) {n=n+1; printf ("%d", i); if(n%Ten==0) {printf ("\ n"); } } }
printf ("There are%d leap years \ n" in total); return 0;}
Program Run Result:
Experiment Four: Enter a real number x and an integer m, calculate XM, and do not allow the POW () function to be called.
Code:
#include <stdio.h>intMain () {floatx,p=1; intM,i; printf ("Please enter an integer and a real number \ n"); scanf ("%d,%f",&m,&x); for(i=1; i<=m;i=i+1) {p=x*p; } printf ("%.2f", p); return 0;}
Program Run Result:
Test Five
Experimental requirements: Enter a string of characters to count the number of letters, spaces, numbers, and other letters.
Code:
# include<stdio.h>intMain () {CharC1; intA=0, b=0, c=0, d=0, e=0, F; printf ("Please enter a string of characters to ' \ n ' to end \ n"); Do{scanf ("%c",&C1); if(c1>='a'&&c1<='Z') {a=a+1; Continue; } Else if(c1>='A'&&c1<='Z') {b=b+1; Continue; } Else if(c1>='0'&&c1<='9') {C=c+1; Continue; } Else if(c1==' ') {D=d+1; Continue; } Else{e=e+1; Continue; } } while(c1!='\ n'); F=a+b; printf ("A total of %d letters,%d digits,%d spaces,%d other characters \ n", f,c,d,e); return 0;}
Program Run Result:
Experiment Six
Experimental requirements: Enter a batch number (positive and negative), enter 0 to end, calculate the average of the positive and negative values respectively.
Code:
#include <stdio.h>intMain () {intA; intb=0;intC=0; floatD,e, sum1=0, sum2=0; Do{printf ("Please enter the value of a \ n"); scanf ("%d",&a); if(a>0) {b=b+1; Sum1=sum1+A; D=sum1/b; } Else{C=c+1; Sum2=sum2+A; E=sum2/C; } } while(a!=0); printf ("positive average:%.2f\n", D); printf ("negative average:%.2f\n", E); return 0;}
Program Run Result:
Experiment Seven
Experimental requirements: Enter all the prime numbers within 1000, 10 per line, and finally output a total number of primes. (aligned per column)
Code:
# include <stdio.h>intMain () {inti,a,flag=0, n=0; for(i=2; i<= +; i++) {flag=0; for(a=2; a<=i-1; a++) { if(i%a==0) {flag=1; Continue; } } if(flag!=1) {printf ("%03d", i); N=n+1; if(n%Ten==0) {printf ("\ n"); }}} printf ("There's a total of%d prime numbers \ n", N); return 0;}
Program Run Result:
Test Eight
Experimental requirements: Print the following graphic
*********
*******
*****
***
*
Code:
#include <stdio.h>intMain () {intI, J; for(i =1; I <=5; i++) { for(j=1; j<=i;j++) {printf (" "); } for(j =1; J <=2*(5-I.) +1; J + +) {printf ("*") ; } printf ("\ n") ; } return 0;}
Program Run Result:
Ii. Summary of knowledge points in this course
- For loop, while Loop and Do...while loop three loop format applications.
- The skilled use of the IF statement and the combination with three loop statements.
- The mastery of tag variables.
- The conversion between an integral constant and a real constant.
Iii. Summary of the experiment
- The sum of the added and minus sums in a format sub-sum should be summed separately and then added and reduced.
- The while back should be the condition of the end of the loop.
- To assign a value to the initial value of the loop.
- Two while statements are applied in cases where the loop count is not known, and the For statement is applied in the case of loops that know the number of loops.
Fourth time assignment