C Language Program Development Classic examples of two

Source: Internet
Author: User
Tags printf

"Program 11"

Title: Classical question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month for how much?

1. Procedure analysis: Rabbit's rule for series 1,1,2,3,5,8,13,21 ....

2. Program Source code:

main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
 { printf("%12ld %12ld",f1,f2);
   if(i%2==0) printf("\n");/*控制输出,每行四个*/
   f1=f1+f2; /*前两个月加起来赋值给第三个月*/
   f2=f1+f2; /*前两个月加起来赋值给第三个月*/
 }
}

"Program 12"

Title: Determine how many primes there are between 101-200, and output all primes.

1. Procedure analysis: To determine the method of prime number: To remove 2 to sqrt (this number), if divisible, it indicates that this number is not prime, conversely is prime.

2. Program Source code:

#include "math.h"
main()
{
 int m,i,k,h=0,leap=1;
 printf("\n");
 for(m=101;m<=200;m++)
  { k=sqrt(m+1);
   for(i=2;i<=k;i++)
     if(m%i==0)
      {leap=0;break;}
   if(leap) {printf("%-4d",m);h++;
        if(h%10==0)
        printf("\n");
        }
   leap=1;
  }
 printf("\nThe total is %d",h);
}

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.