zoukankan      html  css  js  c++  java
  • Chapter 4 : Character Strings and Formatted Input/Output

    1. Write a program that asks for your first name, your last name, and then prints the names in the format last, first.

    #include <stdio.h>
    
    int main(void) {
        char firstName[40];
        char lastName[40];
    
        printf("Please enter your first name:
    ");
        scanf("%s", firstName);
        printf("Please enter your last name:
    ");
        scanf("%s", lastName);
        printf("Your name is %s %s.
    ", firstName, lastName);
    
        return 0;
    }

    4. Write a program that requests your height in inches and your name, and then displays the information in the following form:

    Dabney, you are 6.208 feet tall

    Use type float, and use / for division. If you prefer, request the height in centimeters and display it in meters.

    #include <stdio.h>
    
    int main(void) {
        float height;
        char name[30];
    
        printf("Enter your height in inches and your name please: 
    ");
        scanf("%f %s", &height, name);
        printf("%s, you are %.3f feet tall", name, height);
        return 0;
    }

    7. Write a program that sets a type double variable to 1.0/3.0 and a type float variable to 1.0/3.0. Display each result three times—once showing four digits to the right of the decimal, once showing 12 digits to the right of the decimal, and once showing 16 digits to the right of the decimal. Also have the program include float.h and display the values of FLT_DIG and DBL_DIG. Are the displayed values of 1.0/3.0 consistent with these values?

    #include <stdio.h>
    #include <float.h>
    
    int main(void) {
        double a = 1.0 / 3.0;
        float b = 1.0 / 3.0;
    
        printf("a = %.4f, b = %.4f
    ", a, b);
        printf("a = %.12f, b = %.12f
    ", a, b);
        printf("a = %.16f, b = %.16f
    ", a, b);
    
        printf("FLT_DIG: %d, DBL_DIG: %d
    ", FLT_DIG, DBL_DIG);
    
        return 0;
    }
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    Python strip()方法
    C#操作计划任务
    C# Task的用法
    C#异步编程之浅谈Task
    [深入学习C#]C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)
    [深入学习C#]C#实现多线程的方式:使用Parallel类
    详细的.Net并行编程高级教程--Parallel
    5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
    C# 并行任务——Parallel类
    C#多线程--线程池(ThreadPool)
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829260.html
Copyright © 2011-2022 走看看