4.8.1
#include<stdio.h>
int main() {
char name_first[20], name_last[20];
printf("Please enter your firstname:");
scanf_s("%s", name_first, 20);
printf("Please enter your lastname:");
scanf_s("%s", name_last, 20);
printf("%s,%s", name_first, name_last);
return 0;
}
4.8.2
#include<stdio.h>
#include<string.h>
int main() {
char firstname[20], lastname[20];
int len1,len2;
printf("Please enter your firstname:");
scanf_s("%s", firstname, 20);
printf("Please enter your lastname:");
scanf_s("%s", lastname, 20);
len1 = strlen(firstname);
len2 = strlen(lastname);
printf("\"%s %s\"\n", firstname,lastname); //a
printf("\"%*s %s\"\n",20 - len2 -1, firstname, lastname); //b
printf("\"%-s %-*s\"\n", firstname, 20 - len1 - 1 , lastname); //c
printf("%*s %s\n", len1 + 3, firstname, lastname); //d
return 0;
}
4.8.3
#include<stdio.h>
int main() {
float num;
printf("Please enter a float number");
scanf_s("%f", &num);
printf("decimal point is %.1f\n",num);
printf("exponent is %.1e",num);
return 0;
}
4.8.4
//1 feet = 12 inch ; 1 inch = 2.43 cm;
#include<stdio.h>
int main() {
float height;
char name[20];
printf("Please enter your height(inch):");
scanf_s("%f", &height);
printf("Please enter your name:");
scanf_s("%s", name, 20);
printf("%s, ", name);
printf("you are %.3f feet tall", height / 12);
return 0;
}
4.8.5
#include<stdio.h>
int main() {
float download_speed, download_size;
printf("Please enter your downloading speed of file(Mb/s):");
scanf_s("%f", &download_speed);
printf("Please enter your size of file(MB):");
scanf_s("%f", &download_size);
printf("At %.2f megabits per second, a file of %.2f megabytes\n", download_speed, download_size);
printf("downloads in %.2f second", download_size * 8 / download_speed);
return 0;
}
4.8.6
#include<stdio.h>
#include<string.h>
int main() {
char firstname[20], lastname[20];
char c = ' ';
int len1, len2;
printf("Please enter your firstname:");
scanf_s("%s", firstname, 20);
printf("Please enter your lastname:");
scanf_s("%s", lastname, 20);
len1 = strlen(firstname);
len2 = strlen(lastname);
printf("\"%s %s\"\n", firstname, lastname); //a
printf("%*c\"%s %s\"\n", 20 - len2 - len1- 3, c, firstname, lastname); //b
printf("\"%-s %-s\"%8c\n", firstname, lastname, 20 - len1 -len2 -3, c); //c
printf("%*s %s\n", len1 + 3, firstname, lastname); //d
return 0;
}
4.8.7
#include<stdio.h>
#include<float.h>
int main() {
double num = 1.0 / 3.0;
printf(" %.6f\n", num);
printf(" %.12f\n", num);
printf(" %.16f\n", num);
printf(" %f\n", FLT_DIG);
printf(" %f\n", DBL_DIG);
return 0;
}
4.8.8
#include<stdio.h>
#define kilometre 1.61
#define litre 4.55
int main() {
float mile, gallon;
printf("Please enter your miles in travel(mile):");
scanf_s("%f", &mile);
printf("Please enter your expended gas(gallon):");
scanf_s("%f", &gallon);
printf("your car's efficiency is: %.2f(mile/gallon\n)", mile / gallon);
printf("your car's efficiency is: %.2f(litre/km)",gallon * litre /(mile / kilometre));
return 0;
}