2/27/2009

C Program To Perform Array Of Structure



/*C Program To Perform Array Of Structure*/

#include <stdio.h>
#include <stdlib.h>

struct date
{
    int day;
    int month;
    int year;
};
int main()
{
    struct date d[10];
    int i,n;
    void call(struct date);
    void call1(int);
    void call2(struct date[] ,int);

    printf("How many date you want to print\n");
      scanf("%d",&n);
    printf("Enter the dates:\n");
      for(i=0;i<n;i++)
      scanf("%d%d%d",&d[i].day,&d[i].month,&d[i].year);

      call(d[0]);
      call1(d[0].year);
      call2(d,n);

    return 0;
}
void call2(struct date d[], int n)
{
    int i;
      printf("Displaying the dates\n");
        for(i=0;i<n;i++)
        {
            printf("%d/%d/%d\n",d[i].day,d[i].month,d[i].year);

        }
}
void call1(int year)
{
    printf("Year= %d\n",year);
}
void call(struct date x)
{
    printf("%d/%d/%d\n",x.day,x.month,x.year);
} 
Out Put:
How many date you want to print
1
Enter the dates:
23
1
2009
23/1/2009
Year= 2009
Displaying the dates
23/1/2009

C Program Array Of Structure



/*C Program Array Of Structure */

#include <stdio.h>
#include <stdlib.h>


struct date
{
    int day;
    int month;
    int year;
};
int main()
{
    struct date d[10];
    int i,n;
    printf("How many date you want to input\n");
      scanf("%d",&n);
    printf("Enter the dates:\n");
      for(i=0;i<n;i++)
      scanf("%d%d%d",&d[i].day,&d[i].month,&d[i].year);

      printf("Printing the dates\n");
        for(i=0;i<n;i++)
        {
            printf("%d/%d/%d\n",d[i].day,d[i].month,d[i].year);

        }
    return 0;
}
Out Put:
How many date you want to input
3
Enter the dates:
1 3 2009
3 1 2008
4 2 2007
Printing the dates
1/3/2009
3/1/2008
4/2/2007

C Structures



Structures:
One of the unique features of the C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handling a group of logically related data items. It is a user defied data type with a template that serves to define its data properties.

Syntax:
        <tag_name>
        struct struct_name{
           ---------- //members of a structure
           ---------- //members of a structure       
           ---------- //members of a structure

         };

/*C Structures */

#include <stdio.h>
#include <stdlib.h>


int main()
{
    struct sample
{
    int x;
    float y;
    char z;
};
    struct sample v1,v2;

    v1.x=4;
    v1.y=4.65;
    v1.z='U';

    printf("%d\n%.2f\n%c\n",v1.x,v1.y,v1.z);
    printf("Enter an integer, float and a char\n");
      scanf("%d%f%s",&v2.x,&v2.y,&v2.z);
    printf("%d\n%.2f\n%c\n",v2.x,v2.y,v2.z);

    return 0;
}
Out Put:
4
4.65
U
Enter an integer, float and a char
3
1.6
l
3
1.60
l

2/25/2009

Different Methods Of Calling Functions In C Programming Language

Methods of calling funcctions in C:
===================================
 1) Call by value:
 =================
    In call by value, only the value will be passed.If any modification is done for the formal parameters within a function it does not reflect on the actual parameters.

 2) Call by reference:
 =====================
    In pass by reference both, value and adddress of the variable will be passed. If any modification is done by a formal parameters within a function, it reflects on actual parameters.

 3) Call by value_Result
 =======================
 
 4) Call by result
 =================

 5) Call by constatnt value
 ==========================
    

/* 1. Call by value*/
#include <stdio.h>
 
/* function declaration */
void swap(int x, int y);
/* function definition to swap the values */
void swap(int x, int y)
{
   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put temp into y */
  
   return;
}
 
int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );
 
   /* calling a function to swap the values */
   swap(a, b);
 
   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );
   printf("Did not swap. ");
 
   return 0;
}

/* 2. Call by reference*/
#include <stdio.h>
#include <conio.h>


