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;
}

Binary search

Program to search for a key element in an entered array using Binary search technique.


#include<stdio.h>
#include<conio.h>
#include<string.h>
char ch;
main()
{
int bin_srch(char*,int,int);
int n,l,h,flag;
char a[10];
clrscr();
printf("\nEnter the string :");gets(a);
printf("\nEnter the character to be searched : ");
scanf("%c",&ch);
n=strlen(a);
l=0;h=n-1;
flag=bin_srch(a,l,h);
if(flag)
printf("\nCharacter %c found in the string!!",ch);
else
printf("\nSearch unsuccessful!! ");
getch();
return 0;
}

int bin_srch(char a[],int l,int h)
{
int min,flag;
if(h<l)
return 0;
min=(l+h)/2;
if(a[min]==ch)
return 1;
else if(a[min]>ch)
flag=bin_srch(a,l,min-1);
else
flag=bin_srch(a,min+1,h);
return flag;
}


Stack

Program to show the implementation of stack.


#include<stdio.h>
#include<conio.h>
#define stk_size 5
void push(int);
void pop();
void display();
int top=-1,n,s[10],j;
main()
{
int num;
clrscr();
printf("\n1 : PUSH\n2 : POP\n3 : DISPLAY\n4 : DISPLAY AND EXIT\n");
while(j!=5)
{
printf("\nEnter choice ");
scanf("%d",&j);
switch(j)
{
case 1: printf("\nEnter the value to be pushed to the stack ");
scanf("%d",&num);
push(num);
break;
case 2: pop();
break;
case 3: display();
break;
default:display();
break;
}
}
getch();
return 0;
}
void push(int a)
{
if(top==(stk_size-1))
{
printf("\nStack overflow");
return;
}
top++;
s[top]=a;
printf("Element pushed!!");
}
void pop()
{
if(top==-1)
{
printf("\nStack underflow");
return ;
}
n=s[top];
--top;
printf("Element popped..!!");
}
void display()
{
int i;
if(top==-1)
{
printf("\nNo more elements");
return ;
}
for(i=top;i>=0;i--)
printf("%d\n",s[i]);
if(j==4) j=5;
}

Stack implementation

Program to implement stack using array.


#include<stdio.h>
#include<conio.h>
#define stk_size 5
void push(int);
void pop();
void display();
int top=-1,n,s[10];
main()
{
clrscr();
push(20);
push(30);
pop();
push(8);
display();
getch();
return 0;
}
void push(int a)
{
if(top==(stk_size-1))
{
printf("\nStack overflow");
return;
}
top++;
s[top]=a;
}
void pop()
{
if(top==-1)
{
printf("\nStack underflow");
return ;
}
n=s[top];
--top;
}
void display()
{
int i;
if(top==-1)
{
printf("\nNo more elements");
return ;
}
for(i=top;i>=0;i--)
printf("%d\n",s[i]);
}

Concatanate 2 strings

Program to concatanate 2 entered strings ( without using inbuilt function )

#include<stdio.h>
#include<conio.h>
main()
{
char a[20],b[20];
int i,l1=0,l2=0,j=0;
clrscr();
printf("Enter the string A ");
gets(a);
printf("Enter the string B ");
gets(b);
for(i=0;a[i]!='\0';i++)
l1++;
for(i=0;b[i]!='\0';i++)
l2++;
for(i=l1;j<l2;i++)
{
a[i]=b[j];
j++;
}
a[i]='\0';
printf("Concatenated string is ");
puts(a);
getch();
return 0;
}

Palindrome check for a string

Program to check if the entered string is a palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[20],rev[20];
int i,l=0,flag=0;
clrscr();
printf("Enter the string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
l++;
for(i=0;i<l;i++)
{
rev[i]=a[l-1-i];
}
rev[i]='\0';
for(i=0;i<l;i++)
{
if(a[i]!=rev[i])
{
flag=1;
break;
}
}
printf("Entered string is %s \n",a);
printf("Reversed string is %s \n",rev);
if(flag)
printf("String is not a palindrome");
else
printf("String is a palindrome");
getch();
return 0;
}


Linear search

Program to search for a key element in an entered array using linear search.


#include<stdio.h>
#include<conio.h>
main()
{
int a[30],i,j,key,n,m,flag=0;
clrscr();
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
j=i;
if(flag)
{
printf("The key element is found in the position %d\n",i+1);
printf("Enter the no which is to be replaced with the key element\n");
scanf("%d",&m);
a[j]=m;
printf("Array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
else
printf("Key element is not found in the array");
getch();
return 0;
}

Palindrome check for an array

Program to check if the entered array is a palindrome or not..

#include<stdio.h>
#include<conio.h>
main()
{
int a[30],b[30],i,n,swap,temp=0;
clrscr();
printf("Enter n\n" );
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[n-i-1]=a[i];
}
for(i=0;i<=(n/2);i++)
{
swap=a[i];
a[i]=a[n-i-1];
a[n-i-1]=swap;
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
temp=1;
}
printf("Reversed array is \n");
for(i=0;i<n;i++)
printf("%d\n",b[i]);
if(temp)
printf("The entered array is a palindrome");
else
printf("The entered array is not a palindrome");
getch();
return 0;
}

GCD of 2 numbers

Program to find the GCD of 2 numbers using the Euclid's algorithm.


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,m,n,r;
clrscr();
printf("Enter the two nos\n");
scanf("%d%d",&a,&b);
if(a>b)
{
m=a;
n=b;
}
else
{
m=b;
n=a;
}
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
printf("GCD of %d and %d is %d",a,b,n);
getch();
return 0;
}

Bubble sort

Program to sort a given array using BUBBLE SORT TECHNIQUE..


#include&lt;stdio.h>
#include&lt;conio.h>
#include&lt;time.h>
#include&lt;dos.h>
#include&lt;stdlib.h>
main()
{
void bbl_sort(int *,int);
int n,i,a[20];
clrscr();
printf("\nEnter the no elements : ");
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for(i=0;i&lt;n;i++)
scanf("%d",&a[i]);
for(i=0;i&lt;1000;i++)
bbl_sort(a,n);
printf("\nSorting.... " );
getch();
printf("\n");
printf("\nSorted array is \n");
printf("\n");
for(i=0;i&lt;n;i++)
printf("%d\t",a[i]);
getch();
return 0;
}

void bbl_sort(int a[],int n)
{
int i,temp;
if(n==0)
return;
for(i=0;i&lt;n-1;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
bbl_sort(a,n-1);
}