Creating Header
Files:
Step1:
Confirm the
Convention to be used to ‘include’ the header file.
# include
<stdio.h> (OR) #include “stdio.h”
If the First Version
is chosen then the HEADER FILE must be in the INCLUDE
DIRECTORY.
If the Second Version
is chosen then the HEADER FILE must be the SOURCE
DIRECTORY.
Step 2:
Open a NEW FILE and
write DEFINITIONs of ALL FUNCTIONS, which are
required to be part
of the Header File.
Step 3 :
Save the FILE in the
INCLUDE DIRECTORY (OR) in the SOURCE DIRECTORY.
Step 4:
Make use of the
header file is any Program wherever the need arises.
The Contents of our
Header File.
int fact(int n)
{
int x, j;
for(j = 1, x = 1; j
<= n; j++)
x = x * j;
return(x);
}
int primecheck(int x)
{
int j, flag = 0;
if(x == 0 || x == 1)
flag = 0;
else
if(x == 2)
flag = 1;
else
{
for(j = 2; j < x /
2; j++)
{
if(x % 2 == 0)
{
flag = 0;
break;
}
else
flag = 1;
}
}
if(flag == 1)
return 1;
else
return 0;
}
long int power(int x,
int y)
{
long int pow;
int k;
for(k = 1, pow = x; k
<= y; k++)
pow = pow * k;
return(pow);
}
int checkvowel(char
ch)
{
if(ch == 'a' || ch ==
'c' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' ||
ch == 'I' || ch ==
'O' || ch == 'U')
return (1);
else
return(0);
}
Save the file as
Myfunc.h
Using the CONTENTS of
the HEADER FILE
Program
#include<stdio.h>
#include "F:\Vc\\PROGHE~1.h"
void main()
{
int choice = 0, num,
a, b;
char ch;
do
{
printf("\nMain
Menu Functions.");
printf("\n1.Check
Vowel ");
printf("\n2.Prime
Check ");
printf("\n3.Power
");
printf("\n4.Factorial
");
printf("\n0.Exit
");
printf("\nEnter
Choice[0..4]: ");
scanf("%d",
&choice);
switch(choice)
{
case 1:
printf("\nEnter Any Character : ");
fflush(stdin);
scanf("%c",
&ch);
if(checkvowel(ch))
puts("It is a
Vowel.");
else
puts("It is Not
a Vowel.");
break;
case 2:
printf("\nEnter Any Value : ");
scanf("%d",
&num);
if(primecheck(num))
printf("It is
Prime Number.");
else
printf("It is
Not a Prime Number.");
break;
case 3:
printf("\nEnter Any Two Positive Values : ");
scanf("%d%d",
&a, &b);
printf("\nThe
Result is : %d", power(a, b));
break;
case 4: printf("\nEnter
Any Positive Value : ");
scanf("%d",
&num);
printf("\nThe
Factorial is : %d", fact(num));
break;
case 0:
printf("\nThank You...");
}
}while(choice >
0);
}
No comments:
Post a Comment