1.design, Develop & Execute A Program in C To Find and Output All The Roots of A Given Quadratic Equation, For Non Zero Coefficients.

You might also like

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

/*1.

Design,develop & 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<math.h> int main() { float a,b,c,disc,x1,x2; printf("Enter the value of a,b,c\n"); scanf("%f%f%f",&a,&b,&c); disc=(b*b)-(4*a*c); /*Find the discreminent */

if(disc>0) { printf("Roots are real and distinct\n"); x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf("x1=%f \t x2=%f",x1,x2); } else if(disc==0) { printf("Roots are real and equal\n"); x1=x2=-b/(2*a); printf("x1=%f \t x2=%f",x1,x2); } else { printf("Complex and imaginary roots\n"); x1=-b/(2*a); x2=sqrt(fabs(disc))/(2*a); printf("x1+ix2=%f+i%f",x1,x2); } }

/*OUTPUT:
Enter the value of a,b,c 2 3 1 Roots are real and distinct X1=-0.500000 x2=-1.000000 Enter the value of a,b,c 1 2 1 Roots are real and equal x1=-1.000000 x2=-1.000000 Enter the value of a,b,c 4 2 1 complex and imaginary roots x1+ix2=-0.250000+i0.433013

*/

/*2.Design,develop and execute a program in C to implement Euclid's algorithm to find the GCD and LCM of 2 integers and output the results along with the given integers */ #include<stdio.h> int main() { int m,n,a,b; int rem,gcd,lcm; printf("Enter m and n\n"); scanf("%d%d",&m,&n); a=m; b=n; while(n!=0) { rem=m%n; m=n; n=rem; } gcd=m; lcm=(a*b)/gcd; printf("GCD=%d, LCM=%d",gcd,lcm); }

/* find the remainder */ /* copy n to m */ /* copy remainder to n */

/*OUTPUT:
Enter m and n 24 36 GCD=12, LCM=72

*/

/*3.Design,develop & execute a program in C to reverse a given 4 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 dig,rev,n,x; printf("Enter the value of n \n"); scanf("%d",&n); x=n; rev=0; while(n!=0) { dig=n%10; rev=(rev*10)+dig; n=n/10; }

/*Obtain least significant digit*/ /*reverse the given No*/

if(x==rev) printf("Number is palindrome & reverse is:%d\n",rev); else printf("Number is not a palindrome & the reverse is %d\n",rev); }

/*OUTPUT:
Enter the value of n 1234 Number is not a palindrome & the reverse is:4321 Enter the value of n 1221 Number is palindrome & the reverse is:1221

*/

/*4.Design,develop & execute a program in C to evaluate the given polynomial f(x)=a4x^4+a3x^3+a2x^2+a1x+a0 for a given value of x and the coefficients using the Horner's method. */ #include<stdio.h> int main() { int n,x,a[30],sum,i; printf("Enter the degree of polynomial : \n"); scanf("%d",&n); printf("Enter the n+1 coefficients : \n"); for(i=0;i<=n;i++) scanf("%d",&a[i]); printf("Enter the value of x : \n"); scanf("%d",&x); sum=a[n]*x; for(i=(n-1);i>0;i--) { sum=(sum+a[i])*x; } sum=sum+a[0]; printf("Evaluated result= %d \n",sum); }

/*OUTPUT:
Enter the 3 Enter the 2 2 2 2 Enter the 2 Evaluated degree of polynomial : n+1 coefficients :

value of x : result= 30 */

/*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> #include<string.h> int main() { int i,j=0; /* j is First index of destination string*/ char input[50],output[50]; printf("Enter the input string\n"); scanf("%[^\n]",input); for(i=0;i<strlen(input);i++) { if(input[i]==' ' && input[i+1]!=' ') /*If there is a */ output[j++]=input[i]; /* single space followed by a character, copy that space too*/ else if(input[i]!=' ') /* If not space(means a character)*/ output[j++]=input[i]; /* copy it to output */ } output[j]='\0'; /* Null terminate the string*/ printf("The input string with single blank space= %s",output); }

/*OUTPUT:
Enter the input string hai what doing The input string with single blank space= hai what doing */

