Fourth time assignment

Source: Internet
Author: User

1. Write the program, enter an integer x, and follow the output to the corresponding Y-value.
#include <stdio.h>intMain () {intX,y,i; CharZ;  while(1) {printf ("Please enter a positive integer \ n"); scanf ("%d",&x);  for(y=0, i=0; i<=x;i+=2)    {        if(%2==0) {y=y+i; }        Else        {            if(i==0) {y+=1; I=1; }            Else{y=y+i; }}} printf ("%d\n", y); scanf ("%c",&z); if(z=='y')    {        Continue; }    Else    {         Break; }    }}

The first experiment, the main use is the difference between continue and break.

Continue is the loop after the end loop, and break jumps out.

2. Programming for 1-1/2+1/3-1/4+1/5-... +1/99-1/100, results reserved two decimal places.
#include <stdio.h>intMain () {intX,i; floaty;  for(i=2, y=1, x= -; i<=x;i++)    {        if(i%2==0) {y=y-1/(float) I; }        Else{y=y+1/(float) I; }} printf ("%.2f", y);}

This experiment focuses on the transformation of plastic and real numbers.

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>intMain () {intYear,i;  for(year= -, i=0; year<= the; year++)    {        if(year%4==0&&year% -!=0|| year% -==0) {printf ("%d", year); I++; if(i%Ten==0) {printf ("\ n"); }}} printf ("a total of%d \ n", i); }

Note the position of the line break instruction.

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>int  main () {    int  m,i;     float x, y;    scanf ("%f%d",&x,&m);    I=0, y=1;      while (i<m)    {        y=y*x;        I+ +;    }    printf ("%f"

5. Enter a string of characters, respectively, to count the number of letters, spaces, numbers, and other characters.
#include <stdio.h>intMain () {CharA; intb,c,d,i,e; printf ("Please enter a string of characters"); D=0; I=0; b=0; C=0; E=0;  Do{scanf ("%c",&a); if(a>='0'&&a<='9') {b++; }      Else if(a>='a'&&a<='Z'|| a>='A'&&a<='Z') {C++; }     Else if(a==' ') {D++; }         Else{i++; }    } while(a!='\ n'); printf ("number%d, letter%d, space%d, character%d", b,c,d,i-1);}

Note the space in the buffer.

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 () {inta,b,c,d,e; floatM,n; b=0, c=0, d=1, e=1;  while(1) {scanf ("%d",&a); if(a==0)        {             Break; }        Else        {            if(a>0) {b=b+A; D++; }            Else{C=c+A; E++; }        }    }    if(d1==0) {printf ("no positive"); }    Else{m=(float) b/(d1); printf ("%.2f\n", M); }    if(E-1==0) {printf ("No negative numbers"); }    Else{n=(float) c/(E-1); printf ("%.2f\n", N); }    }

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 () {inti,n,flag,m=0; printf ("all primes within 1000 are listed. \ n");  for(i=2; i<= +; i++)    {         for(n=2; n<=i-1; n++)        {            if(i%n==0) {flag=1;  Break; }            Else{flag=0; }         }        if(flag==0) {printf ("%3d", i); M++; if(m%Ten==0) {printf ("\ n"); }        }    }    return 0;} 

8. Print the following graphic
#include <stdio.h>intMain () {intb;  for(a=9; a>=1; a-=2)    {         for(b=9; b>a;b--) {printf (" "); }         for(b=1; b<=a;b++) {printf ("* "); } printf ("\ n"); }}

Really, the first is the graph of the legend, too wrong to change to this, and then a look at the question ...

Ii. Summary of Knowledge points

1, the use of continue and break. The characteristics of the two statements have been mentioned above. The flexible application can realize the control of the termination of the program and the cycle.

2, display the layout "in layman's list" thought.

Iii. Summary of the experiment

1, good examining ... And do it early next time (everyone sorry)

2, pay attention to character type variable "read each" characteristic.

3, pay attention to the buffer.

Fourth time assignment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.