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