Abstract thinking in C Programming 1.9 programming exercises

Source: Internet
Author: User

Address: Workshop.

1. Temperature conversion:

#include<stdio.h>int main(){    double F,C;    while(~scanf("%f", &C))        printf("%lf\n", 9 * C * 1.0 / 5 + 32);    return 0;}   

2. length conversion:

#include<stdio.h>int main(){    double YCI, YC, M;    while(~scanf("%f", &M)){        printf("YCI:%lf\n", 1.0 / 0.0254);        printf("YC:%lf\n", 1.0 / 12);    }    return 0;}   

3. Calculate 1 + 2 + 3 + ...... + 100

#include<stdio.h>int main(){    int sum = 0;    for(int i = 1; i <= 100; i++)        sum += i;    printf("%d\n", sum);    return 0;}   

4. Calculate the Sequence Value

#include<stdio.h>int main(){    int N, i, sum;    scanf("%d", &N);    sum = 0;    i = 1;    while(i <= N)        sum += 2 * (i++) - 1;    printf("%d\n", sum);    return 0;}   

5. Enter the maximum value of an integer sequence in the specified format.

#include<stdio.h>int main(){    int max, n, i;    printf("This program finds the largest integer in a list.\n");    printf("Enter 0 to sigal the end of the list.\n");    printf("? ");    max = -1;    while(scanf("%d", &n) && n) {        if(max < n) max = n;        printf("? ");    }    printf("The largest value is %d\n", max);    return 0;}   

6. invert the input integer

#include<stdio.h>void print(int N){    printf("The reverse number is ");    while(N) {        printf("%d", N % 10);        N /= 10;    }    printf("\n");}int main(){    int N;    printf("This program reverses the digits in an integer.\n");    printf("Enter a positive integer: ");    scanf("%d", &N);    print(N);    return 0;}   

7. Search for the full number

#include<stdio.h>#include<math.h>#include<stdbool.h>bool IsPerfect(int n){    int m, sum, t;    sum = 1;    m = (int)sqrt(n);    for(int i = 2; i <= m; i++){         if(n % i == 0) {            if(i * i == n) {                sum += m;            } else {                sum += (i + n / i);            }        }    }    if(sum == n)        return true;    else        return false;}int main(){    for(int i = 2; i <= 9999; i++) {        if(IsPerfect(i))            printf("%d\n", i);    }    return 0;}   

8. decompose the prime factor according to the specified format

#include<stdio.h>#include<math.h>#include<stdbool.h>#define N 100000int isprime[N];bool prime(int n){    int i;    for(i = 2; i * i <= n; i++) {        if(n % i == 0)            return false;    }    return true;}void init(){    int i, k;    k = 0;    for(i = 2; i < 100000; i++) {        if(prime(i))            isprime[k++] = i;    }}void solve(){    int i, n, flag, k, t;    init();    printf("Enter number to be factored: ");    while(~scanf("%d", &n)) {        k = flag = 0;        if(prime(n)) {            printf("%d\n", n);        } else {            t = n;            while(isprime[k] < n) {                while(t % isprime[k] == 0) {                    if(!flag) {                        flag = 1;                        printf("%d", isprime[k]);                    } else {                        printf(" * %d", isprime[k]);                    }                    t /= isprime[k];                }                k++;            }            printf("\n");        }        printf("Enter number to be factored: ");    }}int main(){    solve();    return 0;}   

9. The floating point number is converted to an integer according to the rule.

#include<stdio.h>#include<math.h>#include<stdbool.h>void Round(float n){    int m;    m = floor(n);    if(n > 0) {        if(n - m < 0.5) {            printf("%d\n", m);        } else {            printf("%d\n", m + 1);        }    } else {        if(n - 0.5 > m) {            printf("%d\n", m + 1);        } else {            printf("%d\n", m);        }    }        }int main(){    float n;    while(~scanf("%f", &n))        Round(n);    return 0;}   

10. calculate PI using the lebbiz Formula

#include<stdio.h>#include<math.h>#include<stdbool.h>double count(int n){    int t, i;    double ans = 0.0;    t = 1;    for(i = 1; i <= n; i++) {        ans += t * 1.0 / (2 * i - 1);        t *= -1;    }    return 4 * ans;}int main(){    printf("%f\n", count(10000));    return 0;}   

11. computation PI using slice area Similarity

#include<stdio.h>#include<math.h>#include<stdbool.h>#define N 100void solve(){    int i;    double w, r, ans, x;    r = 2.0;    w = r / N;    ans = 0.0;    for(i = 1; i <= N; i++) {        x = w * (i - 0.5);        ans += sqrt(r * r - x * x) * w;    }    printf("%f\n", ans);}int main(){    solve();    return 0;}   

 

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.