2016-10-27

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 () {intI,x,y; printf ("Please enter an integer x\n"); scanf ("%d",&x); if(%2!=0) {y=0;  for(i=1; i<=x;i+=2) {y=y+i; } printf ("%d\n", y); }        Else if(%2==0) {y=0;  for(i=2; i<=x;i+=2) {y=y+i; } printf ("%d\n", y); }        return 0;}
Summary: Attention to the discussion of the situation2. Programming 1 -1/2+1/3 -1/4+1/5 - ... +1/99 -1/100 , the result is two decimal places.  
#include <stdio.h>intMain () {inti; floatsum; Sum=0;  for(i=1; i<= -; i++)    {        if(i%2!=0) {sum=sum-(1.0/i); }        Else if(i%2==0) {sum=sum+ (1.0/i); }} printf ("and for%.2f\n", sum); return 0;}

Summary: Be aware that negative sums are reduced

3. output The era name of all leap years from year to year , and each output is a line of era name. Finally count the total number of leap years.

#include <stdio.h>intMain () {inta,b=0;  for(a= -; a<= the; a++)   {       if(a%4==0&&a% -!=0|| a% -==0) {printf ("%d", a); b++; if(b%Ten==0) {printf ("\ n"); }}} printf ("There are a total of%d leap years \ n", b);return 0;}

4. Enter a real number x and an integer mto calculate XM and do not allow the POW() function to be called.

#include <stdio.h>int  main () {   int  a,b,c,sum;   scanf ("%d%d",&a,&b);     for (sum=1, c=1; c<=b;c++)   {       sum=sum*A;   }   printf ("%d\n", sum);    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; Charch1; printf ("Please enter a string of characters \ n");  while((Ch1=getchar ())! ='\ n')    {        if((ch1>='A'&&ch1<='Z')|| ch1>='a'&&ch1<='Z') {a++; }        Else if(ch1==' ') {b++; }        Else if(ch1>='0'&&ch1<='9') {C++; }        Else{d++; }} printf ("letters, spaces, numbers, and other characters are%d,%d,%d,%d\n, respectively .", a,b,c,d); return 0;}

Summary: Ch1=getchar () in the relational operator! = The precedence is before the assignment operator = Priority High pressure heater () before the preceding

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=1, c=1, sum1,sum2; floatAverage1,average2; printf ("Please enter a batch number \ n"); scanf ("%d",&a); Sum1=0; Sum2=0;  while(a!=0)    {        if(a>0) {sum1+=A; b++; }        Else{sum2+=A; C++; } scanf ("%d",&a); }        if(B-1==0) {printf ("no positive number \ n"); }        Else{average1=(float) sum1/(b-1); printf ("positive average is%.2f\n", Average1); }            if(C-1==0) {printf ("no negative number \ n"); }        Else{average2=(float) sum2/(C-1); printf ("negative mean is%.2f\n", Average2); }        return 0;}

Summary: Note that sum must be assigned an initial value

7. output all primes within the range, each line of ten , and the final output of a total number of primes. (aligned per column)

#include <stdio.h>intMain () {intA,b,c=0;  for(a=2; a<= +; a++)    {         for(b=2; b<=a-1; b++)            if(a%b==0)                 Break; if(a==b) {printf ("%03d", B); C++; if(c%Ten==0) {printf ("\ n"); }}} printf ("There's a total of%d prime numbers \ n", c); return 0;}

8. Print the following graphic

             
#include <stdio.h>intMain () {inti,j,k;  for(i=0; i<=4; i++)    {         for(j=0; j<=i;j++) printf (" ");  for(k=1; k<=9-2*i;k++) printf ("*"); printf ("\ n"); }    return 0;}

Summary: Find out what is the first output of the law

Knowledge Point Summary: 1 Note that the cycle must have an initial value

The 2①while statement executes the loop body after the expression is first judged, and the loop body may not execute at once

The ②do while statement executes the loop body and then evaluates the expression, performing at least one loop body

3 The while after the Do and has;

4 Note the precedence of the operator to first perform a low-priority addition ()

2016-10-27

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.