0% found this document useful (0 votes)
19 views13 pages

POP Programs

The document contains various C programs demonstrating fundamental programming concepts such as calculating roots of quadratic equations, reversing integers, finding square roots, checking leap years, evaluating polynomials, calculating sine values, sorting arrays, multiplying matrices, performing binary search, copying strings, counting vowels and consonants, calculating factorials, right rotating integers, and identifying prime numbers. Each program includes user input and outputs the results accordingly. The document serves as a practical guide for understanding basic algorithms and data manipulation in C.

Uploaded by

PriyaSrihari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

POP Programs

The document contains various C programs demonstrating fundamental programming concepts such as calculating roots of quadratic equations, reversing integers, finding square roots, checking leap years, evaluating polynomials, calculating sine values, sorting arrays, multiplying matrices, performing binary search, copying strings, counting vowels and consonants, calculating factorials, right rotating integers, and identifying prime numbers. Each program includes user input and outputs the results accordingly. The document serves as a practical guide for understanding basic algorithms and data manipulation in C.

Uploaded by

PriyaSrihari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

//Roots of a Quadratic Equation

#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

Enter the Co-efficients


0.4 0.6 0.9
The roots are complex
The first root=-0.750000+i1.299038
The second root=-0.750000-i1.299038

Enter the Co-efficients


0.2 -0.5 -0.7
The roots are distinct
x1=3.500000
x2=-1.000000

Enter the Co-efficients


1 2 1
The roots are equal
x1=-1.000000
x2=-1.000000
//Reverse of an integer
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
m=n;
rev=0;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=digit+10*rev;
}
printf(“The Reverse of the givrn number is %d”,rev);
if(m==rev)
printf("%d is a Palindrome\n",n);
else
printf("%d is not a Palindrome\n",n);
getch();
}

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

Enter the year


2014
2014 is not a leap year
//Polynomial Evaluation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],x,n,i=0,result;
clrscr();
printf("\n Enter maximum power of n\n");
scanf("%d",&n);
printf("enter coefficients of polynomial\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter X\n");
scanf("%d",&x);
result=a[n]*x;
for(i=n-1;i>=1;i--)
result=(result+a[i])*x;
result=result+a[0];
printf("\n va;ue of the polynomial=%d",result);
getch();
}
OUTPUT
Enter maximum power of n
4
Enter coefficients of polynomial
2 3 2 4 1
Enter X
2
Value of the polynomial=64
//Finding Sin(x)
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
int i, n ;
float x, val, sum, t ;
clrscr() ;
printf("Enter the value for x : ") ;
scanf("%f", &x) ;
printf("\nEnter the value for n : ") ;
scanf("%d", &n) ;
x = x * 3.14159 / 180 ;
sum = t = x ;
for(i = 1 ; i < n + 1 ; i++)
{
t = (t * pow((double) (-1), (double) (2 * i - 1)) *
x * x) / (2 * i * (2 * i + 1)) ;
sum = sum + t ;
}
printf("\n User defined Sine value = %6.2f\n", sum) ;
printf(" Built-in Sin value = %6.2f\n",sin(x));
getch() ;
}

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

void read_matrix(int z[][10],int m,int n)


{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&z[i][j]);
}

}
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

//Count of vowels and consonants


#include<stdio.h>
#include<conio.h>
#include<String.h>
void main()
{
char str[40],ch;
int i, vowel_count,const_count;
clrscr();
printf("Enter the sentence\n");
gets(str);
vowel_count=const_count=0;
for(i=0;i<strlen(str);i++)
{
if(isalpha(str[i]))
{
ch=tolower(str[i]);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vowel_count++;
else
const_count++;
}
}
printf("No.of vowels = %d\n",vowel_count);
printf("No. of consonants = %d\n",const_count);
getch();
return;
}
OUTPUT
Enter the sentence
how are you
No. of Vowels = 5
No. of Consonants = 4

//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();
}

int rightrot(unsigned int x,int n)


{
int i,right_bit,left_bit;
for(i=1;i<=n;i++)
{
if(x%2==0)
x=x>>1;
else
x=x>>1,x+=32768;
}
return x;
}

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

You might also like