The fourth chapter in Stephen Prat’s C Primer Plus.
Programming exercises and their solutions:
1. Write a program that asks for the first and last name and then outputs them in the format last name, first name.
#include <stdio.h>
int main(void)
{
char name[20], surname[20];
printf("Enter your name and surname:\n");
scanf("%s %s", name, surname);
printf("Your surname is %s, %s", surname, name);
getchar();getchar();
return 0;
}
2. Write a program that asks for a name and does the following with it:
- Displays it enclosed in double quotes.
- Displays it in a field 20 characters wide, with the entire field enclosed in quotes, and the name is right-aligned with zero.
- Displays it at the left edge of a 20-character field, with the entire field enclosed in quotation marks.
- Displays it in a field that is three characters longer than the name.
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[20];
printf("Enter your name:\n");
scanf("%s", name);
printf("\"%s\"\n", name);
printf("\"%20s\"\n", name);
printf("\"%-20s\"\n", name);
printf("%*s", strlen(name) + 3, name);
getchar();getchar();
return 0;
}
3. Write a program that reads a floating point number and outputs it first in decimal and then in exponential form. Consider output in the following formats (number of exponent digits may vary on your system).
a) The output is 21.3 or 2.1e+001.
b) The input is +21.290 or 2.129E+001.
#include <stdio.h>
int main(void)
{
float num1;
printf("Enter floating-point number:");
scanf("%f", &num1);
printf("You entered: %.1f or %.1e\n", num1, num1);
printf("which is also: %+.3f or %.3E\n", num1, num1);
getchar();getchar();
return 0;
}
4. Write a program that asks for height in inches and a name, and then displays the information in the following form:
Larry, you are 6,208 feet tall.
Use the float type as well as the division operator /. If you want, you can request height in centimeters and display it in meters.
#include <stdio.h>
int main(void)
{
float cm, meters;
char name[20];
printf("Enter your name:\n");
scanf("%s", &name);
printf("Enter your height in cm:\n");
scanf("%f", &cm);
meters = cm / 100;
printf("%s, you height is %.2f meters", name, meters);
getchar();getchar();
return 0;
}
5. Write a program that queries the download speed in megabits per second and the file size in megabytes. The program should calculate the file download time. Keep in mind that in this case one byte is equal to eight bits. Use the float type as well as the division operator /. The program should output all three values (upload speed, file size, and upload time) with two digits displayed to the right of the decimal point, as in the following output:
With a download speed of 18.12 megabits per second, a file size of 2.20 megabytes
loads in 0.97 seconds(s).
#include <stdio.h>
int main(void)
{
float download_speed_MBits, filesize_MBytes, MBytes_to_Mbits, download_time;
printf("Enter your internet connection speed in Mbits (Mbs):\n", download_speed_MBits);
scanf("%f", &download_speed_MBits);
printf("Enter file size in Mbytes (MB):\n");
scanf("%f", &filesize_MBytes);
MBytes_to_Mbits = filesize_MBytes * 8;
download_time = MBytes_to_Mbits / download_speed_MBits;
printf("Your speed is %.2f Mbs, file size is:%.2f MB. \
It would be downloaded in %.2f seconds",
download_speed_MBits, filesize_MBytes, download_time);
getchar();getchar();
return 0;
}
In the above program, I specifically indicated human-readable variables, because many beginners get confused when translating bits and bytes.
6. Write a program that asks for a user’s first and last name.
Make it print the entered names on one line and the number of characters in each word on the next line. Align each number of characters to the end of the corresponding name, as shown below:
Character strings and formatted I/O 155
Ivan Petrov
4 6
Then make the program output the same information, but with
the number of characters aligned at the beginning of each word:
Ivan Petrov
4 6
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[20], surname[20];
printf("Enter your name:\n");
scanf("%s", &name);
printf("Enter your surname:\n");
scanf("%s", &surname);
printf("%s %s\n", name, surname);
printf("%*d %*d\n", strlen(name), strlen(name), strlen(surname), strlen(surname));
printf("%s %s\n", name, surname);
printf("%-*d %-*d\n", strlen(name), strlen(name), strlen(surname), strlen(surname));
getchar();getchar();
return 0;
}
7. Write a program that sets a double variable to 1.0/3.0 and a float type variable to 1.0/3.0. Display each result three times: in the first case with four digits and to the right of the decimal point, in the second case with twelve digits, and in the third case with sixteen digits. Also include the float.h header file in the program and output the FLT_DIG and DBL_DIG values. Do the entered values agree with the value 1.0/0.3?
#include <stdio.h>
#include <float.h>
int main (void)
{
float numf = 1.0/3.0;
double numd = 1.0/3.0;
printf("float: %.4f; double: %.4f \n", numf, numd);
printf("float: %.12f; double: %.12f \n", numf, numd);
printf("float: %.17f; double: %.17f \n", numf, numd);
printf("%d %d", FLT_DIG, DBL_DIG);
getchar();
return 0;
}
I deliberately displayed 17 characters at the end to show that the accuracy of double is 16 characters, and then the game goes 🙂
8) Write a program that prompts the user to enter the number of miles traveled and the number of gallons of gas used. This program should then calculate and display on the screen the number of miles driven per gallon of fuel, with one decimal place. Further, given that one gallon is approximately 3.785 liters and one mile is 1.609 kilometers, the program should convert the miles per gallon to liters per 100 kilometers (the common European measure of fuel consumption) and output the result with one decimal place. Please note that in the USA it is customary to measure mileage per unit of fuel (the higher, the better), while in Europe it is customary to measure fuel consumption per unit of distance (lower, the better). Use symbolic constants (defined with const or #define) for these two conversion factors.
#include <stdio.h>
int main(void)
{
float miles, gas;
const float km_per_mile = 1.609;
const float litre_per_gallon = 3.785;
float km, litres;
float km_per_litre;
printf("Enter number of miles and number of gas (in gallons)\n");
scanf("%f" "%f", &miles, &gas);
printf("At one gallon of gas you passed %.1f miles\n", miles/gas);
km = miles * km_per_mile;
litres = gas * litre_per_gallon;
km_per_litre = km/litres;
printf("For EU users: at one litre of gas you passed %.1f km\n", km/litres);
printf("For EU users: at 100 litres of gas you passed %.1f km", 100/km_per_litre);
getchar();getchar();
return 0;
}
I will be glad to see your comments!