Part-A: SI. No Program Page No

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

CONTENTS

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. RIGHT ROTATION ………………………………………………………………………….


2. REVERSE OF A STRING …………………………………………………………………..
3. MATCH ANY ………………………………………………………………………………….
4. REPLACING BLANK SPACE ………………………………………………………………
5. KK
6. MULTIPLICATION OF MATRIX ………………………………………………………..
7. OMP ……………………………………………………………………………………………..
8. OMP PRIME …………………………………………………………………………………..
QUADRATIC EQUATION

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;

printf("enter the co-effecients \t:");

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)
{

printf("\n roots are real and distinct\n");

root1=(-b +sqrt(disc))/dino;

root2= (-b - sqrt(disc))/dino;

printf("\n root1\t:%2f\n root2\t:%2f\n",root1,root2);

if(disc==0)

root1= -b/dino;

printf("\n roots are real and equal\n");

printf("\n root1=root2\t:%2f",root1);

else

printf("\n complex roots\n");

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:

 Enter the co-efficient : 1 2 0


Invalid data

 Enter the co-efficient : 1 2 1


Roots are real and equal
Root1 = Root2 : -1.0000

 Enter the co-effecients : 1 2 3

complex roots

root1 :-1.000000+i1.414214

root2 :-1.000000+i1.414214

 Enter the co-effecients : 1 5 5

roots are real and distinct

root1 :-1.381966

root2 :-3.618034
GCD AND LCM (EUCLID’S ALGORITHM)

2. Design, develop and execute a program in C to implement


Euclid’s algorithm to find the GCD and LCM of two integers and to
output the results along with the given integers.
#include<stdio.h>

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

the GCD and LCD of the numbers 4 and 18 :

GCD : 2

LCM : 36
REVERSE A INTEGER & CHECK PALINDROME OR NOT

3. Design, develop and execute a program in C to reverse a given four


digit integer number and check whether it is a palindrome or not.
Output the given number with suitable message.
#include<stdio.h>
int main()
{
int rev=0,temp,num,rem;
printf("enter the number \t:");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem = num % 10;
num= num/10;
rev=rev*10+rem;
}
if (rev==temp)
printf("the given number %d is palindrome \n",temp);
else
printf("the given number %d is not a palindrome \n",temp);
}
Result:

Enter the numbers : 121

The given number 121 is a palindrome

Enter the numbers : 123

The given number 123 is not a palindrome


EVALUATION OF POLYNOMIAL (HORNER’S METHOD)

4. Design, develop and execute a program in C to evaluate the


given polynomial f(x) = a4x4+ a3x3+ a2x2+ a1x + a0 for given value of x
and the coefficients using Horner’s method

#include<stdio.h>
int main()
{
int x,i;
int coeff[30];
int degree,temp,result;
printf("enter the degree of polynomial :\t");
scanf("%d",&degree);
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:

enter the degree of polynomial : 2


enter the value of x: 3
enter the co-effecients of a 0: 1
enter the co-effecients of a 1: 1
enter the co-effecients of a 2: 1
result of polynomial evaluation is 3=13
5. Design, develop and execute a program in C to copy its input to its
output, replacing each string of one or more blanks by a single blank .
#include<stdio.h>

int main()

char src[100],dest[100];

int i=0;

int j=0;

printf("Enter the source data\t:");

gets(src);

while(src[i]!='\0')

dest[j]=src[i];

i++;

j++;

if(src[i-1]==' ')

while(src[i]==' ')

i++;

dest[j]='\0';

printf("Original string:%s\n blank replaced string:%s",src,dest);

return 0;
}

Result:
Enter the source data : hello world ?

Original string : hello world ?

Blank replaced string : hello world ?


BINARY SEARCH

6. Design, develop and execute a program in C to input N integer


numbers in ascending order into a single dimensional array and
perform a binary search for a given key integer number and report
success or failure in the form of a suitable message.
#include<stdio.h>

#include<stdlib.h>

int main()

int a[20],i,size,ele,low,mid,high;

printf("enter the size of the array\n");

scanf("%d",&size);

