2. Write a program to prompt for an ASCII code value (e.g. 66), and then print the input characters.
#include <stdio.h> int main(void) { char ch; printf("Enter the ASCII:"); scanf("%d\n",&ch); printf("The character is:%c\n",ch); return 0; }
Note: char type is also an integer type, so it can be input with% d conversion description or displayed with% c conversion description.
3. Write a program to sound an alarm, and then print the following text: started by the sudden sound, Sally shot, "by the great pumpkin, what was that!"
#include <stdio.h> int main(void) { printf("\a"); printf("Startted by the sudden sound,sally Shouted,\n"); printf("\"By the Great Pumpkin,what was that!\"\n"); return 0; }
Note: \ a is an escape series, indicating an alarm. To avoid confusion, the escape series \ "should be used when printing double quote characters".
4. Write a program to read a floating-point number, print it into decimal point form first, and then exponential form. Then, if the system supports it, print it into p notation (i.e. hexadecimal notation). Output it in the following format (the actual displayed exponential digits vary from system to system):
Enter a floating- point value: 64. 25
fixed- point notation: 64. 250000
exponential notation: 6. 425000e+ 01
p notation: 0x1. 01p+ 6
#include <stdio.h> int main(void) { float a; printf("Enter a float-pointing:"); scanf("%f",&a); printf("fixed-point notation:%f\n",a); printf("exponential notation:%e\n",a); /*Exponential form*/ printf("p notation:%a\n",a); /*Hexadecimal p counting*/ return 0; }
Operation results:
Enter a float-pointing:3.14
fixed-point notation:3.140000
exponential notation:3.140000e+00
p notation:0xc.8f5c3p-2
Press any key to continue...
Note: the conversion description of exponential counting method is% e, and the conversion description of hexadecimal P counting method is% a. It is only the result of hexadecimal 0Xc.8fc53p-2. How to infer decimal 3.14? Can you give me some advice? Thank you first.
5. About 3.156 a year × 107 seconds. Write a program to prompt the user to enter the age, and then display the number of seconds corresponding to the age.
#include <stdio.h> int main(void) { int year; printf("Enter your year old:"); scanf("%d",&year); printf("There are %f seconds of your year old\n",year*3.156e7); return 0; }
Note: use% f conversion description when outputting, because 3.156e7 is a floating point number.
6.1 the mass of water molecule is about 3.0 × 10 − 23 g. 1 Quart dehydration is about 950 g. write a program to prompt the user for the number of quarts of water delivered and display the number of water molecules.
#include <stdio.h> int main(void) { int kuatuo; printf("Enter the kuatuo of water:"); scanf("%d",&kuatuo); printf("Count of the H2O is %f\n",kuatuo*950/(3.0*10e-23)); return 0; }
Note: as in the previous question, this question uses the% f conversion description. If the integer conversion description such as% d and% ld is used, the output overflow error will be caused.
7.1 inch is equivalent to 2.54 cm. Write a program to prompt the user to enter the height (/ inch), and then display the height in cm.
#include <stdio.h> int main(void) { float inch; printf("Enter the height in inch:"); scanf("%f",&inch); printf("The height in cetimeter is :%.2f",inch*2.54); return 0; }
Note:%. 2f conversion instructions retain two decimal places.
8. In the volume measurement system in the United States, 1 pint is equal to 2 cups, 1 cup is equal to 8 ounces, 1 ounce is equal to 2 tablespoons, and 1 tablespoon is equal to 3 teaspoons. Write a program to prompt the user to enter the number of cups and display the equivalent capacity in pints, ounces, tablespoons and teaspoons. Think about why floating-point type is more appropriate than integer type for this program?
#include <stdio.h> #int main(void) { float cup; printf("Enter the count of cup:"); scanf("%f",&cup); printf("There are %f pintuo in %f cup\n",cun/2,cup); printf("There are %f angsi in %f cup\n",cup*8,cup); printf("There are %f tangshao in %f cup\n",cup*16,cup); printf("There are %f chashao in %f cup\n",cup*48,cup); return 0; }
Note: because the number of cups is entered, it is necessary to divide by two when calculating pints. The integer division result will automatically default the decimal part, which may lead to inaccurate calculation results.