You are on page 1of 19

COMPUTER PROGRAMMING LABORATORY Subject Code : 10CPL16 / 10CPL26 PART A 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<conio.h> #include<math.h> void main() { float a,b,c,disc,r1,r2,realp,imagp; printf("enter the non zero coefficients\n"); scanf("%f%f%f",&a,&b,&c); if(a==0|b==0||c==0) printf("renter the non zero coefficients\n"); else { disc=b*b-4*a*c; if(disc>0) { printf("roots r real and distinct\n"); r1=(-b+sqrt(disc))/(2*a); r2=(-b-sqrt(disc))/(2*a); printf("root1=%f",r1); printf("root2=%f",r2); } else if(disc<0) { printf("roots r imaginary\n"); realp=-b/(2*a); imagp=sqrt(fabs(disc)))/(2*a); printf("root1 are %f+i%f",realp,imagp); printf("root1 are %f-i%f",realp,imagp); } else { printf("roots are real and equal\n"); r1=-b/(2*a); r2=r1; printf("root1=%f",r1); printf("root2=%f",r2); } } getch(); }

2.

Design, develop and execute a program in C to implement Euclids algorithm to find the GCD and LCM of two integers and to output the results along with the given integers.

#include<stdio.h> #include<conio.h> void main() { int r,m,n,p,q,gcd,lcm; printf("enter two no's"); 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\n",gcd); printf("lcm=%d",lcm); getch(); }

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> #include<conio.h> void main() { int n,rev,dec,temp; printf("enter a number"); scanf("%d",&n); temp=n; rev=0; while(n!=0) { dec=n%10; rev=rev*10+dec; n=n/10; } printf("reversed num=%d",rev); if(rev==temp) printf("num is palindrome\n"); else printf("num is not a palindrome\n"); getch(); }

4.

Design, develop and execute a program in C to evaluate the given polynomial f(x) = a 4x4 + a3x3 + a2x2 + a1x + a0 for given value of x and the coefficients using Horners method.

#include<stdio.h> #include<conio.h> void main() { int a[10],i,n,x,sum=0; clrscr(); printf("enter the value of x\n"); scanf("%d",&x); printf("enter the value of n\n"); scanf("%d",&n); printf("enter the elements\n"); for(i=n;i>=0;i--) { scanf("%d",&a[i]); } for(i=n;i>=0;i--) { sum=a[i]+sum*x; } printf("the sum is %d",sum); getch(); }

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> #define BLANK ' ' void main() { char src[100],dst[100]; int i,j; printf("enter a string:"); gets(src); i=0; j=0; while(src[i]!='\0') { dst[j]=src[i]; i++; j++; if(src[i-1]==BLANK) { while(src[i]==BLANK) i++; } dst[j]='\0'; } printf("string without blanks\n"); puts(dst); }

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<conio.h> #include<process.h> void main() { int a[100],n,i; int low,mid,high,key; printf("enter size of array"); scanf("%d",&n); printf("enter the elements in sorted order\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter key to be searched\n"); scanf("%d",&key); low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) { printf("key found"); getch(); exit(0); } else if(key<a[mid]) high=mid-1; else low=mid+1; } printf("key not found"); getch(); }

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> #include<conio.h> void main() { int a[100],n,i,j,temp; printf("enter size of array"); scanf("%d",&n); printf("enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<(n-i-1);j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }

printf("sorted elements are\n"); for(i=0;i<n;i++) printf("%d\n",a[i]);\ getch(); }

8. Design, develop and execute a program in C to compute and print the word length on the host machine.

#include<stdio.h> void main() { unsigned int i=1; int count=0,n; while(i!=0) { i=i<<1; count++; printf("i=%u\n",i); } printf("word length=%d",count); }

9.

PART B 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<conio.h> #include<math.h> #include<float.h> int fact(int n) { int i,y; y=n; for(i=1;i<n;i++) y*=(n-i); return y; }

double myexp(int x) { int i; double sum,term; sum=1; for(i=1;;i++) { term=pow(x,i)/fact(i); if(term<FLT_EPSILON) break; sum=sum+term; } return sum; } int main() { int n; float x; double sum; printf("enter value of x \n"); scanf("%f",&x); sum=myexp(x); printf("e^%f=%lf\n",x,sum); printf("%f",exp(x)); getch(); }

