Sei sulla pagina 1di 26

RECURSION

#include<stdio.h>
int factorial(int n);
int fib(int a);
void main()
{
int n1,ch,z,w,i;
char c;
do
{
printf("MENU\n1.FACTORIAL\n2.FIBINOCCI SERIES\n");
printf("Enter your choice:");
scanf("%d",&ch);
printf("Enter the number :");
scanf("%d",&n1);
switch(ch)
{
case 1:
z=factorial(n1);
printf("Factorial is %d",z);
break;
case 2:
for(i=0;i<n1;i++)
{
w=fib(i);
printf("%d\t",w);
}
break;
}
printf("\nDo you want to continue?y/n:");
__fpurge(stdin);
scanf("%c",&c);
}while(c=='y');
}
int factorial(int n)

if(n==1)
return(1);
else
return(n*factorial(n-1));

}
int fib(int a)
{
if(a==0)
return(0);
else if(a==1)
return(1);
else
return(fib(a-1)+fib(a-2));
}

OUTPUT
MENU
1.FACTORIAL
2.FIBINOCCI SERIES
Enter your choice:1
Enter the number :4
Factorial is 24
Do you want to continue?y/n:y
MENU
1.FACTORIAL
2.FIBINOCCI SERIES
Enter your choice:2
Enter the number :3
0
1
1
Do you want to continue?y/n:n

MATRIX

#include<stdio.h>
void mul(int a1[50][50],int m1,int n1,int b1[50][50],int p1,int
q1);
void sum(int a1[50][50],int m1,int n1,int b1[50][50],int p1,int
q1);
void transpose(int a1[50][50],int m1,int n1);
void main()
{
int c,i,j,a[50][50],b[50][50],m,n,p,q;
char ch;
do
{
printf("MENU\n1.MULTIPLICATION\n2.SUM\n3.TRANSPOSE\n");
printf("Enter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the number of rows and columns of matrix 1:");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of matrix 2:");
scanf("%d%d",&p,&q);
printf("Enter the elements into matrix 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements into matrix 2:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{

scanf("%d",&b[i][j]);
}
}
mul(a,m,n,b,p,q);
break;
case 2:
printf("Enter the number of rows and columns of matrix 1:");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of matrix 2:");
scanf("%d%d",&p,&q);
printf("Enter the elements into matrix 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements into matrix 2:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
sum(a,m,n,b,p,q);
break;
case 3:
printf("Enter the number of rows and columns of matrix 1:");
scanf("%d%d",&m,&n);
printf("Enter the elements into matrix :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

scanf("%d",&a[i][j]);
}
}
transpose(a,m,n);
break;
default:printf("WRONG OPTION\n");
}
printf("Do you want to continue?(y/n):");
__fpurge(stdin);
scanf("%c",&ch);
}while(ch=='y');

}
void mul(int a1[50][50],int m1,int n1,int b1[50][50],int p1,int
q1)
{
int i,j,c[50][50],k;
if((n1==p1))
{
for(i=0;i<m1;i++)
{
for(j=0;j<q1;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
{
c[i][j]=c[i][j]+a1[i][k]*b1[k][j];
}
}
}
printf("After matrix multiplication:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<q1;j++)
{
printf("%d\t",c[i][j]);

}printf("\n");

}
}
else
{
printf("Matrix multiplication not possible\n");
}
}
void sum(int a1[50][50],int m1,int n1,int b1[50][50],int p1,int
q1)
{
int i,j,c[50][50];
if((m1==p1) && (n1==q1))
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a1[i][j]+b1[i][j];
}
}
printf("Sum of the 2 matrixes:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t",c[i][j]);
}printf("\n");
}
}
else
{
printf("Sum cannot be determined\n");
}
}
void transpose(int a1[50][50],int m1,int n1)

int i,j,b[50][50];
for(i=0;i<n1;i++)
{
for(j=0;j<m1;j++)
{
b[i][j]=a1[j][i];
}
}
printf("Transpose of the matrix :\n");
for(i=0;i<n1;i++)
{
for(j=0;j<m1;j++)
{
printf("%d ",b[i][j]);
}printf("\n");
}

OUTPUT
MENU
1.MULTIPLICATION
2.SUM
3.TRANSPOSE
Enter your choice :1
Enter the number of rows and columns of matrix 1:2 2
Enter the number of rows and columns of matrix 2:2 2
Enter the elements into matrix 1:
11
11
Enter the elements into matrix 2:
11
11
After matrix multiplication:
2
2