/* function definition to swap the values */
void swap(int *a, int *b)
{
   int temp;

   temp = *a; /* save the value of x */
   *a = *b;    /* put y into x */
   *b = temp; /* put temp into y */

   return;
}

int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values */
   swap(&a, &b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

/* 3. Call by value_result*/

#include <stdio.h>
#include <conio.h>

/* function declaration */
int area(int l, int w);
 
/* function definition to calculate the area */
int area(int l, int w)
{
   return(l*w);
}
 

int main ()
{
   /* local variable definition */
   int l = 12;
   int w = 5;
 
   printf("area : %d\n", area(l,w) );
   
   return 0;
}

/* 4. Call by result*/

#include <stdio.h>
#include <conio.h>

int a=3,b=5;
/* function definition to calculate the sum */
int sum()
{
   return(a+b);
}
 

int main ()
{
   /* local variable definition */
   int result;
   
   result=sum();

   printf("Sum : %d\n", result );
  
   return 0;
}

/* 5. Call by constatnt value*/

#include <stdio.h>
#include <conio.h>

/* function definition to calculate the area */
float area(const float pi, float r)
{
   return(pi*r*r);
}
 

int main ()
{
   /* local variable definition */
   const float pi=3.14;
   float r=2.5;
 
   printf("Area : %.2f\n", area(pi,r) );
  
   return 0;
}

2/24/2009

C Program To Concatenate Two Strings Without Using Inbuilt Functions


/* C Program To Concatenate Two Strings Without Using Inbuilt Functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
    char str1[10],str2[10],str3[20];
    int i,j;
    clrscr();
       printf("Enter the first string\n");
            scanf("%s",&str1);
       printf("Enter the second string\n");
            scanf("%s",&str2);
           for(i=0;str1[i]!=NULL;i++)
           {
               str3[i]=str1[i];
           }
           str3[i]=' ';
           for(j=0;str2[i]!=NULL;j++)
           {
               str3[i+j+1]=str2[j];
           }
           str3[i+j+1]='\0';
           printf("Resultant strings are %s\n",str3);
           getch();
}
Out Put:
C Program To Concatenate Two Strings Without Using Inbuilt Functions

2/23/2009

C Program To Perform Merge_Sort On An Array


/* C Program To Perform Merge_Sort On An Array */
#include<stdio.h>
#include<conio.h>
void simple_merge(int a[],int low,int mid,int high)
{
 int i=low,j=mid+1,k=low,c[10];

  while(i<=mid&&j<=high)
    {
       if(a[i]<a[j])
    {
     c[k]=a[i];
     i=i+1;
     k=k+1;
    }
       else
    {
      c[k]=a[j];
      j=j+1;
      k=k+1;
    }
    }

  while(i<=mid)
   {
   c[k++]=a[i++];
   }

  while(j<=high)
   {
     c[k++]=a[j++];
   }
  for(i=low;i<=high;i++)
   {
     a[i]=c[i];
   }
}

void merge_sort(int a[],int low,int high)
{
       int mid;

       if(low<high)
 {
    mid=(low+high)/2;
    merge_sort(a,low,mid);
    merge_sort(a,mid+1,high);
    simple_merge(a,low,mid,high);
 }
}

void main()
{
   int a[10],n,i;
   clrscr();
    printf("Enter the number of element\n");
  scanf("%d",&n);
    printf("Enter the elements \n");
    for(i=0;i<n;i++)
  scanf("%d",&a[i]);

  merge_sort(a,0,n-1);

  printf("Sorted array is\n");
  for(i=0;i<n;i++)
  printf("%d ",a[i]);
   getch();
}
Out Put:
C Program To Perform Merge_Sort On Array

C Program To Perform Insertion, Deletion And Display Operations On Array


/* C Program To Perform Insertion, Deletion And Display Operations On Array */
#include<stdio.h>
#include<conio.h>
void main()
{
  int i,n,a[10],loc,ele,choice,temp;
  clrscr();
 printf("Enter the size of array\n");
  scanf("%d",&n);
 printf("Enter the elements\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 printf("Entered array is\n");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);

 for(;;)
  {
  printf("\n1:Insertion  2:Deletion 3:Dispaly 4:Exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);

  switch(choice)
   {
    case 1:
    printf("Enter the location and the elements\n");
     scanf("%d%d",&loc,&ele);
    loc=loc-1;

    for(i=0;i<n;i++)
     {
        if(i==loc)
      a[i]=ele;
     }
    printf("%d is inserted at %d\n",ele,loc+1);
    break;
    case 2:
    printf("Enter the location of element to be deleted\n");
     scanf("%d",&loc);
    if(n<loc)
     {
       printf("There is no element in that location\n");
       break;
     }
    loc=loc-1;
    temp=a[loc];

    if(loc==n)
     {
       n--;
       printf("Deleted element is %d\n",temp);
       break;
     }
    else if(loc<n)
     {
     for(i=loc;i<n;i++)
      {
        a[i]=a[i+1];
      }
     }
    n--;
    printf("Deleted element is %d\n",temp);
    break;
    case 3:
    if(n<=0)
     printf("Error: the array is empty\n");
     printf("Array is \n");
     for(i=0;i<n;i++)
      printf("%d\t",a[i]);
     printf("\n");
     break;
    case 4:
    exit(0);
    break;
    default:
    printf("Invalid choice\n");
    break;
   }
  getch();
  }
}
Out Put:
C Program To Perform Insertion, Deletion And Display Operations On Array

C Program To Perform Upper Bound And Lower Bound Matrix


/* C Program To Perform Upper Bound And Lower Bound Matrix */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],u[10][10],l[10][10],i,j,m,n;
clrscr();
 printf("Enter the order of the matrix\n");
  scanf("%d%d",&m,&n);
 if(m==n)
 {
 printf("Enter the element of the matrix\n");
 for(i=0;i<m;i++)
  {
  for(j=0;j<m;j++)
   {
   scanf("%d",&a[i][j]);
   }
  }

 for(i=0;i<m;i++)
  {
  for(j=0;j<m;j++)
   {
   u[i][j]=a[i][j];
   l[i][j]=a[i][j];
   }
  }

 printf("Upper bound traingularised matrix\n");
 for(i=0;i<m;i++)
  {
  for(j=0;j<m;j++)
   {
   if(i>j)
    {
    u[i][j]=0;
    }
   printf("%d ",u[i][j]);
   }
  printf("\n");
  }

 printf("Lower bound traingularised matrix\n");
 for(i=0;i<m;i++)
  {
  for(j=0;j<m;j++)
   {
   if(i<j)
    {
    l[i][j]=0;
    }
   printf("%d ",l[i][j]);
   }
  printf("\n");
  }
 }
 else
  printf("The orders are not equal\n");
 getch();
}
Out Put:
C Program To Perform Upper Bound And Lower Bound Matrix

C Program To Perform Merging Of Two Arrays


/* C Program To Perform Merging Of Two Arrays */
#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,n,m,a[5],b[5],c[10];
  clrscr();
 printf("Enter the size of an array A\n");
  scanf("%d",&m);
 printf("Enter the element of the array A\n");
 for(i=0;i<m;i++)
  scanf("%d",&a[i]);
 printf("Enter the size of an array B\n");
  scanf("%d",&n);
 printf("Enter the element of the array B\n");
 for(i=0;i<n;i++)
  scanf("%d",&b[i]);
 printf("Array A is\n");
 for(i=0;i<m;i++)
  printf("%d ",a[i]);
    printf("\n");
 printf("Array B is\n");
 for(i=0;i<n;i++)
  printf("%d ",b[i]);
    printf("\n");
 //formulla part
 for(i=0;i<m;i++)
  c[i]=a[i];
  i=m;
  j=0;

   while(i<(m+n))
    {
       c[i]=b[j];
       j++;
       i++;
    }
  printf("The merged array is\n");
  for(i=0;i<(m+n);i++)
   printf("%d ",c[i]);
       printf("\n");

getch();
}
Out Put:
C Program To Perform Merging Of Two Arrays

