Starting from:
$25

$19

Homework #2 (C Programming for Beginners - OnLine)

2.1 Here is a listing of a program, which demonstrates calculating area of a circle whose radius is 2.

int radius = 2;

double area;

const double pi = 3.142;

area = pi * radius * radius;

printf("The area is: ");

printf("%5.2f\n", area);


Modify the above program and use only one print statement instead of two printf() statements to show the same output (“The area is: xx.xx”).

2.2 Following code uses scanf() to get the radius from user during run time, calculates area and displays it.

//extra line feed

printf("\n");

printf("Enter the radius: ");

scanf("%d", &radius);

area= pi * radius * radius;

printf("The area is: ");

printf("%5.2f\n", area);

Modify the above program instead of hard-coding the value of PI in the program, get the value of PI from the user as well, similar to how you got the value of the radius above

2.3 Following code demonstrate that radius could be a short type. It also displays how to get a character input and string input from user, and clean the input buffer

//extra line feed

printf("\n");

printf("Enter the radius: ");

short newRadius;

scanf("%hd", &newRadius);

area= pi * newRadius * newRadius;

printf("The area is: ");

printf("%5.2f\n", area);

//extra line feed
printf("\n");

char lastName[20];

char yourInitial;

//extra line feed

printf("\n");

printf("Please enter your last name: ");

scanf("%19s", lastName); //19 is stop user from typing long name

//scanf leaves new line character inserted by enter key

//You nee clean the input buffer by repeatedly calling getchar() //until you get the new line character. You will learn following //when you read about lops

while (getchar() != '\n')

continue;

printf("What is your first name?: ");

yourInitial = getchar(); //user may enter more than one char //but, getchar() returns only one char and leaves rest of the //characters including enter (new line '\n') character

//in the input buffer, which may effect your next scanf.

while (getchar() != '\n')

continue;

printf("Hello Mr. %c. %s\n", yourInitial, lastName);

Following code demonstrate use of printf(), and format specifiers

//extra line feed

printf("\n");

printf("5185 is fun course.\n\n");

printf("First Name
\tLast Name\tCity\n");
printf("-----------
\t---------
\t----
\n");
printf("Bill
\tClinton
\tHarlem\n");

printf("\n");

//extra line feed

printf("\n");

printf("How do you print double quotes?\n");

printf("Who said\"Test Scores Can Be Used ....\"\n");

Modify the above demo code so that you are not hard coding the name, and city (Bill Clinton, Harlem etc) but, get the values from user.

a) Add a column for zip code as well
    b) Declare four variables (decide on data type): First Name, Last Name, City, and Zip
        c) Ask user for the values for these variables and display them instead of using the hardcoded names like Bill Clinton Harlem

A sample run may look like this:



























2.4 Print a menu of choices, and ask the user to make a selection and print what selection user selected and provide a feedback. An interaction with user looks like this:

More products