/*6.Design,develop and execute a program in C to input N integer numbers in ascending order into a single dimensional array and perform binary search for a given key integer number and report success or failure in the form of a suitable message */ #include<stdio.h> int main() { int n,a[50],i,flag=0; int low,high,key,mid; printf("Enter the number of elements :\n"); scanf("%d",&n); printf("Enter the array elements in ascending order:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the key element to be searched :\n"); scanf("%d",&key); low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) /*If item found,come out of the loop */ { flag=1; break; } else if(key>a[mid]) /*To Search Right part */ low=mid+1; else /*To Search Left part */ high=mid-1; } if(flag==1) printf("Successful search, %d found at position %d\n",key,mid+1); else printf("Unsuccessful search, %d not found \n",key); }

/*OUTPUT 1:
Enter the number of elements : 5 Enter the array elements in ascending order: 11 22 33 44 55 Enter the key element to be searched : 44 Successful search, 44 found at position 4

OUTPUT 2
Enter the number of elements : 5 Enter the array elements in ascending order: 11 22 33 44 55 Enter the key element to be searched : 26 Unsuccessful search,26 not found */

/*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 sorted array with suitable headings. */ #include<stdio.h> int main() { int i,j,a[50],n,temp; printf("Enter the number of elements to sort :\n"); scanf("%d",&n); printf("Enter the array elements: \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("The input array is\n"); for(i=0;i<n;i++) printf("%d\t",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("\nThe sorted array for(i=0;i<n;i++) printf("%d\t",a[i]); } is:\n");

/*OUTPUT:
Enter the number of elements to sort : 5 Enter the array elements: 33 1 26 485 2 The input array is 33 1 26 The sorted array is: 1 2 26 485 33 2 485 */

/*8.Design,develop & execute a program in C to compute and print the world length on the host machine. */ #include<stdio.h> int main() { unsigned int n,count=0; n=~0; while(n!=0) { n=n>>1; count++; } printf("The word length is:%d bits",count); }

/*OUTPUT:
The world length is 32 bits */

/*9.Design,develop & execute a program in C to calculate the approximate value of exp(0.5) using Tylor 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<float.h> #include<math.h> int factorial(int num) { int i,fact=1; for(i=1;i<=num;i++) { fact=fact*i; } return fact; } int main() { float x,res=1,term=1; int i; printf("Enter the of x :\n"); scanf("%f",&x); for(i=1;term>=FLT_EPSILON;i++) { term=pow(x,i)/factorial(i); res=res+term; } printf("Approximate value of exp(%f) is %f \n",x,res); printf("exp(%f) from math function is %f \n",x,exp(x)); }

/*OUTPUT:
Enter the of x : 1 Approximate value of exp(1.000000) is 2.718215 exp(1.000000) from math function is 2.718282 Enter the of x : 0.5 Approximate value of exp(0.500000) is 1.648721 exp(0.500000) from math function is 1.648721 */

/*10.Design,develop & execute a program in C to read 2 matrices A(MXN) and B(PXQ) 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.(Assume 3X3 matrices) */ #include<stdio.h> #include<stdlib.h> int main() { int i,j,p,q,k,m,n,a[20][20],b[20][20],c[20][20],sum; printf("Enter the size of matrix A :\n"); scanf("%d%d",&m,&n); printf("Enter the elements of matrix A: \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("Enter the size of matrix B :\n"); scanf("%d%d",&p,&q); printf("Enter the elements of matrix B :\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { scanf("%d",&b[i][j]); } } printf("Matrix Elements of A :\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",a[i][j]); } printf("\n"); }