C Program To Perform Binary Search


/* C Program To Perform Binary Search */
#include<stdio.h>
#include<conio.h>
int a[20],ele;
void binary(int low,int high)
{
 int mid;
 mid=(low+high)/2;

 if(mid==low && mid==high && a[mid]!=ele)
  printf("%d could not be found in the array\n",ele);
 else if(ele<a[mid])
  {
    high=mid;
    binary(low,high);
  }
 else if(ele==a[mid])
  {
    printf("%d is present at position %d",ele,mid);
  }
 else if(ele>a[mid])
  {
    low=mid+1;
    binary(low,high);
  }
}
void main()
{
 int i,j,temp,n;
 clrscr();
  printf("Enter the number of elements\n");
   scanf("%d",&n);
  printf("Enter the elements of the array\n");
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);

  for(i=0;i<n;i++)
   {
   for(j=0;j<n;j++)
    {
    if(a[j]>a[j+1])
     {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
     }
    }
   }

  printf("Sorted array is \n");
  for(i=1;i<=n;i++)
   printf("%d\n",a[i]);
  printf("Enter the element to be searched\n");
   scanf("%d",&ele);
  binary(0,n-1);
 getch();
}
Out Put:
C Program To Perform Binary Search

C Program To Perform Bubble Sort

First Program:
/* C Program For Sorting Given Set Of Numbers Using Bubble Sort Technique */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],i,n,temp,j;

 printf("Enter the size of an array:\n");
  scanf("%d",&n);
 printf("Enter the elements to an array:\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);

 for(i=0;i<n;i++)
  {
  for(j=0;j<n-1;j++)
   {
   if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
   }
  }
 printf("Sorted array elements are:\n");
 for(i=0;i<n;i++)
  printf("a[%d]=%d \n",i,a[i]);

}
Out Put:

