Part-A: SI. No Program Page No
Part-A: SI. No Program Page No
Part-A: SI. No Program Page No
PART-A
1) QUADRATIC EQUATION ……………………………………………………………….
2) GCD AND LCM (EUCLID’S ALGORITHM) ………………………………………..
3) REVERSE A INTEGER & CHECK PALINDROME OR NOT …………………..
4) EVALUATION OF POLYNOMIAL (HORNER’S METHOD) …………………..
5) WORD LENGTH …………………………………………………………………………….
6) BINARY SEARCH……………………………………………………………………………
7) BUBBLE SORT……………………………………………………………………………….
8) CHECK PRIME OR NOT…………………………………………………………………..
PART-B
1. Design, develop and execute a program in C to find and output all the
roots of a given quadratic equation, for non-zero coefficients
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
float a,b,c,disc,dino,root1,root2,imagpart,realpart;
scanf("%f%f%f",&a,&b,&c);
if(a*b*c==0)
printf("invalid data\n");
exit(0);
disc=b*b-4*a*c;
dino=2*a;
if(disc>0)
{
root1=(-b +sqrt(disc))/dino;
if(disc==0)
root1= -b/dino;
printf("\n root1=root2\t:%2f",root1);
else
imagpart= sqrt(abs(disc))/dino;
realpart= -b/dino;
printf("\n root1\t:%2f+i%2f\n",realpart,imagpart);
printf("\n root2\t:%2f+i%2f\n",realpart,imagpart);
return 0;
}
Result:
complex roots
root1 :-1.000000+i1.414214
root2 :-1.000000+i1.414214
root1 :-1.381966
root2 :-3.618034
GCD AND LCM (EUCLID’S ALGORITHM)
int main()
{
int m,n,r,gcd,lcm,num1,num2;
printf("enter the numbers \t:");
scanf("%d%d",&num1,&num2);
m=num1;
n=num2;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm= (num1 * num2)/gcd;
printf("the GCD and LCD of the numbers %d and %d :\n GCD\t:%d\n
LCM\t: %d\n",num1,num2,gcd,lcm);
}
Result:
enter the numbers : 4 18
GCD : 2
LCM : 36
REVERSE A INTEGER & CHECK PALINDROME OR NOT
#include<stdio.h>
int main()
{
int x,i;
int coeff[30];
int degree,temp,result;
printf("enter the degree of polynomial :\t");
scanf("%d",°ree);
printf("enter the value of x:\t");
scanf("%d",&x);
for(i=0;i<=degree;i++)
{
printf("enter the co-effecients of a %d: \t",i);
scanf("%d",&coeff[i]);
}
temp=coeff[degree];
for(i=degree-1;i>=0;i--)
temp=coeff[i]+x*temp;
result=temp;
printf("result of polynomial evaluation is %d=%d",x,result);
}
Result:
int main()
char src[100],dest[100];
int i=0;
int j=0;
gets(src);
while(src[i]!='\0')
dest[j]=src[i];
i++;
j++;
if(src[i-1]==' ')
while(src[i]==' ')
i++;
dest[j]='\0';
return 0;
}
Result:
Enter the source data : hello world ?
#include<stdlib.h>
int main()
int a[20],i,size,ele,low,mid,high;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
scanf("%d",&ele);
low=0;
high=size-1;
while(low<=high)
{
mid=(low+high)/2;
if(ele==a[mid])
exit(0);
else if(ele>a[mid])
low=mid+1;
else
high=mid-1;
}
Result:
enter the size of the array
11
22
33
44
33
int main()
int a[20],i,size,temp,j;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=1;i<size;i++)
for(j=0;j<size-i;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
for(i=0;i<size;i++)
printf("%d\n",a[i]);
return 0;
Result:
Enter the size of the array :
12
23
11
11
12
23
WORD LENGTH
int main()
unsigned int n;
int count=0;
n=~0;
while(n!=0)
n=n<<1;
count++;
return 0;
Result:
Word length of the computer is: 32 bits
Taylor series
#include<stdlib.h>
#include<math.h>
#include<float.h>
int main()
float x,sum,term;
int i;
sum=0;
term=10;
x=0.5;
i=0;
while(term>FLT_EPSILON)
{
i++;
term=(float)pow(x,i)/(float)factorial(i);
sum=sum+term;
if(n<=1)
return 1;
else
n=n*factorial(n-1);
return n;
Result:
the value of exp(0.5) using library function is 1.648721
int main()
int r1,r2,c1,c2,i,j,k;
int a[10][10],b[10][10],c[10][10];
scanf("%d%d",&r1,&c1);
scanf("%d%d",&r2,&c2);
if(c1==r2)
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
printf("%d\t",a[i][j]);
printf("\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",b[i][j]);
printf("\n");
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
printf("%d\t",c[i][j]);
printf("\n");
else
return 0;
}
Result:
enter the order of matrix a
22
22
22
22
22
22
elements of matrix a
2 2
2 2
elements of matrix of b
2 2
2 2
8 8
8 8
enter the order of matrix a
11
#include<omp.h>
int main()
float a[10000],b[10000],c[10000];
int size,i,chunk=5,tid;
scanf("%d",&size);
for(i=0;i<size;i++)
a[i]=1.0;
b[i]=2.0;
tid=omp_get_thread_num();
c[i]=a[i]+b[i];
printf("thread %d:c[%d]=%d\n",tid,i,c[i]);
return 0;
Result:
enter the size of the array : 10
12. Design and develop a function rightrot (x, n) in C that returns the
value of the integer x rotated to the right by n bit positions as an
unsigned integer. Invoke the function from the main with different
values for x and n and print the results with suitable headings.
#include<stdio.h>
int main()
unsigned int x;
int n, flag;
scanf("%d",&x);
scanf("%d",&n);
flag=rightrotate(x,n);
return 0;
{
return((x<<n)|(x>>32-n));
Result:
enter the number which has to be right rotated :16
rightrotation of 16 and 2 is 64
CHECK PRIME OR NOT
int main()
int x,flag;
scanf("%d",&x);
flag=isprime(x);
if(flag==1)
else
return 0;
int isprime(int x)
{
int i;
for(i=2;i<=x/2;i++)
if(x%i==0)
return 0;
return 1;
Result:
enter the value of number : 7
#include<stdio.h>
#include<math.h>
#include<omp.h>
int main()
int a[10000],n,i,k;
scanf("%d",&n);
for(i=2;i<=n;i++)
a[i]=1;
omp_get_thread_num();
for(k=2;k<=sqrt(n);k++)
if(a[k]!=0)
#pragma for
{
for(i=k*k;i<=n;i+=k)
a[i]=0;
for(i=2;i<=n;i++)
if(a[i])
printf("%d\n",i);
return 0;
Result:
Enter the value of n : 20
1
Reverse of a string
15. Design and develop a function reverses (s) in C to reverse the string
s in place. Invoke this function from the main for different strings
and print the original and reversed strings.
#include<stdio.h>
char dest[100];
int main()
char src[100];
scanf("%s",src);
reverse(src);
return 0;
{
int length=0,j=0,i;
while(src[length]!='\0')
length++;
for(i=length-1;i>=0;i--)
dest[j]=src[i];
j++;
dest[j]='\0';
Result:
enter the string : hello
#include<stdio.h>
int main()
int pos;
char s1[10],s2[10];
scanf("%s",s1);
scanf("%s",s2);
pos=matchany(s1,s2);
if(pos==-1)
else
printf("match found at position %d",pos);
return 0;
int i,j;
for(i=0;s2[i]!='\0';i++)
for(j=0;s1[j]!='\0';j++)
if(s2[i]==s1[j])
return (j+1);
return -1;
}
Result:
enter the main string :
hello
welcome
abc
xyz