printf("Matrix Elements of B :\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { printf("%d\t",b[i][j]); } printf("\n"); } if(n!=p) { printf("Matrix multiplication not possible\n"); 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; } } printf("Product of two matrix A and B :\n"); for(i=0;i<m;i++) { for(j=0;j<p;j++) { printf("%d\t",c[i][j]); } printf("\n"); } }

/*OUTPUT:
Enter the size of matrix A : 3 3 Enter the elements of matrix A: 1 2 3 1 2 3 1 2 3 Enter the size of matrix B : 3 3 Enter the elements of matrix B : 4 5 6 4 5 6 4 5 6 Matrix Elements of A : 1 2 3 1 2 3 1 2 3 Matrix Elements of B : 4 5 6 4 5 6 4 5 6 Product 24 24 24 of two matrix A and B : 30 36 30 36 30 36 */

/* 11.Design,develop & execute a program in C to add element-wise 2 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() { int n,i,chunk,tid; float a[100],b[100],c[100]; printf("Enter the size of the array :\n"); scanf("%d",&n); for(i=0;i<n;i++) { a[i]=1.0; b[i]=3.0; } chunk=5; #pragma omp parallel { tid=omp_get_thread_num(); #pragma omp for schedule(dynamic,chunk) for(i=0;i<n;i++) { c[i]=a[i]+b[i]; printf("Thread %d : c[%d] = %f \n",tid,i,c[i]); } } } /*To compile the program type the following command $> cc fopenmp filename.c To run the file type $> ./a.out Enter the size of the array: 5 Thread 1:c[0]=3.0 Thread 1:c[1]=3.0 Thread 1:c[2]=3.0 Thread 1:c[3]=3.0 Thread 1:c[4]=3.0

/*12.Design & 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 rightrot(unsigned int x,unsigned int n) { int i; for(i=1;i<=n;i++) { if(x%2==0) x=x>>1; else { x=x>>1; x=x+32768; } } return x; } int main() { unsigned int n,x,res; printf("Enter the value of unsigned integer x \n"); scanf("%u",&x); printf("Enter the number of rotation n scanf("%d",&n); res=rightrot(x,n); printf("Rightrot(%u,%u)=%u \n",x,n,res); } \n");

/*OUTPUT:
Enter the value of unsigned integer x 32000 Enter the number of rotation n 2 Rightrot(32000,2)=8000 */

/*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 i; for(i=2;i<=x/2;i++) { if(x%i==0) { return 0; /* Number is not prime */ } } return 1; } int main() { int x; printf("Enter the number to check, it is prime or not:\n"); scanf("%d",&x); if(isprime(x)) printf("%d is a prime number\n",x); else printf("%d is not a prime number\n",x); } /* Number is prime */

/* OUTPUT:
Enter the number to check,it is prime or not: 47 47 is a prime number Enter the number to check,it is prime or not: 63 63 is not a prime number */

/*14.Design,develop & execute a parallel program in C to determine & print the prime numbers which are less than 100 making use of algorithm of Sieve of Eratosthenes. */ #include<stdio.h> #include<math.h> #include<omp.h> int main() { int a[50],n,i,j; printf("Enter the value of n\n"); scanf("%d",&n); for(i=0;i<=n;i++) a[i]=1; #pragma omp parallel { omp_get_thread_num(); for(k=2;k<=sqrt(n);k++) { if(a[k]!=0) /*If it is not zero*/ { for(i=k*k;i<=n;i+=k) a[i]=0; /*Eliminate multiples of k } } } printf("Prime Numbers are:"); for(i=2;i<=n;i++) { if(a[i]) /*If not zero */ printf("%d\t",i); } } /* To compile,type $> cc fopenmp lm filename.c To run type $> ./a.out Enter the value of n: Prime Numbers are: 2 20 3 5 7 11 13 17 19 */

/*15.Design,develop a function reverse(s) in C to reverse the string s inplace. Invoke this function from the main for different strings and print the original & reverse strings. */ #include<stdio.h> #include<string.h> int reverse(char s[40]) { int i,len=0,j; char temp; for(i=0;s[i]!='\0';i++) len++; printf("The given string is: %s \n",s); for(i=0,j=len-1;i<j;i++,j--) { temp=s[i]; /*exchange ith letter with */ s[i]=s[j]; /* jth letter as long as i<j */ s[j]=temp; } s[len]='\0'; printf("The reversed string is:%s\n",s); } int main() { char s[40]; printf("Enter the string :\n"); scanf("%s",s); reverse(s); }

/*OUTPUT:
Enter the string : Dhavala The given string is: Dhavala The reversed string is: alavahD */

/*16.Design and develop a function matchany(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 matchany(s1,s2) from the main for different strings and print both the strings and return value from the function matchany(s1,s2). */ #include<stdio.h> #include<string.h> int matchany(char s1[50],char s2[50]) { int pos=-1,i,j,len1,len2; len1=strlen(s1); len2=strlen(s2); for(i=0;i<len2;i++) { for(j=0;j<len1;j++) { if(s2[i]==s1[j]) /*if there is a match */ { pos=j; /* note down the position & break*/ break; } } } return pos; } int main() { int pos; char s1[50],s2[50]; printf("Enter the first string :\n"); scanf("%s",s1); printf("Enter the second string\n"); scanf("%s",s2); pos=matchany(s1,s2);

if(pos!=-1) { printf("First position in string %s , where any character of string %s \n",s1,s2); printf("Occurs at %d position \n", pos+1); } else { printf("String %s contains no character from %s \n",s1,s2); } }

/*OUTPUT:
Run 1: Enter the first string : college Enter the second string student First position in string college , where any character of string student Occurs at 5 position Run 2: Enter the first string : Peacock Enter the second string Vtu String Peacock contains no character from Vtu */

You might also like