C Primer Plus 4th string and format input/output programming exercises

Source: Internet
Author: User

1.

#include <stdio.h>intMainvoid){    Charfirst_name[ +]; Charlast_name[ +]; printf ("Please enter your name:"); scanf ("%s", last_name); printf ("Please enter your last name:"); scanf ("%s", first_name); printf ("%s,%s", last_name, first_name); return 0;}

2.

#include <stdio.h>#include<string.h>intMainvoid){    Charname[ +]; printf ("Please enter your first name: \ n"); scanf ("%s", name); printf ("\ "%s\" \ n", name); printf ("\ "%20s\" \ n", name); printf ("\ "%-20s\" \ n", name); printf ("\ "%*s\" \ n", strlen (name) +3, name); return 0;}

3.

#include <stdio.h>intMainvoid){    floatNumber ; printf ("Please enter a floating-point number: \ n"); scanf ("%f", &Number ); printf ("The input is%.1f or%.1e\n", number, number); printf ("The input is%+.3f or%.3e\n", number, number); return 0;}

4.

#include <stdio.h>intMainvoid){    floatfeet; Charname[ +]; printf ("Please enter your height: \ n"); scanf ("%f", &feet); printf ("Please enter your first name: \ n"); scanf ("%s", name); printf ("%s, you are%.3f m tall.\n", Name, feet/ -); return 0;}

5.

#include <stdio.h>#include<string.h>intMainvoid){    Charlname[ -]; Charfname[ -]; printf ("Please enter first name: \ n"); scanf ("%s", lname); printf ("Please enter last name: \ n"); scanf ("%s", fname); printf ("%10s%10s\n", lname, fname); printf ("%10d%10d\n", strlen (lname), strlen (fname)); printf ("%-10s%-10s\n", lname, fname); printf ("%-10d%-10d\n", strlen (lname), strlen (fname)); return 0;}

6.

#include <stdio.h>#include<float.h>intMainvoid){    DoubleN1 =1.0/3.0; floatN2 =1.0/1.R; printf ("N1 =%.4f\n", N1); printf ("N1 =%.12f\n", N1); printf ("N1 =%.16f\n", N1); printf ("N2 =%.4f\n", N2); printf ("N2 =%.12f\n", N2); printf ("N2 =%.16f\n", N2); printf ("Flt_dig =%d, Dbl_dig =%d\n", Flt_dig, Dbl_dig); return 0;}

7.

#include <stdio.h>#defineCAST1 3.785#defineCAST2 1.609intMainvoid){    floatmiles; floatgallons; printf ("Please enter the number of miles traveled: \ n"); scanf ("%f", &miles); printf ("Please enter the number of gallons of petrol consumed: \ n"); scanf ("%f", &gallons); printf ("Mileage per gallon of petrol:%.1f\n", Miles/gallons); printf ("liters per 100 km:%.1f\n", gallons * CAST1/(Miles * CAST2/ -)); return 0;}

C Primer Plus 4th string and format input/output programming exercises

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.