Sie sind auf Seite 1von 26

PART A Problem 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.
Algorithm:

Step1: [Read co-efficients] Read a,b,c Step2: [Find Discriminant Value] Disc=b2 4ac If d>0 go to step 3 If d=0 go to step 4 Else go to step 5 Step3: [ Compute real and distinct roots] X1=(-b+sqrt(disc))/2a X2=(-b-sqrt(disc))/2a Print roots are real and distinct Print Root1= ,Root2= , X1,X2 Go to step 6 Step4: [Compute equal roots ] X1=X2=-b/2a Print roots are real and equal Print Root1= ,Root2= , X1,X2 Go to step 6 Step4: [Compute complex roots ] X1= -b/2a X2=(sqrt(abs(disc))/2a Print Root1= X1+IX2 ,X1,X2 Print Root1= X1-IX2 ,X1,X2 Go to step 6 Step 6: [End of algorithm] Stop

#include<stdio.h> #include<math.h> #include<stdlib.h> Void main() { float a,b,c,x1,x2,disc; clrscr(); printf("\n enter coefficients \n"); scanf("%f%f%f",&a,&b,&c); disc=b*b-4*a*c; if(disc>0) { x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf("\n roots are real and distinct \n"); printf("x1=%f\n x2=%f\n",x1,x2); } else if(disc==0) { x1=x2=-b/(2*a); printf("\n roots are real and equal \n"); printf("\n x1=%f x2=%f\n",x1,x2); } else { x1=-b/(2*a); x2=sqrt(abs(disc))/(2*a); printf("\n roots are imaginary \n"); printf("\n first root=%f+i%f\n",x1,x2); printf("\n second root=%f-i%f",x1,x2); } getch(); }

OUTPUT: Enter the roots 144 The roots are equal X1=X2=-2.00000 Enter the roots 156 The roots are distinct X1=-2.000000 X2=-3.000000 Enter the roots 624 The roots are complex First roots=-0.166667+i0.799305

second root=-0.166667-i0.799305

Problem 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.

Algorithm: Step1: [Read two numbers] Read m,n Step2: [Store the numbers in another] P=m Q=n Step3: [Looping] Repeat while n>0 R=m%n m=n n=r Step4: [Gcd and Lcm] Gcd=m Lcm=(p*q)/Gcd Print gcd of m and n= ,gcd Print lcm of m and n = lcm Step 5: [ End of algorithm] Stop

#include<stdio.h> #include<conio.h> void main() { int m, n, p, q, r, gcd, lcm; clrscr(); printf("Enter two numbers\n"); 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, %d)=%d\n", p, q, gcd); printf("LCM (%d, %d)=%d\n", p, q, lcm); getch(); } OUTPUT: 1. Enter two numbers 24 GCD (2, 4)=2 LCM (2, 4)=4 2. Enter two numbers 6 16 GCD (6, 16)=2 LCM (6, 16)=48 3. Enter two numbers 9 27 GCD (9, 27)=9 LCM (9, 27)=27

Problem 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.
Algorithm: Step1: [Read the number to reverse] Read n Step2: [Initialize] Tvar=n Step3: [Looping] Repeat while n>0 rem=n%10 rev=rev*10+rem n=n/10 Step4: [Print reversed number] Print reverse of given number Step5: [Compare orginal number and reversed number] If temp=rev then Print num is palindrome ,temp Else Print num is not palindrome ,temp Step 6: [End of algorithm] Stop

#include<stdio.h> #include<conio.h> void main() { int n, m, rev, digit; clrscr(); printf("Enter the number\n"); scanf("%d", &n); m=n; rev=0; while(n!=0) { digit=n % 10; n=n / 10; rev=rev * 10 + digit; } if(m==rev) printf("Number is palindrome\n"); else printf("Number is not a palindrome\n"); getch(); } OUTPUT: 1. Enter the number 1351 Number is not a palindrome 2. Enter the number 1551 Number is palindrome 3. Enter the number 1221 Number is palindrome

Problem 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 Horners method. Algorithm: Step1:[Read the value of n ] Read n Step2:[Enter Co-efficients] Repeat for i=0 to n in step 1 Read a[i] Step3:[Read the value of x] Read x Step4:[Initialize] sum =a[n]*x; Step5: [Evaluate polynomial] Repeat for i=n-1 down to 1 sum =(sum+a[i])*x; Step 6: [Finally add the first term] sum =sum+a[0] step 7:[Result] print the sum of polysum is ,sum Step 8: [End of algorithm] Stop

#include<stdio.h> void main() { int n, i; float a[30], x, sum; printf("Enter the value of n\n"); scanf("%d", &n); printf("Enter n+1 values\n"); for(i=0; i<=n; i++) scanf("%f", &a[i]); printf("Enter the value for x\n"); scanf("%f", &x); sum=a[n]*x; for(i=n-1; i>0; i--) sum=(sum+a[i])*x; sum=sum+a[0]; printf("Evaluated result=%f\n", sum); } OUTPUT: 1. Enter the value of n 5 Enter n+1 values 11111 Enter the value for x 2 Evaluated result=63.000000 2. Enter the value of n 5 Enter n+1 values 54321 Enter the value for x 2 Evaluated result=120.000000

Problem 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<stdlib.h> int main() { char a; printf("Enter the string to copy into output\n"); a=getchar(); printf("Copied string is : "); while(a!='\n') { if(a==' '||a=='\t') { while((a==' '||a=='\t')&&a!='\n') a=getchar(); putchar(' '); if(a=='\n') exit(0); } putchar(a); a=getchar(); } return 0; } //loop terminate when newline encountered //to check is it space or tab //loop executes when continuous space

//read only the first character

//printing single space

//if we reached the end terminate

//read the next character //end of while loop //end of main

OUTPUT: 1. Enter the string to copy into output BANGALORE IS BIG Copied string is : BANGALORE IS BIG CITY 2. Enter the Enter the string to copy into output hi how are you Copied string is : hi how are you

CITY

Problem 6 Design, develop and execute a program in C to input N integer numbers in ascending order into a single dimension array, and then to perform a binary search for a given key integer number and report success or failure in the form of a suitable message.

Algorithm: Step1:[Read the limit value ] Read n Step2:[Read n elements] Repeat for i=0 to n in step 1 Read a[i] Step3:[Print entered elements] print entered elements of an array a[i] Step4:[Enter the elements to search] print 'enter the elements to search',key Step5: [Binary search technique] [Initialize] low=0 high=n-1 Step 6: [Looping] Repeat while low<=high Begin mid=(low+high)/2 if Key=a[mid] then print 'success', exit(0) end if if key<a[mid] then high=mid-1 otherwise if Key >a[mid] high=mid+1 end while step 7:[Result] print' key element not found in an array' Step 8: [End of algorithm] Stop

#include<stdio.h> void main() { int i, n, a[10], low, high, mid, item; printf("Enter the value of n\n"); scanf("%d", &n); printf("Enter n items in ascending order\n"); for(i=0; i<n; i++) scanf("%d", &a[i]); printf("Enter the item to search\n"); scanf("%d", &item); low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(item==a[mid]) //Compare with middle element { printf("Item found at position %d\n", mid+1); exit(0); } if(item<a[mid]) high=mid-1; else low=mid+1; } printf("Item not found\n"); } OUTPUT: 1. Enter the value of n 5 Enter n items in ascending order 7 9 12 15 20 Enter the item to search 20 Item found at position 5 //to read n values

2. Enter the value of n 5 Enter n items in ascending order 4 7 9 15 20 Enter the item to search 12 Item not found

Problem 7 Design, develop and execute a program in C to input N integer numbers into a single dimension array, sort them in to ascending order using bubble sort technique, and then to print both the given array and the sorted array with suitable headings. Algorithm: Step1:[Read the limit value ] Read n Step2:[Read n elements] Repeat for i=0 to n in step 1 Read a[i] Step3:[Print entered elements] print entered elements of an array a[i] Step4:[Bubble sort technique] Repeat for j=1 to n in step 1 Repeat for i=0 to n-j in step 1 if a[i]>=a[i+1] Begin temp=a[i] a[i]=a[i+1] a[i+1]=temp end Step5: [Print sorted array] Print 'after sorting the elements of array' a[i] Go to step 6 Step 6: [End of algorithm] Stop

#include<stdio.h> int n,i,j,a[10],temp; void input() { for(i=0;i<n;i++) { scanf("%d",&a[i]); } } void output() { for(i=0;i<n;i++) { printf("%d\n",a[i]); } } void bubble() { 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; } } } } int main() { printf("\n enter no of elements"); scanf("%d",&n); printf("\n enter array elements \n"); input(); printf("\n sort the elements \n"); output(); bubble(); printf("\n sorted elements are \n"); output(); return 0; } OUTPUT: Enter no of elements 3 Enter array elements 36 65 12 Sort the elements 36 65 12 Sorted elements are 12 36 65

Problem 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() { int count=0,i=0; char word[100]; printf("Enter the word to find its length\n"); scanf(%s,word); while(word[i++]!='\0') { count++; } printf("The word length is %d\n", count); return 0; }

OUTPUT: 1. Enter the word to find its length COMPUTER The word length is 8 2. Enter the word to find its length ENGINEERING The word length is 11

PART B Problem 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 defines as FLT_EPSILON in the header file <float.h>. Print the value returned by the Mathematical function exp ( ) also.

#include<stdio.h> #include<float.h> #include<math.h> int factorial(int); int main() { float x, result=1, term=1; int i; printf("Enter the value of x\n"); scanf("%f", &x); for(i=1; term>=FLT_EPSILON; i++) { term=pow(x,i)/factorial(i); result=result+term; }

//calculating each term // adding each term with previous result

printf("Approximate value of exp(%g) is %f\n", x,result); printf("exp(%g) from math function is %f\n",x, exp(x)); } /*Function to find factorial of a number*/ int factorial(int num) { int i, fact=1; for(i=1; i<=num; i++) fact=fact*i; return fact; }

