Friday, March 4, 2011

User-Defined Functions

Write a function that takes an integer parameter m representing the month number of the year and returns the corresponding name of the month. For instance, if m = 3, the month is March. Test your program.

#include <stdio.h>
void month(int m);
void main()
{
int n;
clrscr();
printf("Enter a integer number 1 to 12:");
scanf("%d",&n);
month(n);
getch();
}
void month(int m)
{
switch(m)
{
case 1: printf("January");
break;
case 2: printf("February");
break;
case 3: printf("March");
break;
case 4: printf("April");
break;
case 5: printf("May");
break;
case 6: printf("June");
break;
case 7: printf("July");
break;
case 8: printf("August");
break;
case 9: printf("September");
break;
case 10: printf("October");
break;
case 11: printf("November");
break;
case 12: printf("December");
break;
default: printf("Wrong input!");
break;
}
}

OUTPUT
Enter a integer number 1 to 12 : 1
January

Enter a integer number 1 to 12 : 2
February

Enter a integer number 1 to 12 : 3
March

Enter a integer number 1 to 12 : 12
December

Enter a integer number 1 to 12 : 13
Wrong input!

No comments:

Post a Comment