[Courses] [C] Beginner's Lesson Three: Answers to Exercises
KWMelvin
kwmelvin at intrex.net
Wed Oct 9 19:27:25 EST 2002
Greetings,
Here are my answers to the exercises from lesson 3.
I have limited myself to using only the information
presented in the lessons to date, so these answers
may not be how a professional C programmer would
solve these problems.
I found an error in these exercises:
In Exercise 3, the formula for the circumference of
a circle should be: Circumference = 2 * pi * radius
I'm sorry.
/* exerciz1.c -- output name, birthday, fave ice cream */
#include <stdio.h>
int main(void)
{
printf("My name is KWMelvin.\n");
printf("My birthday is 23 August.\n");
printf("My favorite ice cream is \"Mexican Vanilla\".\n);
return 0;
}
/* exerciz2.c -- output block 'F' 8x6 using #'s */
#include <stdio.h>
int main(void)
{
printf("######\n");
printf("##\n");
printf("##\n");
printf("######\n");
printf("##\n");
printf("##\n");
printf("##\n");
printf("##\n");
return 0;
}
/* exerciz3.c -- compute area and circumference of circle */
/* Area formula: A=pi*r^2 ------ Circumference: C=2*pi*r */
#include <stdio.h>
int main<void>
{
/* variable declarations */
float pi = 3.14159; /* rounded-off value for pi */
double radius = 6; /* diameter is 12 so radius is 6 */
double area = 0; /* initialize area to zero */
double circum = 0; /* initialize circumference to zero */
/* calculate the area */
area = pi * radius * radius;
/* calculate the circumference */
circum = 2 * pi * radius;
/* Output area and circumference of circle */
printf("The area is %f\n", area);
printf("The circumference is %f\n", circum);
return 0;
}
What changes have to made to output the area and circumference
for a 12.5 feet diameter circle?
Change the line
double radius = 6;
to
double radius = 6.25;
/* exerciz4.c -- print your name in 7x5 block letters */
#include <stdio.h>
int main(void)
{
printf("K K W W M M\n");
printf("K K W W MM MM\n");
printf("K K W W M M M\n");
printf("K W W W M W M\n");
printf("K K W W W M M\n");
printf("K K WW WW M M\n");
printf("K K W W M M\n");
ireturn 0;
}
/* exerciz5.c -- mistakes */
#include <stdio.h>
int main(void)
{
float flt = 1.0;
int num = 2;
char chr = 'a';
printf("%d\n", flt);
printf("%f\n", num);
printf("%d\n", chr);
return 0;
}
Output:
0
1.000000
97
Happy Programming!
--
K
More information about the Courses
mailing list