OUTPUT: 1. Enter the value of x 0.1 Approximate value of exp(0.1) is 1.105171 exp(0.1) from math function is 1.105171

2. Enter the value of x 0.5 Approximate value of exp(0.5) is 1.648721 exp(0.5) from math function is 1.648721

3. Enter the value of x 0.8 Approximate value of exp(0.8) is 2.225530 exp(0.8) from math function is 2.225541

Problem 10 Design, develop and execute a program in C to read two matrices A (M x N) and B (P x Q) and to compute the product of A and B if the matrices are compatible for multiplication. The program is to 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<process.h> int main() { int m, n, p, q, i, j, k, sum, a[10][10], b[10][10], c[10][10]; 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]); } if(n!=p) { printf("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 matrices\n"); for(i=0; i<m; i++) { for(j=0; j<q; j++) { printf("%d\t", c[i][j]); } printf("\n"); } } OUTPUT: 1. Enter the size of matrix A 2 1 Enter the elements of matrix A 2 4 Enter the size of matrix B 1 2 Enter the elements of matrix B 35 Product of two matrices 6 10 12 20 2. Enter the size of matrix A 21 Enter the elements of matrix A 1 3 Enter the size of matrix B 22 Enter the elements of matrix B 1 2 3 4 Multiplication not possible