Enter the size of an array:
5
Enter the elements to an array:
20
60
50
10
40
Sorted array elements are:
a[0]=10
a[1]=20
a[2]=40
a[3]=50
a[4]=60



Second Program:

/* C Program To Perform Bubble Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],i,n,temp,j;
 clrscr();

 printf("Enter the size of an array\n");
  scanf("%d",&n);
 printf("Enter the element to an array\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);

 for(i=0;i<n;i++)
  {
  for(j=0;j<n-1;j++)
   {
   if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
   }
  }
 printf("Sorted element are\n");
 for(i=0;i<n;i++)
  printf("%d ",a[i]);
getch();
}
Out Put:
C Program To Perform Bubble Sort

C Program To Find An Element In A Given Array With Its Position Using Linear Search Technique (Second Method)


/* C Program To Find An Element In A Given Array With Its Position Using Linear Search Technique (Second Method)*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,j=0,key,flag=0,a[10];
 clrscr();
  printf("Enter the number of element\n");
   scanf("%d",&n);
  printf("Enter the values\n");
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  printf("Enter the key value\n");
   scanf("%d",&key);

  for(i=0;i<n;i++)
   if(a[i]==key)
    {
      printf("%d is present at position %d\n",key,i+1);
      j++;
      flag=1;
    }
  if(flag)
   {
   printf("%d exist %d times\n",key,j);

   }
  else
   printf("%d is not found\n",key);
 getch();
}
Out Put:
C Program To Find An Element In A Given Array With Its Position Using Linear Search Technique (Second Method)

C Program To Find An Element In A Given Array Using Linear Search Technique


/* C Program To Find An Element In A Given Array Using Linear Search Technique */
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,n,key,flag=0,a[10];
 clrscr();
  printf("Enter the number of element\n");
   scanf("%d",&n);
  printf("Enter the values\n");
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
  printf("Enter the key value\n");
   scanf("%d",&key);

  for(i=0;i<n;i++)
   if(a[i]==key)
    {
      flag=1;
      break;
    }
  if(flag)
   printf("%d Found at position=%d\n",key,i+1);
  else
   printf("%d Not found\n",key);
 getch();
}
Out Put:
C Program To Find An Element In A Given Array Using Linear Search Technique

C Program To Perform Insert, Delete And Display The Elements Using Circular Queue


