Leap year

Program to check whether the entered year is a Leap year or not..

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

int main(void)
{
int year;
printf("Enter the year: ");
scanf("%d",&year);

if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
{
printf("Year %d is a leap year",year);
}
else
{
printf("Year %d is not a leap year",year);
}
return 0;
getch();
}

Simple calculator

Program to perform imlpement simple calculator performing basic operations.

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

float add(float,float);
float sub(float,float);
float product(float,float);
float divide(float,float);

void main()
{
float n1,n2;
char sym,choice;
clrscr();
printf("Enter the operation you want to perform(Format eg :- 2+3) \n\n");
scanf("%f%c%f",&n1,&sym,&n2);
if(sym=='+')
printf("\n%f",add(n1,n2));
if(sym=='-')
printf("\n%f",sub(n1,n2));
if(sym=='*')
printf("\n%f",product(n1,n2));
if(sym=='/')
printf("%f",divide(n1,n2));
printf("\nDo you wish to continue[y/n]");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
main();
}

float add(float m1,float m2)
{
return(m1+m2);
}

float sub(float m1,float m2)
{
return(m1-m2);
}

float product(float m1,float m2)
{
return(m1*m2);
}

float divide(float m1,float m2)
{
return(m1/m2);
}

Insertion sort

Program to implement sorting on an entered array using Insertion sort method.


#include<stdio.h>
#include<conio.h>
void ins_sort(int *,int);
main()
{
int n,i,a[5000];
clrscr();
printf("\nEnter n: ");
scanf("%d",&n);
printf("\nEnter the %d array elements ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ins_sort(a,n);
printf("\nSorted array \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
return 0;
}

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

Selection sorting

Program to implement selection sort on an array.


#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[20];
void sel_sort(int *,int);
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sel_sort(a,n);
printf("\nSorted array\n-------------\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
return 0;
}

void sel_sort(int a[],int n)
{
int i,j,p,temp,k;
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;j<n;j++)
if(a[p]>a[j])
p=j;

temp=a[p];
a[p]=a[i];
a[i]=temp;
}
}

Linear search

Program to search for a key element in an entered array using Linear search [Recursively]


#include<stdio.h>
#include<conio.h>
int count=0,key,n;
main()
{
void lin_srch(int *,int);
int i,a[10];
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the array elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&key);
i=0;
lin_srch(a,i);
if(count==0)
printf("\nElement not found in the array..!");
else
printf("\n%d found in the array!! \nCount = %d",key,count);
getch();
return 0;
}

void lin_srch(int a[],int i)
{
if(i==n-1)
return;
if(a[i]==key)
{
count++;
}
lin_srch(a,i+1);
}

Bubble sort

Program to implement bubble sorting on a string.


#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,a[10],temp;
clrscr();
printf("\nEnter the no of elements : ");
scanf("%d",&n);
printf("\nEnter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorting array.... " );
getch();
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nSorted array is \n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
return 0;
}

Binary search

Program to conduct binary search on an enteres STRING...


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int n,l,h,min,flag=0,pos;
char a[10],ch;
clrscr();
printf("\nEnter the sorted string : ");gets(a);
n=strlen(a);
printf("\nEnter the character to be searched : ");
scanf("%c",&ch);
l=0;
h=n-1;
while(l<=h)
{
min=(l+h)/2;
if(a[min]==ch)
{
flag=1;
pos=min;
break;
}
else if(a[min]>ch)
h=min-1;
else
l=min+1;
}
if(flag)
printf("\nThe reqd character found in the string @ position!!",pos+1);
else
printf("\nChracter not found...! search unsuccessful");
getch();
return 0;
}