Problem 11 Design, develop and execute a parallel program in C to add, element-wise, two onedimensional arrays A and B of N integer elements and to store the result in another onedimensional array C of N integer elements.

Problem 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.

Problem 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 is to 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; } return 1; } int main() { int x; printf("Enter the number to check whether 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); } OUTPUT: 1. Enter the number to check whether it is prime or not 10 10 is not a prime number 2. Enter the number to check whether it is prime or not 5 5 is a prime number 3. Enter the number to check whether it is prime or not 13 13 is a prime number

Problem 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.

Problem 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 s[]); int main() { char s[100]; printf("Enter the string to reverse\n"); scanf(%s,s); Reverse(s); return 0; } void Reverse(char s[]) { int i, j, len; char temp; for(i=0;s[i]!='\0';i++) ; len=i; printf("The given string is %s\n", s); for(i=0, j=len-1; i<j; i++, j--) { temp=s[i]; s[i]=s[j]; s[j]=temp; } s[len]='\0'; printf("The reversed string is %s\n",s); } //to swap starting of the string with end of it //to find string length

//assigning string length to len

//attaching null at the end

OUTPUT: 1. Enter the string to reverse COMPUTER SCIENCE The given string is COMPUTER SCIENCE Reversed string is ECNEICS RETUPMOC

2. Enter the string to reverse Bangalore The given string is Bangalore Reversed string is erolagnaB

Problem 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 the return value from the function matchany (s1,s2).

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> #include<string.h> int matchany(char s1[],char s2[]); void main() { char s1[20],s2[20]; int pos; clrscr(); printf("enter first string\n"); scanf("%s",&s1); printf("enter second string\n"); scanf("%s",&s2); pos=matchany(s1,s2); if(pos==-1) printf("no characterof %s is present in %s\n",s2,s1); else printf("Position=%d",pos); getch(); } int matchany(char s1[],char s2[]) { int i,j; char symbol; for(i=0;s1!='\0';i++) { symbol=s1[i]; for(j=0;s2[j]!='\0';j++) { if(symbol==s2[j]) return i+1; } } return -1; }

Output: Enter first string College Enter second string Age Position=5

Enter

Das könnte Ihnen auch gefallen