2
2
Do you want to continue?(y/n):y
MENU
1.MULTIPLICATION
2.SUM
3.TRANSPOSE
Enter your choice :2
Enter the number of rows and columns of matrix 1:2 2
Enter the number of rows and columns of matrix 2:2 2
Enter the elements into matrix 1:
11
11
Enter the elements into matrix 2:
11
11
Sum of the 2 matrixes:
2
2
2
2
Do you want to continue?(y/n):y
MENU
1.MULTIPLICATION
2.SUM
3.TRANSPOSE
Enter your choice :3
Enter the number of rows and columns of matrix 1:2 3
Enter the elements into matrix :
123
456
Transpose of the matrix :
14
25
36
Do you want to continue?(y/n):n

STRUCTURE
#include<stdio.h>

struct employee
{
char empname[25];
int empid;
float basic;
float da;
float hra;
float netsal;
}e[50],temp;
int n;
void maxsal();
void hra();
void sort();
void add();
void del();
void main()
{
int i,c;
char ch;
printf("Enter the number of employees :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name :");
__fpurge(stdin);
gets(e[i].empname);
printf("Enter the id :");
scanf("%d",&e[i].empid);
printf("Enter the basic salary :");
scanf("%f",&e[i].basic);
e[i].da=0.8*(e[i].basic);
printf("DA :%f\n",e[i].da);
printf("Enter the HRA :");
scanf("%f",&e[i].hra);
e[i].netsal=e[i].basic+e[i].hra+e[i].da;
printf("Net salary :%f\n",e[i].netsal);

do
{
printf("\nMENU\n1.EMPLOYEE WITH MAXIMUM SALARY\n");
printf("2.EMPLOYEE HAVING NO HRA\n");
printf("3.SORT THE EMPLOYEES\n");
printf("4.ADD A NEW EMPLOYEE\n");
printf("5.DELETE AN EMPLOYEE\n");
printf("Enter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:
maxsal();
break;
case 2:
hra();
break;
case 3:
sort();
break;
case 4:
add();
break;
case 5:
del();
break;
default:
printf("WRONG CHOICE");
}
printf("Do you want to continue?(y/n)");
__fpurge(stdin);
scanf("%c",&ch);
}while(ch=='y');
}
void maxsal()