printf("enter the elements of the array in sorted order\n");

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

scanf("%d",&a[i]);

printf("enter the elements to be searched\t:\n");

scanf("%d",&ele);

low=0;

high=size-1;

while(low<=high)

{
mid=(low+high)/2;

if(ele==a[mid])

printf("\n binary search is successful");

exit(0);

else if(ele>a[mid])

low=mid+1;

else

high=mid-1;

printf("binary search is unsucessful");

}
Result:
 enter the size of the array

enter the elements of the array in sorted order

11

22

33

44

enter the elements to be searched :

33

binary search is successful

 enter the size of the array

enter the elements of the array in sorted order

enter the elements to be searched :

binary search is unsucessful


BUBBLE SORT

7. Design, develop and execute a program in C to input N integer


numbers into a single dimensional array, sort them in ascending
order using bubble sort technique and print both the given array
and the sorted array with suitable headings.
#include<stdio.h>

int main()

int a[20],i,size,temp,j;

printf("enter the size of the array\t:\n");

scanf("%d",&size);

printf("enter the element of the array\n");

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;

printf("the elements of the array after bubble sorting\t:\n");

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

printf("%d\n",a[i]);

return 0;

Result:
Enter the size of the array :

Enter the elements of the array

12

23

11

The elements of the array after bubble sorting :

11

12

23
WORD LENGTH

8. Design, develop and execute a program in C to compute and print


the word length on the host machine.
#include<stdio.h>

int main()

unsigned int n;

int count=0;

n=~0;

while(n!=0)

n=n<<1;

count++;

printf("word length of the computer is:\t %dbits",count);

return 0;

Result:
Word length of the computer is: 32 bits
Taylor series

9. Design, develop and execute a program in C to calculate the


approximate value of exp(0.5) using the Taylor Series expansion for
the exponential function. Use the terms in the expansion until the
last term is less than the machine epsilon defined FLT_EPSILON in
the header file <float.h>. Also print the value returned by the
Mathematical function exp( ).
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<float.h>

long int factorial(int n);

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;

printf("the value of exp(0.5) using library function is %f\n",1+sum);

printf(" the value of exp(0.5) using user defined function is %f",exp(0.5));

long int factorial(int n)

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

the value of exp(0.5) using user defined function is 1.648721


Multiplication of matrix
10. Design, develop and execute a program in C to read two matrices A
(M x N) and B (P x Q) and compute the product of A and B if the
matrices are compatible for multiplication. The program must print
the input matrices andthe resultant matrix with suitable headings
and format if the matrices are compatible for multiplication,
otherwise the program must print a suitable message. (For the
purpose of demonstration, the array sizes M, N, P, and Q can all be less
than or equal to 3).
#include<stdio.h>

int main()

int r1,r2,c1,c2,i,j,k;

int a[10][10],b[10][10],c[10][10];

printf("enter the order of matrix a\n");

scanf("%d%d",&r1,&c1);

printf("enter the order of matrix b\n");

scanf("%d%d",&r2,&c2);

printf("enter the elements of matrix a\n");

if(c1==r2)

for(i=0;i<r1;i++)
for(j=0;j<c1;j++)

scanf("%d",&a[i][j]);

printf("enter the elements of matrix of b\n");

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

printf("elements of matrix a\n");

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

for(j=0;j<c1;j++)

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

printf("\n");

printf("elements of matrix of b\n");

for(i=0;i<r2;i++)
{

for(j=0;j<c2;j++)

printf("%d\t",b[i][j]);

printf("\n");

printf("the product of matrix a and matrix b :\n");

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

for(j=0;j<c2;j++)

printf("%d\t",c[i][j]);

printf("\n");

else

printf("The matrix multiplication is not possible");

return 0;

}
Result:
 enter the order of matrix a

22

enter the order of matrix b

22

enter the elements of matrix a

22

22

enter the elements of matrix of b

22

22

elements of matrix a

2 2

2 2

elements of matrix of b

2 2

2 2

the product of matrix a and matrix b :

8 8

8 8
 enter the order of matrix a

11

enter the order of matrix b

enter the elements of matrix a

