Program example (beginner)

Keywords: C less

Example 1: calculate the approximate value of π with the formula π / 4 = 1-1 / 3 + 1 / 5-1 / 7 +... Until the absolute value of a term is less than 10 ^ 6.

Tips: (1) to determine the accuracy of the calculation, you can use the fabs() function with the while loop statement to determine the accuracy to exit.

(2) it is observed that the numerator remains the same, but the denominator increases by 2 each time, and the positive and negative switches.

(3) remember to multiply the result by 4.

#include<stdio.h>
#include<math.h>

void main()
{
    int s;
    float n,t,pi;
    t=1;pi=0;n=1.0;s=1;
    while(fabs(t)>1e-6)
    {
        pi=pi+t;
        n=n+2;
        s=-s;
        t=s/n;
    }
    pi=pi*4;
    printf("pi=%f\n",pi);
}

Example 2: find the first 40 numbers of Fibonacci series. This sequence has the following characteristics: the first and second numbers are 1 and 1. Starting from the third number, it is the sum of the first two numbers.

Namely:

F(1)=1    (n=1)

F(2)=1    (n=2)

F(n)=F(n-1)+F(n-2)

Method 1: (I made it myself)

#include<stdio.h>

void main()
{
    int f1,f2,n;
    n=1;
    for(f1=1;n<=40;n++)
    {
        if(n<=2)
        {
            f2=1;
            printf("%d\n",f1);
        }
        else
        {
            if(n%2==0)
                f1=f2+f1;
            else
                f2=f1+f2;
            if(f1>f2)
                printf("%d\n",f1);
            else
                printf("%d\n",f2);
        }
    }
}

Method 2: (refer to the answer)

#include<stdio.h>

void main()
{
    int f1,f2,n;
    f1=1;
    f2=1;
    for(n=1;n<=20;n++)
    {
        printf("%d\n%d\n",f1,f2);
        f1=f1+f2;
        f2=f2+f1;
    }
}

Example 3: write a program, allow to input a number m, and judge whether M is prime.

Algorithm idea: let m be divided by 2 to k. if M can be divided by any integer in 2 to k, the cycle will be terminated in advance. At this time, i must be less than or equal to k; if M cannot be divided by any integer between 2 to k, i will be added with 1 after the last cycle is completed, so i=k+1, and then the cycle will be terminated. After the cycle, judge whether the value of i is greater than or equal to k+1. If it is, it means that it has not been divided by any integer between 2 and k, so the output is "prime".

Method 1: (I made it myself)

#include<stdio.h>

void main()
{
    int i,m;
    scanf("%d",&m);
    for(i=2;i<m;i++)
    {
        if(0==m%i)
        {
            printf("%d is not a prime number\n",m);
            break;
        }
    }
    if(i==m)
        printf("%d is a prime number\n",m);
}

Method 2 (refer to the answer)

#include<stdio.h>
#include<math.h>

void main()
{
    int m,i,k;
    scanf("%d",&m);
    k = sqrt(m);
    for(i=2;i<=k;i++)
    {
        if(m%i==0)
        {
            break;
        }
    }
    if(i>k)
    {
        printf("%d is a prime number",m);
    }
    else
    {
        printf("%d is not a prime number",m);
    }
}

Example 4: print out all prime numbers between 100 and 200.

#include<stdio.h>

void main()
{
    int m,i;
    for(m=100;m<=200;m++)
    {
        for(i=2;i<m;i++)
        {
            if(0==m%i)
                break;
        }
        if(m==i)
        {
            printf("%d\n",m);
        }
    }
}

Example 5: turn the message into a password according to the following rules:

Change the letter A to e, a to e, that is, the fourth letter after that, W to a, X to B, Y to C, Z to D.

Tip: two functions:

1. Input the original text and translate it into a password.

2. Input the password and translate it into the original.

Posted by sheila on Wed, 04 Dec 2019 18:11:06 -0800