float big;
int i,pos=0;
big=e[0].netsal;
for(i=0;i<n;i++)
{
if(e[i].netsal>big)
{
big=e[i].netsal;
pos=i;
}
}
printf("\nEMPLOYEE NAME :");
puts(e[pos].empname);
printf("EMPLOYEE ID :%d\n",e[pos].empid);
printf("NET SALARY :%f\n",e[pos].netsal);

}
void hra()
{
int i=0,flag=0;
for(i=0;i<n;i++)
{
if(e[i].hra==0)
{
flag=1;
printf("\nDETAILS OF EMPLOYEE WITH ZERO HR :\n");
printf("EMPLOYEE NAME :");
puts(e[i].empname);
printf("EMPLOYEE ID :%d\n",e[i].empid);
printf("HRA
:%f\n",e[i].hra);
}
}
if(flag==0)
printf("No employee with zero hra\n");
}
void sort()

int i,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(e[j].netsal<e[j+1].netsal)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("\nEMPLOYEE NAME :");
puts(e[i].empname);
printf("EMPLOYEE ID :%d\n",e[i].empid);
printf("NET SALARY :%f\n",e[i].netsal);
}

}
void add()
{
int i;
printf("Enter the name :");
__fpurge(stdin);
gets(e[n].empname);
printf("Enter the id :");
scanf("%d",&e[n].empid);
printf("Enter the basic salary :");
scanf("%f",&e[n].basic);
e[n].da=0.8*(e[n].basic);
printf("DA :%f\n",e[n].da);
printf("Enter the HRA :");
scanf("%f",&e[n].hra);

e[n].netsal=e[n].basic+e[n].hra+e[n].da;
printf("Net salary :%f\n",e[n].netsal);
for(i=0;i<=n;i++)
{
printf("\nEMPLOYEE NAME :");
puts(e[i].empname);
printf("EMPLOYEE ID :%d\n",e[i].empid);
printf("NET SALARY :%f\n",e[i].netsal);
}
n++;
}
void del()
{
int i,x,pos=0;
printf("Enter the id of the employee to be deleted:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(e[i].empid==x)
{
pos=i;
break;
}
}
for(i=pos;i<n-1;i++)
{
e[i]=e[i+1];
}
n--;
for(i=0;i<n;i++)
{
printf("\nEMPLOYEE NAME :");
puts(e[i].empname);
printf("EMPLOYEE ID :%d\n",e[i].empid);
printf("NET SALARY :%f\n",e[i].netsal);
}

OUTPUT
Enter the number of employees :3
Enter the name :Arun
Enter the id :1
Enter the basic salary :1000
DA :800.000000
Enter the HRA :0
Net salary :1800.000000
Enter the name :Ram
Enter the id :2
Enter the basic salary :2000
DA :1600.000000
Enter the HRA :10
Net salary :3610.000000
Enter the name :Anu
Enter the id :3
Enter the basic salary :1500
DA :1200.000000
Enter the HRA :5
Net salary :2705.000000
MENU
1.EMPLOYEE WITH MAXIMUM SALARY
2.EMPLOYEE HAVING NO HRA
3.SORT THE EMPLOYEES
4.ADD A NEW EMPLOYEE
5.DELETE AN EMPLOYEE
Enter your choice :1
EMPLOYEE NAME :Ram
EMPLOYEE ID :2
NET SALARY :3610.000000
Do you want to continue?(y/n)y
MENU
1.EMPLOYEE WITH MAXIMUM SALARY

2.EMPLOYEE HAVING NO HRA


3.SORT THE EMPLOYEES
4.ADD A NEW EMPLOYEE
5.DELETE AN EMPLOYEE
Enter your choice :2
DETAILS OF EMPLOYEE WITH ZERO HR :
EMPLOYEE NAME :Arun
EMPLOYEE ID :1
HRA
:0.000000
Do you want to continue?(y/n)y
MENU
1.EMPLOYEE WITH MAXIMUM SALARY
2.EMPLOYEE HAVING NO HRA
3.SORT THE EMPLOYEES
4.ADD A NEW EMPLOYEE
5.DELETE AN EMPLOYEE
Enter your choice :3
EMPLOYEE NAME :Ram
EMPLOYEE ID :2
NET SALARY :3610.000000
EMPLOYEE NAME :Anu
EMPLOYEE ID :3
NET SALARY :2705.000000
EMPLOYEE NAME :Arun
EMPLOYEE ID :1
NET SALARY :1800.000000
Do you want to continue?(y/n)y
MENU
1.EMPLOYEE WITH MAXIMUM SALARY
2.EMPLOYEE HAVING NO HRA
3.SORT THE EMPLOYEES
4.ADD A NEW EMPLOYEE
5.DELETE AN EMPLOYEE
Enter your choice :4
Enter the name :Nikhil
Enter the id :4

Enter the basic salary :1200


DA :960.000000
Enter the HRA :10
Net salary :2170.000000
EMPLOYEE NAME :Ram
EMPLOYEE ID :2
NET SALARY :3610.000000
EMPLOYEE NAME :Anu
EMPLOYEE ID :3
NET SALARY :2705.000000
EMPLOYEE NAME :Arun
EMPLOYEE ID :1
NET SALARY :1800.000000
EMPLOYEE NAME :Nikhil
EMPLOYEE ID :4
NET SALARY :2170.000000
Do you want to continue?(y/n)y
MENU
1.EMPLOYEE WITH MAXIMUM SALARY
2.EMPLOYEE HAVING NO HRA
3.SORT THE EMPLOYEES
4.ADD A NEW EMPLOYEE
5.DELETE AN EMPLOYEE
Enter your choice :5
Enter the id of the employee to be deleted:2
EMPLOYEE NAME :Anu
EMPLOYEE ID :3
NET SALARY :2705.000000
EMPLOYEE NAME :Arun
EMPLOYEE ID :1
NET SALARY :1800.000000
EMPLOYEE NAME :Nikhil
EMPLOYEE ID :4
NET SALARY :2170.000000
Do you want to continue?(y/n)n

STRING REPLACE
#include<stdio.h>
#include<string.h>
void replace(char str[50],char rep[50],char wrd[50]);
void main()
{
char str[50],rep[50],wrd[50];
printf("Enter the string :");
__fpurge(stdin);
gets(str);
printf("Enter the string to be replaced :");
__fpurge(stdin);
gets(wrd);
printf("Enter the string with which it is to be replaced :");
__fpurge(stdin);
gets(rep);
replace(str,rep,wrd);
}
void replace(char str[50],char rep[50],char wrd[50])
{
char b[50][50];
int c,i,j,f,r=0,l=0,k;
for(i=0;str[i]!='\0';i++)
l=l+1;
for(i=0;i<l;i++)
{
for(c=0;str[i]!='\0' && str[i]!=' ';c++,i++)
b[r][c]=str[i];
b[r][c]='\0';
r++;
}
for(i=0;i<r;i++)
{
f=0;
for(c=0,j=0;wrd[j]!='\0';j++,c++)

if(b[i][c]!=wrd[j])
{
f=1;
break;
}

}
if(f==0)
{
for(j=0,c=0;str[j]!='\0';j++,c++)
{
b[i][c]=rep[j];
}
b[i][c]='\0';
}
}
printf("The string after replacing:");
for(i=0;i<r;i++)
{
printf("%s ",b[i]);
}
printf("\n");
}

OUTPUT
Enter the string :Flowers are red
Enter the string to be replaced :Flowers
Enter the string with which it is to be replaced :Buds
The string after replacing:Buds are red

