C Programming Assignment 2

Submitted by: Submitted by

Views: 499

Words: 370

Pages: 2

Category: Science and Technology

Date Submitted: 06/27/2011 03:34 PM

Report This Essay

C Programming

Section A:

1. Ask the user to enter a digit between 0 and 9. Have the program print out the digit in words, for example:

Enter a digit between 0 and 9: 4

You entered the number four

Assume that the user will enter only a single digit. The user may accidentally enter a single character, and this should generate an error message.

#include

int main()

{

int number;

printf("Input a number 1-9: ");

scanf("%d", &number);

if (number == 0)

printf("\nYou entered the number Zero\n");

else if (number ==1)

printf("\nYou entered the number One\n");

else if (number == 2)

printf("\nYou entered the number Two\n");

else if (number == 3)

printf("\nYou entered the number Three\n");

else if (number == 4)

printf("\nYou entered the number Four\n");

else if (number == 5)

printf("\nYou entered the number Five\n");

else if (number == 6)

printf("\nYou entered the number Six\n");

else if (number == 7)

printf("\nYou entered the number Seven\n");

else if (number == 8)

printf("\nYou entered the number Eight\n");

else if (number == 9)

printf("\nYou entered the number Nine\n");

else

printf("\nSorry invalid number\n"

"\nSince I'm a simple program I wont be able to tell you the answer\n"

"\nThanks for trying\n");

return 0;

}

Section B:

2. Write a C program to determine whether the year entered from the keyboard is a leap year. Display a message indicating whether the year is or is not a leap year.

To calculate this problem, a year is a leap year if it is evenly divisible by 4 and not by 100, or if it is evenly divisible by 400. Check the remainder of the division statements by using the % function. (Hint: It may help to have three variables, each one holding the remainder of each of the division problems.)

(To check your work, legal leap years are 1952, 1964, 1988, and 2004. Years 1953, 1966, 1990, and 2007 are not leap years.)

{

int year;

printf("Enter the value of year:");...