/* C Program To Perform Insert, Delete And Display The Elements Using Circular Queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define queue_size 4
int item,q[20],f=0,r=-1,count=0;
void insertion();
void deletion();
void display();
void main()
{
 int choice;
 clrscr();
 for(;;)
 {
  printf("1:Insert 2:Delete 3:display 4:exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    printf("Enter the item to be inserted\n");
     scanf("%d",&item);
    insertion();
    break;
   case 2:
    deletion();
    break;
   case 3:
    display();
    break;
   default:
    exit(0);
  }
 getch();
 }
}

void insertion()
{
 if(count==queue_size)
  {
  printf("Queue Overflow\n");
  return;
  }
 r=(r+1)%queue_size;
 q[r]=item;
 count++;
}

void deletion()
{
 if(count==0)
  {
  printf("Queue is underflow\n");
  return;
  }
 printf("Deleted item=%d\n",q[f]);
 f=(f+1)%queue_size;
 count--;
}

void display()
{
 int i,j;
 if(count==0)
  {
  printf("Queue is Empty\n");
  return;
  }
 else
 {
 i=f;
 printf("Content of the circular queue\n");
 for(j=1;j<=count;j++)
  {
  printf("%d\n",q[i]);
  i=(i+1)%queue_size;
  }
 printf("%d\n");
 }
}
Out Put:
C Program To Perform Insert, Delete And Display The Elements Using Circular Queue

C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue


/* C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define queue_size 3
void insertion();
void deletion();
void display();
int element,q[10],f=0,r=-1;
void main()
{
 int choice;
 clrscr();
 for(;;)
 {
  printf("1:Insert 2:Delete 3:Display 4:Exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    printf("Enter the element to be inserted\n");
     scanf("%d",&element);
    insertion();
    break;
   case 2:
    deletion();
    break;
   case 3:
    display();
    break;
   default:
    exit(0);
  }
 getch();
 }
}

void insertion()
{
 if(r==queue_size-1)
  {
   printf("Overflow\n");
   return;
  }
 r=r+1;
 q[r]=element;
}
void deletion()
{
 if(f>r)
  {
   printf("Underflow\n");
   return;
  }
 printf("Deleted element is=%d\n",q[f]);
 f=f+1;
 if(f>r)
  {
    f=0;
    r=-1;
  }
}
void display()
{
 int i;
 if(f>r)
  {
   printf("Queue is empty\n");
   return;
  }
 printf("contents of Queue are\n");
 for(i=f;i<=r;i++)
  printf("%d\n",q[i]);
}
Out Put:
C Program to Perform Insert(), Delete() And Display() Operation On Linear Queue

C Program To Perform Push(), Pop() and Display() Operation On The Stack


/*C Program To Perform Push(), Pop() and Display() Operation On The Stack */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define stack_size 4
int item,s[10],top=-1;
void push();
int pop();
void display();
void main()
{
int item_deleted,ch;
 clrscr();
 for(; ;)
  {
   printf("1:push 2:pop 3:display 4:exit\n");
   printf("Enter your choice\n");
    scanf("%d",&ch);
   switch(ch)
    {
    case 1:
          printf("Enter the item to be inserted\n");
     scanf("%d",&item);
     push();
     break;
    case 2:
     item_deleted=pop();
     if(item_deleted==0)
      printf("Underflow\n");
     else
      printf("Item deleted=%d\n",item_deleted);
      break;
    case 3:
     display();
     break;
    default:
     exit(0);
    }
   getch();
  }
}

void push()
{
 if(top==stack_size-1)
  {
   printf("Stack overflow\n");
   return;
  }
 top=top+1;
 s[top]=item;
}

int pop()
{
 int item_deleted;
  if(top==-1)
   return 0;
  item_deleted=s[top--];
  return item_deleted;
}

void display()
{
 int i;
  if(top==-1)
   {
   printf("Stack is empty\n");
   return;
   }
  printf("Contents of the stack are\n");
  for(i=0;i<=top;i++)
   {
   printf("%d\n",s[i]);
   }
}
Out Put:
C Program To Perform Push(), Pop() and Display() Operation On The Stack

