You are on page 1of 10

CCP PROGRAMS

GCD,LCM-ELUCID'S ALGORITHM

PROG-8

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,r,gcd,lcm;
clrscr();
printf("enter the numbers\n");
scanf("%d%d",&m,&n);
p=m;
q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=(p*q)/gcd;
printf("GCD(%d,%d)=%d\n",p,q,gcd);
printf("LCM(%d,%d)=%d\n",p,q,lcm);
getch();
}
PROG-9

PALINDROME

#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,rev,digit;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
m=n;
rev=0;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=rev*10+digit;
}
if(m==rev)
printf("the number is palindrome\n");
else
printf("the number is not palindrome\n");
getch();
}
QUADRATIC EQUATION

PROG-10

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,dis;

1
www.ncetianz.webs.com

CCP PROGRAMS

clrscr();
printf("enter the coefficients\n");
scanf("%f%f%f",&a,&b,&c);
dis=(b*b)-(4*a*c);
if(dis>0)
{
x1=(-b+sqrt(dis))/(2*a);
x2=(-b-sqrt(dis))/(2*a);
printf("the roots are distinct\n");
printf("X1=%f\n x2=%f\n",x1,x2);
}
else if(dis==0)
{
x1=x2=-b/(2*a);
printf("the roots are equal\n");
printf("X1=%f\n X2=%f\n",x1,x2);
}
else
{
x1=-b/(2*a);
x2=sqrt(fabs(dis))/(2*a);
printf("the roots are complex\n");
printf("X1=%f+i%f\n",x1,x2);
printf("X2=%f-i%f\n",x1,x2);
}
getch();
}
BUBBLE SORT

PROG-11

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,a[20];
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the items to sort\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

2
www.ncetianz.webs.com

CCP PROGRAMS

BINARY SEARCH

PROG-12

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,item,low,mid,high,a[20],n;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the n values\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the item to be searched\n");
scanf("%d",&item);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{
printf("sucess\n");
getch();
exit(0);
}
if(item<a[mid])
high=mid-1;
else
{
low=mid+1;
}
printf("unsucess\n");
getch();
}
}
PROG-13 HORNER'S METHOD
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float a[30],x,sum;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter n+1 values\n");
for(i=0;i<=n;i++)
{
scanf("%f",&a[i]);
}
printf("enter the value of x\n");
scanf("%f",x);
sum=a[n]*x;
for(i=n;i>0;i--)
{

3
www.ncetianz.webs.com

CCP PROGRAMS

sum=(sum+a[i])*pow(x,i);
}
sum=sum+a[0];
printf("the evaluated result=%f\n",sum);
getch();
}
PROG-14
MATRIX MULTPLICATION
#include<stdio.h>
#include<process.h>
void main()
{
int m,n,p,q,i,j,k,a[10][10],b[10][10],c[10][10],sum;
clrscr();
printf("enter the size of matrix a\n");
scanf("%d %d",&m,&n);
printf("enter the elements of matrix a\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the size of matrix b\n");
scanf("%d %d",&p,&q);
printf("enter the elements of matrix b\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
if(n!=p)
{
printf("multiplication is not possible\n");
exit(0);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum=sum+(a[i][k]*b[k][j]);
}
c[i][j]=sum;
}
}
printf("the resultant matrix c\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}

4
www.ncetianz.webs.com

CCP PROGRAMS

getch();
}

UDF-LINEAR SEARCH

PROG-15

#include<stdio.h>
#include<process.h>
#include<conio.h>
int i,n,item,a[20];
void input()
{
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void linear_search()
{
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("found at position=%d\n",i+1);
getch();
exit(0);
}
}
printf("unsucessfull search\n");
getch();
}
void main()
{
clrscr();
printf("enter the number of items\n");
scanf("%d",&n);
input();
printf("enter the item to be searched\n");
scanf("%d",&item);
linear_search();
}
UDF-BUBBLE SORT

