Tuesday 27 January 2015

search for a particular number in given numbers

 void main()
     {
int a[10],n,i,k=0;
clrscr();
printf("enter the numbers");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}

printf("enter the number which you want to search");


scanf("%d",&n);

for(i=0;i<10;i++)
{
  if(a[i]==n)
  k++;
}

if(k==0)
printf("the entered number is not present ");
else
printf("the entered number is=%d occurs = %d times",n,k);
getch();
     }

Monday 26 January 2015

program for producing following output



void main()

{
int i,j,k=65,g=1;
clrscr();
for(i=1;i<=8;i++)
{
for(j=7;j>=i;j--)
{
printf("%c",k++);

} for(g=2;g<=i;g++)
 {
 printf("  ");

 }

 for(j=7;j>=i;j--)
 {
 printf("%c",k--);
 }

 printf("\n");

}
getch();
}


Sunday 25 January 2015

How to add a user defined function to a library function:

Here we are going to create a function for factorial of a the given number:


Step 1:
First create the definition of the program and save it with name fact .c
 Int factorial(int num)
{
int  f=1,I;
For(i=1;i<=num;i++)
{  
            F=f*I;
}
return (f);
}


Step 2:
Compile “fact.c” file by using Alt + F9 keys (in turbo C).


step 3:
“fact.obj” file would be created which is the compiled form of “fact.c” file.


Step 4:
Then for adding your function to the library function type the code given below:
c:\> tlib math.lib + c:\ fact.obj
+ means adding c:\fact.obj file in the math library.
We can delete this file using – (minus){
c:\> tlib math.lib - c:\ fact.obj}.


Step 5:        
Create a file “fact.h” & declare prototype of fact() function like below.
int factorial (int num);
Now fact.h file containing prototype of function “factorial”.


Step 6:
Let us see how to use our newly added library function in a C program.
# include <stdio.h>
// Including our user defined function.
# include “c:\\factorial.h”    
int main ()
{
       int f,n;
        prinntf(“enter any number”);   //message to the user

        scanf(“%d”,&n);                //receiving input from user

                                      // calling function from library
       f = factorial (n);

       printf ("factorial = %d \n", f);

        return 0;
}