2/22/2009

C Program To Read And Write The Information Of An Employee Using Files


/* C Program To Read And Write The Information Of An Employee Using Files */
#include<stdio.h>
#include<conio.h>

void main()
{
    char ch;
    FILE *fp=fopen("C:\\Input.txt","r");
    if(fp==NULL)
    {
        printf("Can not find the file!");
        exit(0);
    }
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF) break;
        printf("%c",ch);
    }
    fclose(fp);
 }
Out Put:

Employee ID     Name            Salary          Department
-----------------------------------------------------------
100             Mohammad        7000($)         Management
200             Kamran          6500($)         Accounting
300             Keyvan          4000($)         Sales

C Program To Accept Employees Information And Display The Same Information Using Structure


/* C Program To Accept Employees Information And Display The Same Information Using Structure */
#include<stdio.h>
#include<conio.h>
struct employees
{
    int id,salary;
    char name[20],deptartment[20];
};
void main()
{
    struct employees emp[10];
    int i,n;
     printf("Enter the number of employees record:\n");
        scanf("%d", &n);
     printf("Enter the Employee information in the following format:\n");
     printf("-------------------------------------------------------\n");

    for(i=1;i<=n;i++)
    {
        printf("Enter the details of [%d] employee\n",i);
          printf("Employee ID:\n");
           scanf("%d",&emp[i].id);
          printf("Employee Name:\n");
           scanf("%s",&emp[i].name);
          printf("Salary:\n");
           scanf("%d",&emp[i].salary);
          printf("Department:\n");
           scanf("%s",&emp[i].deptartment);

    }
    printf("Employees details are as below:\n");
    printf("--------------------------------\n");
    printf("Employee ID\tName\t\tSalary\t\tDepartment \n");
    printf("-----------------------------------------------------------\n");
    for(i=1;i<=n;i++)
    {
        printf("%d\t\t%s\t\t%d($)\t%s\n",emp[i].id,emp[i].name,emp[i].salary,emp[i].deptartment);
    }

 }
Out Put:
Enter the number of employees record:
2
Enter the Employee information in the following format:
-------------------------------------------------------
Enter the details of [1] employee
Employee ID:
100
Employee Name:
Kamran
Salary:
65000
Department:
Management
Enter the details of [2] employee
Employee ID:
200
Employee Name:
Keyvan
Salary:
40000
Department:
Sales
Employees details are as below:
--------------------------------
Employee ID     Name            Salary          Department
-----------------------------------------------------------
100             Kamran          65000($)        Management
200             Keyvan          40000($)        Sales

C Program To Check Whether The Given Number Is A Fibonacci Number Or Not


/* C Program To Check Whether The Given Number Is A Fibonacci Number Or Not */
#include<stdio.h>
#include<conio.h>
void main()
{
 int f1,f2,f3,next,num;
 printf("Enter the number to be determined:\n");
 scanf("%d", &num);
 if((num==0)||(num==1))
   printf("%d is a Fibonacci term",num);
 else
 {
   f1=0;
   f2=1;
   f3=f1+f2;
   while(f3<num)
   {
     f1=f2;
     f2=f3;
     f3=f1+f2;
   }
   if(f3==num)
     printf("%d is a Fibonacci term",num);
   else
     printf("%d is not a Fibonacci term",num);
 }

}
Out Put:
Enter the number to be determined:
13
13 is a Fibonacci term

C Program To Find Second Largest Number In A Given Set Of Elements


/* C Program To Find Second Largest Number In A Given Set Of Elements */
#include<stdio.h>
#include<conio.h>
#include<math.h>


int main()
{
   int a[50],size,i,j=0,large,slarge;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter the elements one by one:\n");
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  large=a[0];
  for(i=1;i<size;i++){
      if(large<a[i]){
           large=a[i];
           j = i;
      }
  }

  slarge=a[size-j-1];
  for(i=1;i<size;i++){
      if(slarge <a[i] && j != i)
          slarge =a[i];


    }
        printf("Second largest: %d", slarge);
        return 0;
}
Out Put:
Enter the size of the array: 8
Enter the elements one by one:
5
9
4
8
2
1
3
6
Second largest: 8