PROG-16

#include<stdio.h>
#include<conio.h>
#include<process.h>
int n,i,j,temp,a[20];
void input()
{
printf("enter the item to be sorted\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void output()
{

5
www.ncetianz.webs.com

CCP PROGRAMS

for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
void bubble_sort()
{
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
void main()
{
clrscr();
printf("enter number of items to sort\n");
scanf("%d",&n);
input();
printf("the given array is\n");
output();
bubble_sort();
printf("the sorted array is\n");
output();
getch();
}

MEAN,VARIANCE AND DEVIATION

PROG-17

#include<stdio.h>
#include<conio.h>
#include<math.h>
float x[10],sum,mean,variance,deviation;
int n,i;
void input()
{
for(i=0;i<n;i++)
scanf("%f",&x[i]);
}
float find_mean()
{
sum=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
mean=sum/n;
printf("mean=%f\n",mean);
return (mean);
}

6
www.ncetianz.webs.com

CCP PROGRAMS

float find_var()
{
mean=find_mean();
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(x[i]-mean)*(x[i]-mean);
}
variance=sum/n;
printf("variance=%f\n",variance);
return (variance);
}
void find_dev()
{
variance=find_var();
deviation=(float)sqrt(variance);
printf("deviation=%f\n",deviation);
}
void main()
{
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter n real values\n");
input();
find_dev();
getch();
}

UDF---SELECTION_SORT

PROG-18

#include<stdio.h>
#include<conio.h>
#include<process.h>
int n,i,j,pos,a[20],temp;
void input()
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void output()
{
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
void selection_sort()
{
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]>a[pos])
pos=j;

7
www.ncetianz.webs.com

CCP PROGRAMS

}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
}
void main()
{
clrscr();
printf("enter number of items to sort\n");
scanf("%d",&n);
printf("enter the item to be sorted\n");
input();
printf("the given array is\n");
output();
selection_sort();
printf("the sorted array is\n");
output();
getch();
}
UDF-MATRIX MULTIPLICATION

PROG-19

#include<stdio.h>
#include<process.h>
#include<conio.h>
void read_matrix(int x[10][10],int m, int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",& x[i][j]);
}
}
}
void mul_matrix(int a[][10],int b[][10],int m,int n,int q,int c[][10])
{
int i,j,k,sum;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
}
void write_matrix(int x[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{

8
www.ncetianz.webs.com

CCP PROGRAMS

for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}
void main()
{
int m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr();
printf("enter the size of matrix a\n");
scanf("%d %d",&m,&n);
printf("enter the elements of matrix a\n");
read_matrix(a,m,n);
printf("enter the size of matrix b\n");
scanf("%d %d",&p,&q);
printf("enter the elements of matrix b\n");
read_matrix(b,p,q);
if(n!=p)
{
printf("multiplication is nor possible\n");
getch();
exit(0);
}
mul_matrix(a,b,m,n,q,c);
printf("product of two matrices\n");
write_matrix(c,m,q);
getch();
}
UDF---SUM OF ELEMENTS

PROG-20

#include<stdio.h>
#include<conio.h>
int m,n,i,j,sum,a[10][10];
void sum_of_rows()
{
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
printf("sum of row %d=%d\n",i,sum);
}
}
void sum_of_columns()
{
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<m;i++)
{
sum+=a[i][j];
}
printf("sum of column %d= %d\n",j,sum);
}
}

9
www.ncetianz.webs.com

CCP PROGRAMS

void sum_of_elements()
{
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
}
printf("total sum = %d\n",sum);
}
void read_matrix()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void write_matrix()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
printf("\n");
}
}
}
void main()
{
clrscr();
printf("enter the size of the matrix\n");
scanf("%d %d",&m,&n);
printf("enter the elements of matrix\n");
read_matrix();
sum_of_rows();
sum_of_columns();
sum_of_elements();
getch();
}

10
www.ncetianz.webs.com

You might also like