10. Design, develop and execute a program in C to read two matrices A (M xN) 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 and the 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> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q; clrscr(); printf("enter row and col of mat A"); scanf("%d%d",&m,&n); printf("enter row and col of mat B"); scanf("%d%d",&p,&q); if(n==p) { printf("enter the matrix a elements"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("enter the matrix b elements"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); printf("matrix A\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%3d",a[i][j]); } printf("\n"); } printf("matrix B\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { printf("%3d",b[i][j]); } printf("\n"); } printf("matrix A\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%3d",a[i][j]);

} printf("\n"); } for(i=0;i<m;i++) for(j=0;j<q;j++) c[i][j]=0; for(i=0;i<m;i++) for(j=0;j<q;j++) for(k=0;k<n;k++) c[i][j]+=a[i][k]*b[k][j]; printf("product matrix\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) { printf("%3d",c[i][j]); } printf("\n"); } } else printf("multiplication not possible"); getch(); }

11. Design, develop and execute a parallel program in C to add, elementwise, 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> void main() { int tid,n,i; int a[1000],b[1000],c[1000];

printf("enter the size of the array"); scanf("%d",&n); for(i=0;i<n;i++) { a[i]=1; b[i]=2; } #pragma omp parallel num_threads(50) { #pragma omp for for(i=0;i<n;i++) { tid=omp_get_thread_num(); c[i]=a[i]+b[i]; printf("Thread %d: c[%d]=%d\n",tid,i,c[i]); } } }

To compile

cc fopenmp lab11.c to run ./a.out

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> #include<conio.h> unsigned int rightrot(unsigned int x,int n) { int i; for(i=1;i<=n;i++) { if(x%2==0) x=x>>1; else { x=x>>1; x+=32768; } } return x; } int main() { unsigned int x; int n; unsigned int res; printf("enter the integer no<=65535\n"); scanf("%u",&x); printf("enter how many times to rotate:"); scanf("%u",&n); res=rightrot(x,n); printf("result=%u",res); getch(); }

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> #include<conio.h> #include<process.h> #include<ctype.h> int isprime(int num) { int i; for(i=2;i<num/2;i++) { if(num%i==0) return 0; } return 1; } void main() { int num,res; printf("enter num"); scanf("%d",&num); res=isprime(num); if(res==1) printf("number is prime"); else printf("number is not a prime"); getch(); }

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<omp.h>

main() { int a[1000]; int n; int i,k; int tid,icpu;

printf("enter the value of n\n"); scanf("%d",&n);

for(i=2;i<=n;i++) a[i]=1;

icpu=omp_get_num_procs(); printf("the prime numbers are\n");

#pragma omp parallel num_threads(50) { for(k=2;k<=sqrt(n);k++) { if(a[k]!=0) { for(i=k*k;i<=n;i+=k)

a[i]=0; } } #pragma omp for for(i=2;i<=n;i++) { tid=omp_get_thread_num(); if(a[i]) printf("thread %d=%d\n",tid,i); } } }

To compile

cc fopenmp -lm lab14.c to run ./a.out

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> int strl(char src[]) { int n,i; for(i=0;src[i]!='\0';i++) {} return i; } void reverses(char src[],char dest[]) { int i,n; n=strl(src); for(i=0;i<n;i++) { dest[n-1-i]=src[i]; } dest[n]='\0'; } void main() { char s1[100],d1[100]; printf("enter string1: "); gets(s1); reverses(s1,d1); printf("Source string Reversed string\n"); printf("%s %s\n",s1,d1); getch(); }

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 str1[],char str2[]) { int i,j; char symbol; for(i=0;str1[i]!='\0';i++) { symbol=str1[i]; for(j=0;str2[j]!='\0';j++) { if(symbol==str2[j]) return i+1; } } return -1; } void main() { char str1[200],str2[200]; int pos; printf("enter the first string: "); gets(str1); printf("enter the second string: "); gets(str2); pos=matchany(str1,str2); if(pos==-1) printf("no character of %s is present in %s\n",str2,str1); else printf("position=%d\n",pos); getch(); }

You might also like