C Program To Find The Largest And Smallest Numbers In A Given Set Of Numbers


/* C Program To Find The Largest And Smallest Numbers In A Given Set Of Numbers */
#include<stdio.h>
#include<conio.h>
#include<math.h>


int main()
{
   int n,i,large,small,a[20];
     printf("Enter a number of elements:\n");
         scanf("%d", &n);
         if(n>0)
         {
           printf("Enter the elements one by one:\n");
            for(i=0;i<n;i++)
            {
              scanf("%d", &a[i]);
            }
            i=0;
            large=a[i];
            small=a[i];
            for(i=0;i<n;i++)
            {
             if(a[i]>large)
             {
                 large=a[i];
             }
             else if(a[i]<small)
             {
                 small=a[i];
             }
         }
          printf("Largest number: %d\n",large);
          printf("Smallest number: %d\n",small);
         }

   return 0;
}
Out Put:
Enter a number of elements:
8
Enter the elements one by one:
8
65
42
98
25
1
34
3
Largest number: 98
Smallest number: 1

C Program To Convert Decimal Number Into Binary Number


/* C Program To Convert Decimal Number Into Binary Number */
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
   int n,rem, i=1, binary=0,m;
     printf("Enter a decimal number:\n");
         scanf("%d", &n);
       m=n;
        while (n!=0)
        {
           rem=n%2;
           n/=2;
           binary+=rem*i;
           i*=10;
        }
       printf("Binary equivalent of %d is: %d", m, binary);

   return 0;
}
Out Put:
Enter a decimal number:
37
Binary equivalent of 37 is: 100101

C Program To Convert Binary Number Into Decimal Number


/* C Program To Convert Binary Number Into Decimal Number */
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
   int n,decimal=0, i=0, rem,m;
     printf("Enter a binary number:\n");
         scanf("%d", &n);
       m=n;
        while (n!=0)
         {
          rem = n%10;
          n/=10;
          decimal += rem*pow(2,i);
          ++i;
         }
       printf("Decimal equivalent of %d is: %d", m, decimal);

   return 0;
}
Out Put:
Enter a binary number:
100101
Decimal equivalent of 100101 is: 37

C Program To Print Fibonacci Series



/*C Program To Print Fibonacci Series */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int f1=1,f2=1,f3,i=1,n,sum=0;
    printf("Enter the value:\n");
        scanf("%d",&n);
    printf("----------------\n");
     do{
        printf("%d\n",f1);
        sum=sum+f1;
        f3=f2+f1;
        f1=f2;
        f2=f3;
        i++;
     }while(i<=n);
      printf("Sum Of The Series = %d\n",sum);
    return 0;
}
Out Put:
Enter the value:
6
----------------
1
1
2
3
5
8
Sum Of The Series = 20

C Program To Find The Square Root Of A Given Number



/*C Program To Find The Square root of a given number*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i;
    printf("Enter the value: \n");
        scanf("%d",&n);
      for(i=1;i<=n/2;i++)
        if(i*i==n)
          printf("Square root of a given number is = %d\n",i);
    return 0;
}
Out Put:
Enter the value:
49
Square root of a given number is = 7

C Program To Print Prime Numbers Up To n



/*C Program To Print Prime Numbers Up To n*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count,n;
    printf("Enter the value:\n");
    scanf("%d",&n);

    printf("Prime numbers up to %d are: \n",n);
    printf("---------------------------\n");
    for(num = 1;num<=n;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        if(count==0 && num!= 1)
             printf("%d \n",num);
    }


   return 0;

}
Out Put:
Enter the value:
11
Prime numbers up to 11 are:
---------------------------
2
3
5
7
11

C Program To Check Whether The Given Number Is Prime Number Or Not



/*C Program To Check Whether The Given Number Is Prime Number Or Not*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,count=0;
    printf("Enter the value:\n");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}
Out Put:
Enter the value:
2
2 is a prime number