You are on page 1of 45

BINARY SEARCH

CODE
#include<stdio.h>
void main()
{
int i,low,high,mid,n,search,a[100];
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter %d integers:", n);
for (i=0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter element that we need to find:");
scanf("%d", &search);
low=0;
high=n-1;
mid=(low+high)/2;
while (low<=high) //Condition to conduct Binary search
{
if (a[mid]<search)
{
low=mid+1;
}
else if (a[mid]==search) {
{
printf("%d found at location %d.",search,mid+1);
}
else
last = mid-1;
}
if (low>high)

printf("Element is not found!!);


}

OUTPUT
Enter number of elements:5
Enter 5 integers:1 23 45 78 90
Enter element that we need to find:23
23 found at location 2.

LARGEST OF THREE NUMBERS


CODE
#include <stdio.h>
void main()
{
int a,b,c;
printf("Enter values of a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
if (a>b&&a>c) {
printf("a is Greater than b and c");
}
else
if (b>a&&b>c)
{
printf("b is Greater than a and c");
}
else
if (c>a&&c>b)
{
printf("c is Greater than a and b");
}
else
{
printf("All are equal or any two values are equal");
}

OUTPUT
Enter values of a,b,c:
12 23 434
c is Greater than a and b

LINEAR SEARCH
CODE

#include<stdio.h>
void main()
{
int a[20];
int i,size,search;
printf("Enter total no. of elements \n : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("Enter %d elements:\n",i+1);
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:\n ");
scanf("%d",&searh);
for(i=0;i<size;i++)
{
if(search==a[i])
{
printf("Element exists in the list at position : %d \n",i+1);
}
else
printf(Element not found!!);
}
}

OUTPUT
Enter total no. of elements:
5

Enter 5 elements:
23 43 78 90 110
Enter the element to be searched:
90
Element exists in the list at position:
4

CHECKING PALINDROME
CODE
#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindrome\n");
gets(a);
strcpy(b,a);
strrev(b);
if (strcmp(a,b) == 0)
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
return 0;
}

OUTPUT
Enter the string to check if it is a palindrome
MALAYALAM
Entered string is a palindrome.

ROOTS OF QUADRATIC EQUATION


CODE

#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
printf("enter the value of a,b & c\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-(4*a*c);
if(d==0)
{
printf("Both roots are equal\n");
r1=-b/(2.0*a) ; r2=r1;
printf("First Root= %f\n",r1);
printf("Second Root= %f\n",r2);
}
else if(d>0) {
printf("Both roots are real and Unique\n");
r1= (-b + sqrt(d))/(2*a);
r2= ( -b - sqrt(d))/(2*a);
printf("First Root = %f\n",x1);
printf("Second Root = %f\n",x2);
}

else printf("Root are imaginary \n No Solution!! \n");


}

OUTPUT
enter the value of a,b & c
234
Roots are imaginary
No Solution

ADDITION OF TWO MATRICES


CODE
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");

scanf("%d%d", &m, &n);


printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}

OUTPUT
Enter the number of rows and columns of matrix
22
Enter the elements of first matrix
1234
Enter the elements of second matrix
4321
Sum of entered matrices
5555

BUBBLE SORT
CODE
#include <stdio.h>
void main()
{
int a[100],i,j,n,temp;

printf("Enter the number of elements to be sorted: ");


scanf("%d",&n);
printf(Enter the elements:\n);
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
}
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;j++)
{
If(a[i]>a[i+1]) //To sort in descending order, change > to < in this
line.
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
printf("Elements in Ascending order: \n");
for(i=0;i<n;++i)
printf("%d",a[i]);
}

OUTPUT
Enter the number of elements to be sorted: 10
Enter the elements:
12 3 78 98 88 99 11 2 234 22
Elements in Ascending order:
2 3 11 12 22 78 88 98 99 234

DISPLAY ALL EVEN AND ODD NUMBERS


IN N NUMBERS
CODE
#include<stdio.h>
void main()
{
int a[100],i,n;

printf(Enter the size of the array:\n);


scanf(%d,&n);`````````````````
printf(Enter the array elements:\n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
for(i=0;i<n;i++)
{
If(a[i]%2==0)
{
printf(Even Numbers: %d \t,a[i]);
}
else
if(a[i]%2==1)
{
printf(Odd Numbers : %d \t,a[i]);
}
else printf(Wrong Input!!);
}
}

OUTPUT

Enter the size of the array:


10
Enter the array elements:
112 3 434 5 23 34 232 111 22 101
Odd Numbers:
3

23

111

101

Even Numbers:
112

432

34

232

22

SELECTION SORT
CODE
#include<stdio.h>
void main()
{
int n,i,j,temp,a[20];
printf("Enter total number of elements:\n ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=0;i<n;i++)
{

scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is:\n ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
}

OUTPUT
Enter total number of elements:
5
Enter 5 elements:
23 33 11 3 56
After sorting is:
3 11 23 33 56

SIMPLE CALCULATOR
CODE
# include <stdio.h>
void main()
{
char ch;
float num1,num2;
printf("Enter Operator symbol ( +,-,*,%,/) : ");

scanf("%c",&ch);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(ch)
{
case '+': printf("%f + %f = %f",num1, num2, num1+num2);
break;
case '-': printf("%f - %f = %f",num1, num2, num1-num2);
break;
case '*': printf("%f * %f = %f",num1, num2, num1*num2);
break;
case '/': printf("%f / %f = %f",num1, num2, num1/num2);
break;
default: printf("Error! operator is not correct");
break;
}
}

OUTPUT
Enter Operator symbol (+,-,*,%,/) : /
Enter two operands: 43 16
43/16 = 2.6875

DISPLAY N NUMBERS IN DECREMENT


ORDER
CODE
#include<stdio.h>
void main()
{
int n,i;
printf(Enter the value of N \n);

scanf(%d,&n);
for(i=n;i<0;i--)
{
printf(The numbers in decrement order are: %d \n
\t,i);
}
}

OUTPUT
Enter the value of N
7
numbers in decrement order are:
7

MATRIX MULTIPLICATION
CODE
#include <stdio.h>
void main()
{
int r, c, r1, c1, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &r, &c);
printf("Enter the elements of first matrix\n");
{

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
scanf("%d", &first[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &r1, &c1);
if (c != r1)
printf("Matrices with entered orders can't be multiplied with each
other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &second[i][j]);
for (i = 0; i < r; i++)
{
for (j = 0; j < c1; j++)
{
for (k = 0; k < r1; k++)
{
sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c1; j++)
printf("%d\t", multiply[i][j]);
printf("\n");
}

}
}

OUTPUT
Enter the number of rows and columns of first matrix
33
Enter the elements of first matrix
357
289
150
Enter the number of rows and columns of second matrix
33
Enter the elements of second matrix
247
123
345
Product of entered matrices:32 50 71
39 60 83
7 14 22

FACTORIAL OF A NUMBER
) CODE
#include<stdio.h>
void main()
{
int i=1,fact=1,num;
printf("Enter a number:\n");
scanf("%d",&num);
while(i<=num)
{
fact=fact*i;
i++;
}
printf("Factorial of %d is %d ",num,fact);

OUTPUT
Enter a number:
10
Factorial of 10 is 3628800

MATRIX TRANSPOSE
CODE
#include <stdio.h>
void main()
{
int a[10][10], i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0 ; i < m ; ++i)
{
for (j = 0; j < n; ++j)

{
scanf("%d", &a[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
}

OUTPUT
Enter the order of the matrix
32
Enter the elements of the matrix
245
123
The given matrix is
245
123
Transpose of matrix is
12
24
3 5

GRADES OF STUDENTS W.R.T THEIR


MARKS
CODE
#include<stdio.h>
int main()
{
int m;
char grad;
printf("Enter the marks of the student\n");
scanf("&d",&m);
if(m>=0 && m<=20)
{
grad='F';

printf("Grade is %c",grad);
}
else
if(m>20 && m<=30)
{
grad='B';
printf("Grade is %c",grad);
}
else
if(m>30 && m<=40)
{
grad='A';
printf("Grade is %c",grad);
}
else
{
grad='O';
printf("Grade is %c",grad);
}
return 0;
}

OUTPUT

Enter the marks of the student


45
Grade is O.

STUDENT RECORD
CODE
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
string dept;
string grade;
};
int main()
{
struct student s[10];

int i;
printf("***** Information of 10 students*****\n");
for(i=0;i<3;i++)
{
s[i].roll=i+1;
printf("Enter roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
printf(Enter dept \n:);
scanf(%f,&s[i].dept);
printf(Enter grade);
scanf(%f,&s[i].grade);
}
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("Information for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);

printf(Department:);
puts(s[i].dept);
printf(Grade:);
puts(s[i].grade);
}
return 0;
}

OUTPUT
***** Information of students*****
Enter roll number
35
Enter name:
Kevin
Marks: 45
Department: BS
Grade: A
Enter roll number
25
Enter name:
Stuart
Marks: 45
Department: BS

Grade: A

Enter roll number


16
Enter name:
Bob
Marks: 45
Department: BS
Grade: A

FIBONACCI SERIES
CODE
#include<stdio.h>
void main()
{
int first, second, sum, n;
printf("Enter the value of 'N': ");
scanf("%d", &num);
first = 0,second=1;
printf("Fibonacci Series : %d %d ", first, second);
while (num!=0)
{
sum = first + second;
printf("%d \t", sum);
first = second;
second = sum;

}
}

OUTPUT
Enter the value of 'N': 11
Fibonacci Series : 0 1 1 2 3 5 8 13 21 34 55 89

EMPLOYEE DATA USING UNIONS


CODE
#include <stdio.h>
union job
{
char emp_name[32];
float salary;
int emp_no;
char emp_dept;
int age;

}u;
int main()
{
printf("Enter name:\n");
scanf("%s",&u.emp_name);

printf("Enter salary: \n");


scanf("%f",&u.salary);
printf(Enter the Employee no. \n);
scanf("%d",&u.emp_no);
printf("Enter the Department of the Employee:\n");
scanf("%s",&u.emp_dept);
printf("Enter the age of the Employee:\d");
scanf("%d",&u.age);
printf("Displaying\nName :%s\n",u.emp_name);
printf("Salary: %.1f",u.salary);
printf("The Employee No.is:%d \n",u.age);
printf("The Department is:",u.emp_dept);
printf("The age of the Employee is:",u.emp_no);
return 0;
}

OUTPUT
Enter name:
Gautam
Enter salary:
75,000/Enter the Employee no:

MU007
Enter the Department of the Employee:
Marketing
Enter the age of the Employee:
22
Displaying
Name:
Gautam
Salary: 75,000
The Employee No.is:
MU007
The Department is: Marketing
The age of the Employee is: 22

INPUT & DISPLAY THE ELEMENTS IN ONE


DIMENSIONAL ARRAY
CODE
#include<stdio.h>
void main()
{
int i,n;
printf("Enter the value of n\n");
scanf("%d",&n);
int a[n];
printf("Enter the elements of the array a\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The elements in the array are:\n");
for(i=0;i<n;i++)

{
printf("%d\t",a[i]);
}
}

OUTPUT
Enter the value of n
4
Enter the elements of the array a
1234
The elements in the array are:
1

DISPLAY PRIME NUMBER UPTO N


CODE
#include<stdio.h>
void main()
{
int i,a[100],n,count=0,j;
printf(Enter the value of n \n);
scanf(%d,&n);
for(i=n,i<=0;i--)
if(i%2==0 && i/i==0)
{
count++;

if( count==2)
{
printf(Prime numbers : %d \t,i);
}
else
i=j;

printf(Non-Prime numbers : %d \t,j);


}
}

OUTPUT
Enter the value of n
11
Prime numbers are:
2 3 5 7 11
Non-Prime numbers: 1 4 6 8 9 10

LINEAR SEARCH USING FUNCTIONS


CODE
#include<stdio.h>
#include<conio.h>
#define MAX 20
int lsearch(int [],int,int);
void main()
{
int a[MAX],k,i,n,m;
clrscr();
printf(Enter the size of the array:);
scanf(%d,&n);
if(n>MAX)
{
printf(Invalid index);
}
else
{
printf(Enter The Array Elements:);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
printf(Enter the search element:);
scanf(%d,&k);

m=lsearch(a,n,k);
if(m==-1)
{
printf(Element Not found);
}
else
{
printf(Element found @ position: %d,m);
}
}
getch();
}
int lsearch(int a[],int n,int k)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
return i;
}
}
if(i==n)
{
return -1;
}
}

OUTPUT
Enter the size of the array: 5
Enter The Array Elements: 11 34 55 66 78
Enter the search element: 55
Element found @ position: 3

GCD OF THREE NUMBERS


CODE
#include<stdio.h>
#include<conio.h>
int gcd(int m,int n)
{
int rem;
while(n!=0)
{
rem=m%n;
m=n;
n=rem;
}
return(m);
}
main()
{
int num1,num2,num3,gcd1,gcd2;
printf("Enter three positive integers:\n");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1==0 && num2==0 && num3==0)

{
printf("\n Invalid number");
}
gcd1=gcd(num1,num2);
gcd2=gcd(num3,gcd1);
printf("\n GCD of %d, %d and %d is :
%d\n",num1,num2,num3,gcd2);
getch();
}
}

OUTPUT
Enter three positive integers:
24 36 78
GCD of 24,36 and 78 is 2