STRING COUNT
#include<stdio.h>
#include<string.h>
void count(char s[20]);
void main()

{
char str[50],ch;
int i;
printf("Enter a line :");
printf("Enter # at the end of the line.\n");
while((ch=getchar())!='#')
{
str[i]=ch;
i++;
}
str[i]='\0';
count(str);
}
void count(char s[20])
{
int vowel=0,cons=0,space=0,line=1,words=1,spl=0,i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='\n')
{
line++;
words++;
}
else if(s[i]==' ')
{
space++;
words++;
}
else if((s[i]>='A' && s[i]<='Z') || (s[i]>='a' && s[i]<='z'))
{
if((s[i]=='a')||(s[i]=='e')||(s[i]=='i')||(s[i]=='o')||(s[i]=='u')||
(s[i]=='A')||(s[i]=='E')||(s[i]=='I')||(s[i]=='O')||(s[i]=='U'))
{
vowel++;
}
else

{
cons++;
}
}
else
{
spl++;
}
}
printf("The
printf("The
printf("The
printf("The
printf("The
printf("The
}

number
number
number
number
number
number

of
of
of
of
of
of

lines
:%d\n",line);
words
:%d\n",words);
vowels
:%d\n",vowel);
consonants
:%d\n",cons);
special characters :%d\n",spl);
spaces
:%d\n",space);

OUTPUT
Enter a line :Enter # at the end of the line.
How
are u??#
The number of lines
:2
The number of words
:3
The number of vowels
:4
The number of consonants
:3
The number of special characters :2
The number of spaces
:1

SORT NAMES

#include<stdio.h>
#include<string.h>
void sort(char str1[50][50],int n1);
void main()
{
char str[50][50];
int i,n;
printf("Enter the number of names :");
scanf("%d",&n);
printf("Enter the names :\n");
__fpurge(stdin);
for(i=0;i<n;i++)
{
gets(str[i]);
}
sort(str,n);
}
void sort(char str1[50][50],int n1)
{
int i,c,j;
char temp[50];
for(i=0;i<n1-1;i++)
{
for(j=0;j<n1-1-i;j++)
{
c=strcmp(str1[j],str1[j+1]);
if(c>0)
{
strcpy(temp,str1[j]);
strcpy(str1[j],str1[j+1]);
strcpy(str1[j+1],temp);
}
}
}
printf("\n");
for(i=0;i<n1;i++)

puts(str1[i]);

OUTPUT
Enter the number of names :5
Enter the names :
ram
anu
rahul
arjun
mohan
anu
arjun
mohan
rahul
ram

LINEAR SEARCH
#include<stdio.h>
void linear(int b[100],int n1,int x1);
void main()
{
int a[100],n,i,x;
printf("Enter the number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements into the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&x);

linear(a,n,x);

}
void linear(int b[100],int n1,int x1)
{
int x,i,f=0;
for(i=0;i<n1;i++)
{
if(x1==b[i])
{
f=1;
break;
}
}
if(f==0)
printf("Element not present\n");
else
printf("The element %d is present in position
%d\n",x1,i+1);
}

OUTPUT
Enter the number of elements in the arrar:5
Enter the elements into the array:
12345
Enter the element to be searched:3
The element 3 is present in position 3

BINARY SEARCH
#include<stdio.h>
void binary(int b[100],int n1,int x1);
void main()
{
int a[100],n,i,x;
printf("Enter the number of elements in the array:");

scanf("%d",&n);
printf("Enter the elements into the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&x);
binary(a,n,x);

}
void binary(int b[100],int n1,int x1)
{
int i,mid,low=0,high,f=0;
high=n1-1;
while(low<=high)
{
mid=(low+high)/2;
if(b[mid]==x1)
{
f=1;
break;
}
else if(b[mid]<x1)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(f==0)
printf("The element is not present\n");
else
printf("The element %d is found in position
%d\n",x1,mid+1);

OUTPUT
Enter the number of elements in the arrar:5
Enter the elements into the array:
12345
Enter the element to be searched:2
The element 2 is found in position 2

BUBBLE SORT
#include<stdio.h>
void sort(int b[100],int n1);
void main()
{
int a[100],n,i;
printf("Enter the number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements into the array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
}
void sort(int b[100],int n1)
{
int i,j,temp,k;

for(i=0;i<=n1-2;i++)
{
printf("\nPASS %d\n",i+1);
for(j=0;j<=(n1-i-2);j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
for(k=0;k<n1;k++)
printf("%d ",b[k]);
}
printf("\n");
}

OUTPUT
Enter the number of elements in the array:5
Enter the elements into the array:
50 40 30 20 10
PASS 1
40 30 20 10 50
PASS 2
30 20 10 40 50
PASS 3
20 10 30 40 50
PASS 4
10 20 30 40 50

Potrebbero piacerti anche