The matrix multiplication is not possible


11. Design, develop and execute a parallel program in C to add,
element-wise, two one-dimensional arrays A and B of N integer
elements and store the result in another one-dimensional array C of N
integer elements.
#include<stdio.h>

#include<omp.h>

int main()

float a[10000],b[10000],c[10000];

int size,i,chunk=5,tid;

printf("enter the size\n");

scanf("%d",&size);

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

a[i]=1.0;

b[i]=2.0;

#pragma omp parallel

tid=omp_get_thread_num();

#pragma omp for schedule(dynamic,chunk)


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

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

thread 0, c[0]= 3.000000

thread 0, c[1]= 3.000000

thread 0, c[2]= 3.000000

thread 0, c[3]= 3.000000

thread 0, c[4]= 3.000000

thread 0, c[5]= 3.000000

thread 0, c[6]= 3.000000

thread 0, c[7]= 3.000000

thread 0, c[8]= 3.000000

thread 0, c[9]= 3.000000


Right rotation

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>

unsigned int rightrotate(unsigned int x,int n);

int main()

unsigned int x;

int n, flag;

printf("enter the number which has to be right rotated");

scanf("%d",&x);

printf("enter the number of rotations in which rightrotation is performed");

scanf("%d",&n);

flag=rightrotate(x,n);

printf("rightrotation of %d and %d\t is %d",x,n,flag);

return 0;

unsigned int rightrotate(unsigned int x,int n)

{
return((x<<n)|(x>>32-n));

Result:
enter the number which has to be right rotated :16

enter the number of rotations in which rightrotation is performed :2

rightrotation of 16 and 2 is 64
CHECK PRIME OR NOT

13. Design and develop a function isprime (x) that accepts an


integer argument and returns 1 if the argument is prime and 0
otherwise. The function must use plain division checking approach to
determine if a given number is prime. Invoke this function from
the main with different values obtained from the user and print
appropriate messages.
#include<stdio.h>

int isprime(int x);

int main()

int x,flag;

printf(" enter the value of number");

scanf("%d",&x);

flag=isprime(x);

if(flag==1)

printf(" the number %d is prime number",x);

else

printf(" the number %d is not prime number ",x);

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

the number 7 is prime number

enter the value of x : 9

the number 9 is not prime number


14. Design, develop and execute a parallel program in C to
determine and print the prime numbers which are less than 100
making use of algorithm of the Sieve of Eratosthenes.

#include<stdio.h>

#include<math.h>

#include<omp.h>

int main()

int a[10000],n,i,k;

printf("enter the value of n");

scanf("%d",&n);

for(i=2;i<=n;i++)

a[i]=1;

#pragma omp parallel

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;

printf("prime numbers are\n");

for(i=2;i<=n;i++)

if(a[i])

printf("%d\n",i);

return 0;

Result:
Enter the value of n : 20

Prime numbers are

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>

void reverse(char src[100]);

char dest[100];

int main()

char src[100];

printf("enter the string");

scanf("%s",src);

reverse(src);

printf("the reversed string of the original string: %s is %s",src,dest);

return 0;

void reverse(char src[100])

{
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

the reversed string of the original string is : hello is olleh


16. Design and develop a function match any (s1,s2) which returns
the first location in the string s1 where any character from the string s2
occurs, or – 1 if s1 contains no character from s2. Do not use the
standard library function which does a similar job! Invoke the
function match any (s1. s2) from the main for different strings and
print both the strings and the return value from the function match any
(s1,s2).

#include<stdio.h>

int matchany(char s1[10],char s2[10]);

int main()

int pos;

char s1[10],s2[10];

printf("enter the main string\t:\n");

scanf("%s",s1);

printf("enter the sub string\t:\n");

scanf("%s",s2);

pos=matchany(s1,s2);

if(pos==-1)

printf("the string s1and s2 does not match\n");

else
printf("match found at position %d",pos);

return 0;

int matchany(char s1[10],char s2[10])

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

enter the sub string :

welcome

match found at position 2

 enter the main string :

abc

enter the sub string :

xyz

the string s1and s2 does not match

You might also like