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,i,y=0; printf ("Enter an integer:"); scanf ("%d",&x); if(%2==0)    {         for(i=2; i<=x;i+=2) {y=y+i; }     }      Else     {          for(i=1; i<=x;i+=2) {y=y+i; }} printf ("y=%d\n", y); return 0;}

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 () {floati,y=0, x=0, G;  for(i=2; i<= -; i+=2) {y=y+1.0/i; }     for(i=1; i<= -; i+=2) {x=x+1.0/i; } g=x-y; printf ("%.2f\n", G); 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>intMain () {intI,a=0, j=0;  for(i= -; i<= the; i++)    {        if(i%4==0&&i% -!=0|| i% -==0) {printf ("%d", i); A++; J++; if(j%Ten==0) {printf ("\ n"); }}} printf ("\ n There are%d leap years \ n", a); 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 () {floatx,y=1.0; intM,i; printf ("Enter a real number and an integer:"); scanf ("%f%d",&x,&m);  for(i=1; i<=m;i++) {y=y*x; } printf ("%.2f\n", y); return 0;}

5. Enter a string of characters, respectively, to count the number of letters, spaces, numbers, and other characters.

#include <stdio.h>intMain () {intA=0, b=0, c=0, d=0; Chary; printf ("enter a string of characters:"); scanf ("%c",&y);  while(y!='\ n')    {        if(y>='a'&&y<='Z'|| y>='A'&&y<='Z') {a++; }        Else if(y>='0'&&y<='9') {b++; }        Else if(y==' ') {C++; }        Else{d++; } scanf ("%c",&y); } printf ("There are%d letters,%d digits,%d spaces, other characters%d \ n", a,b,c,d); 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 () {inta,b=0, c=0, sum1=0, sum2=0; printf ("Enter the number of batches (positive and negative, enter 0 to end):"); scanf ("%d",&a);  while(a!=0)    {        if(a>0) {sum1=sum1+A; b++; }        Else if(a<0) {sum2=sum2+A; C++; } scanf ("%d",&a); }    if(b!=0&&c!=0) {printf ("The average number of positive numbers is%.2f, and the average of negative numbers is%.2f",(float) Sum1/b, (float) sum2/c); }    Else if(b!=0&&c==0) {printf ("The average number of positive numbers is%.2f and no negative number is entered",(float) sum1/b); }        Else if(b==0&&c!=0) {printf ("The average number of negative numbers is%.2f and no positive number is entered",(float) sum2/c); }    Else{printf ("No data entered"); }    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 () {intI,m,a=0, j=0;  for(i=2; i<= +; i++)    {         for(m=2; m<=i;m++)        {            if(i%m==0) Break; }             if(i==m) {printf ("%4d", i); A++; J++; if(j%Ten==0) {printf ("\ n"); }}} printf ("\ n There are%d prime numbers \ n", a); return 0;}

8. Print the following graphic
#include <stdio.h>intMain () {inti,j;  for(i=1; i<=5; i++)    {         for(j=1; j<=i;j++) {printf (" "); }         for(j=1; j<= One-2*i;j++) {printf ("*"); } printf ("\ n"); }    return 0;}

Knowledge Point Summary: 1.break is the end of the entire loop body, continue is the end of a single cycle

2. The number of cycles is known, select the counting control loop for statement, the number of cycles is unknown, controlled by a given condition, select the conditional control of the loop while statement; the loop body must be executed at least once to select the Do...while statement.

The 3.while statement evaluates the expression first, then executes the loop body, and the loop body may not execute once. The Do...while statement executes the loop body first and then evaluates the expression.

4.++--cannot be used for constants and expressions, such as 5++ (A+B) + +. ++i=>i=i+1 first add after use I++=>i=i+1 first use after add

Experiment Summary: 1. There is no semicolon behind For,if,while, and the for statement is separated by semicolons

2. The sum of the multiplicative initial value should be one, not 0

3. Don't forget to assign an initial value

4. Think well of the structure diagram of the loop, in accordance with the diagram write program

5. Note the rotation of the apply, {} corresponding

Fourth time assignment

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.