POP Programs
POP Programs
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x1,x2,disc;
clrscr();
printf("Enter the Co-efficients\n");
scanf("%f %f %f",&a,&b,&c);
disc = b*b-4*a*c;
if(a==0||b==0||c==0)
{
printf("ERROR:Roots cannot be Determined");
}
else if(disc==0)
{
x1 = x2 =-b/(2*a);
printf("The roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
}
else if(disc>0)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The roots are distinct\n");
printf("x1=%f\nx2=%f\n",x1,x2);
}
else if(disc<0)
{
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("The roots are complex\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
}
getch();
return;
}
OUTPUT
OUTPUT
Enter the number
2332
2332 is a Palindrome
//square root
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
double i,j,n;
clrscr();
printf("enter a number\n");
scanf("%lf",&n);
j=n;
for(i=1;i<=n;i++)
{
j=(j+(n/j))/2;
}
printf("\nsquare root %lf=%lf\n",n,j);
getch();
}
OUTPUT
Enter a number
25
Square root of 25=5.000000
//Leap Year
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the Year\n");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap yaer",year);
getch();
return;
}
OUTPUT
Enter the year
2000
2000 is aleap year
OUTPUT
Enter the value for x: 90
Enter the value for n:10
User defined sin value = 1.00
Built-in Sin value = 1.00
//Bubble Sort
#include<stdio.h>
#include<conio.h>
void bubble_sort(int a[],int n);
void main()
{
int a[20],b[20],n,i;
clrscr();
printf("Enter the number of items\n");
scanf("%d",&n);
printf("Enter the items to be sorted\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
bubble_sort(a,n);
printf("ARRAY BEFORE SORTED ARRAY AFTER SORTED\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t\t\t%d\n",b[i],a[i]);
}
getch();
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
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;
}
}
}
return(0);
}
OUTPUT
Enter the number of items
5
Enter the items to be sorted
23
45
12
67
10
ARRAY BEFORE SORTED ARRAY AFTER SORTED
23 10
45 12
12 23
67 45
10 67
//Matrix Multiplication
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<math.h>
void read_matrix(int z[][10],int m,int n);
void multiply_matrix(int a[][10],int b[][10],int c[][10],int m,int n,int p,int q);
void print_matrix(int z[][10],int m,int n);
void main()
{
int m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the size of the matrix a\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix a\n");
read_matrix(a,m,n);
printf("Enter the size of the matrix b\n");
scanf("%d%d",&p,&q);
printf("Enter the elements of the matrix b\n");
read_matrix(b,p,q);
multiply_matrix(a,b,c,m,n,p,q);
printf("The product matrix is c\n");
print_matrix(c,m,q);
getch();
return;
}
}
return;
}
void multiply_matrix(int a[][10],int b[][10],int c[][10],int m,int n,int p,int q)
{
int i,j,k,sum;
if(n!=p)
{
printf("Matrix multiplication is not possible");
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;
}
}
return;
}
void print_matrix(int z[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",z[i][j]);
//printf(" ");
}
printf("\n");
}
return;
}
OUTPUT
Enter the size of the matrix a
23
Enter the elements of the matrix a
123
456
Enter the size of the matrix b
32
Enter the elements of the matrix b
12
34
56
The resultant matrix is c
22 28
49 64
//Binary Search
// Strcpy(dest,src)
#include<stdio.h>
#include<conio.h>
void my_strcpy(char dest[],char src[]);
void main()
{
char src[20];
char dest[20];
clrscr();
printf("Enter the string\n");
scanf("%s",src);
my_strcpy(dest,src);
printf("Destination string = %s\n",dest);
getch();
return;
}
void my_strcpy(char dest[], char src[])
{
int i=0;
while(src[i]!='\0')
{
dest[i]=src[i];
i++;
}
dest[i]='\0';
}
OUTPUT
Enter the String
computer
Destination String = computer
//Factorial
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n,r;
float res;
printf("Enter the value of n and r\n");
scanf("%d %d",&n,&r);
res=fact(n)/(fact(n-r)*fact(r));
printf("%dC%d = %f \n",n,r,res);
getch();
return;
}
int fact(int n)
{
if(n==0)
return 1;
return n*fact(n-1);
}
OUTPUT
Enter the value of n and r
63
6C3 = 20.000000
//Rightshift(x)
#include<stdio.h>
#include<conio.h>
int rightrot(unsigned int x,int n);
void main()
{
unsigned int x;
unsigned int res;
int n;
clrscr();
printf("Enter an unsigned integer less<=65535\n");
scanf("%u",&x);
printf("Rotate %u how many times:",x);
scanf("%d",&n);
res=rightrot(x,n);
printf("rightrot(%u,%d)=%u\n",x,n,res);
getch();
}
OUTPUT
Enter an unsigned integer less<=65535
8
Rotate 8 how many times:4
rightrot(8,4)=32768
//isprime
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter the range:");
scanf("%d%d",&n1,&n2);
printf(“Prime numbers between %d to %d\n”,n1,n2);
for(i=n1;i<=n2;i++)
{
if(isprime(i))
printf(“%d”,i);
}
}
int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
{
return 0;
}
}
return 1;
}
OUTPUT
Enter the range : 1 25
Prime numbers between 1 to 25 are
1 2 3 5 7 11 13 17 19 23