Upload
vtu
View
0
Download
0
Embed Size (px)
Citation preview
HKBK College of EngineeringDepartment of Computer Science and Engineering
Noor-E-Saba, Dept. of CSE@HKBKCE Page 1
10CPL16/26 COMPUTER PROGRAMMING LABORATORYPART – A
1. Design, develop and execute a program in C to find and output all the roots of a givenquadratic equation, for non-zero coefficients.
2. Design, develop and execute a program in C to implement Euclid’s algorithm to find theGCD and LCM of two integers and to output the results along with the given integers.
3. Design, develop and execute a program in C to reverse a given four digit integer numberand check whether it is a palindrome or not. Output the given number with suitablemessage.
4. Design, develop and execute a program in C to evaluate the given polynomial
f(x) = a4x4
+ a3x3
+ a2x2
+ a1x1
+ a0 for given value of x and the coefficients using Horner’s
method.
5. Design, develop and execute a program in C to copy its input to its output, replacing eachstring of one or more blanks by a single blank.
6. Design, develop and execute a program in C to input N integer numbers in ascending orderinto a single dimension array, and then to perform a binary search for a given key integernumber and report success or failure in the form of a suitable message.
7. Design, develop and execute a program in C to input N integer numbers into a singledimension array, sort them in to ascending order using bubble sort technique, and then toprint both the given array and the sorted array with suitable headings.
8. Design, develop and execute a program in C to compute and print the word length on thehost machine.
PART – B
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 theexpansion until the last term is less than the machine epsilon defines as FLT_EPSILON in theheader file <float.h>. Print the value returned by the Mathematical function exp ( ) also.
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 headingsand format if the matrices are compatible for multiplication, otherwise the program mustprint a suitable message.(For the purpose of demonstration, the array sizes M, N, P, and Qcan all be less than or equal to 3)
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 2
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 to store the result in another one-dimensional array C of N integer element.
12. Design and develop a function rightrot(x, n) in C that returns the value of the integer xrotated to the right by n bit positions as an unsigned integer. Invoke the function from themain with different values for x and n and print the results with suitable headings.
13. Design and develop a function isprime(x) that accepts an integer argument and returns 1 ifthe argument is prime and 0 otherwise. The function is to use plain division checkingapproach to determine if a given number is prime. Invoke this function from the main withdifferent values obtained from the user and print appropriate messages.
14. Design, develop and execute a parallel program in C to determine and print the primenumbers which are less than 100 making use of algorithm of the Sieve of Eratosthenes.
15. Design and develop a function reverses(s) in C to reverse the string s in place. Invoke thisfunction from the main for different strings and print the original and reversed strings.
16. Design and develop a function matchany (s1,s2) which returns the first location in the strings1 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 functionmatchany (s1. s2) from the main for different strings and print both the strings and thereturn value from the function matchany (s1, s2).
1. Note: In the practical examination the student has to answer two questions. Onequestion from Part A and one question from Part B, will be selected by the student bylots. All the questions listed in the syllabus have to be included in the lots. The change ofquestion (Part A only / Part B only / Both Part A & Part B) has to be considered, providedthe request is made for the same, within half an hour from the start of the examination.The allotment of marks is as detailed below:
Sl. # Activity Max. Marks
1 ProcedureWriting program & procedure for the assigned problems along withalgorithms / flowchart
Part A 5*
Part B 5*
2 ConductionExecution of the program and showing the results in proper format
Part A 10
Part B 20
3 Viva-voce** 10
Total Max. Marks 50
Minimum Passing Marks (40% of Max. Marks) 20
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 4
1) Design, develop and execute a program in C to find and output all the roots ofa given quadratic equation,for non-zero co-efficient.
Algorithm1. Start.2. Input co-efficient of equation a, b, c .3. IF any or all the coefficients are zero
Print Invalid inputELSE
d ← b2- 4ac
r ←√ |d|
IF d > 0r1 ← (-b +r)/ (2a)r2 ← (-b -r)/ (2a)Print “Roots are REAL and DISTINCT”Print r1, r2ELSE IF d < 0
r1 ←-b/ (2a)r2 ←r/ (2a)Print “Roots are COMPLEX”Print r1 “+i” r2, r1 “- i” r2
ELSEr1←-b/(2a)Print “Roots are EQUAL”Print r1, r1
END IFEND IF
END IF.4. Stop
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 6
Program#include <stdio.h>#include <math.h>void main(){
int a,b,c;float d,x1,x2,r;printf("Enter the three co-efficient :\n");scanf("%d%d%d",&a,&b,&c);
if (a* b* c == 0){printf("\n Invalid Input ");}
else{
d = b * b - 4 * a * c;r=sqrt(fabs(d));
if (d > 0){x1 = (-b +r) / (2.0*a);x2 = (-b -r) / (2.0*a);printf("\n The roots are real and distinct\n");printf("\n The roots are \n 1) x1=%f\t\t \n 2) x2=%f",x1,x2);}
else if (d == 0){x1 = x2 = -b/(2.0*a);printf("\n The roots are real and equal\n");printf("\n The roots are: \n 1) x1=x2=%f",x1);}
else{x1 = -b / (2.0 * a);x2 = r / (2.0*a);printf("\n The roots are real and imaginary\n");printf("\n The roots are:\n 1) %f +i %f \t\t\n 2) %f –i %f ",x1,x2,x1,x2);}
}}
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 7
Sample Output1. Enter the three co-efficient :1 4 4The roots are real and equalThe roots are:X1=X2=2.0000
2. Enter the three co-efficient :1 - 5 6The roots are real and distinctThe roots are:X1=3.0000X2=2.0000
3. Enter the three co-efficient :2 3 4The roots are real and imaginaryThe roots are:1) -0.750000 +i 1.1989582) -0.750000 - i 1.198958
4. Enter the three co-efficient :1 0 5Invalid Input
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 8
2) Design, develop and execute a program in C to implement Euclid’s algorithm tofind the GCD and LCM of two integers and to output the results along with thegiven integers.
Algorithm
1. Start.2. Input m , n.3. Initialize p ← m , q ← n.4. Until n <> 0
rem ← m mod nm ← nn ← rem
END until5. GCD ← m6. LCM ← p * q / GCD.7. Print p, q, GCD, LCM.8. Stop
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 10
Program#include<stdio.h>void main(){
int m,n,p,q,gcd,lcm,rem;printf("Enter two numbers : ");scanf("%d%d",&m,&n);p = m;q = n;while(n!=0){
rem=m%n;m=n;n=rem;
}gcd = m;lcm = (p * q) / gcd;
printf("\n The LCM of %d and %d = %d",p,q,lcm);printf("\n The GCD of %d and %d = %d",p,q,gcd);}
Sample OutputEnter two numbers:3466The LCM = 1122The GCD = 2
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 11
3) Design, develop and execute a program in C to reverse a given four digitinteger number and check whether it is a palindrome or not. Output the givennumber with suitable message.
Algorithm
1. Start.2. Input n.3. Initialize a ← n, rev ← 0, rem ← 0.4. IF n <= 999 OR n>9999
Print “Not a 4 digit number”Goto step 7.
5. Until n <>0rem ← n % 10rev ← rev *10 + rem.n ← n / 10.
END until6. IF a EQUAL TO rev
Print “Palindrome”.ELSE
Print “Not a Palindrome”.7. Stop.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 13
Program#include <stdio.h>void main(){
int n,rev=0,rem,a;printf("Enter a number : ");scanf("%d",&n);a = n;if(n<=999 || n>9999){printf(“ Not a 4 digit number\n”);exit(0);}while(n != 0){
rem=n%10;rev= rev*10+rem;n = n / 10;
}if(a==rev)
printf("\n The given Number %d is Palindrome",a);else
printf("\n The given Number %d is not Palindrome",a);}
Sample Output
1. Enter a number:201Not a four digit number
2. Enter a number:5642The Number is not Palindrome
3. Enter a number:8118The Number is Palindrome
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 14
4) Design, develop and execute a program in C to evaluate the given polynomial
f(x)= a4x4
+ a3x3
+ a2x2
+ a1x1
+ a0 for given value of x and the coefficients usingHorner’s method.
Algorithm
1. Start.2. Read n.3. FOR i ← 0 to n in steps of 1
Read a[i]END FOR
4. Read x.5. poly ← a[0]6. FOR i ← 1 to n in steps of 1
poly ← poly * x + a[i]END FOR.
7. Print poly.8. Stop.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 16
Program
#include <stdio.h>void main(){
int n,i,x,a[10],poly=0;printf("\n Enter the degree of the polynomial : ");scanf("%d",&n);printf("\n Enter the %d coefficients\n",n+1);for(i = 0 ; i <= n ; i++){
scanf("%d",&a[i]);}printf("\n Enter the value of x :");scanf("%d",&x);poly=a[0];for(i = 1 ; i <= n ; i++){
poly = poly* x+a[i];}printf("\n The sum of polynomial = %d",poly);
}
Sample OutputEnter the degree of the polynomial : 4Enter the 5 coefficient1 2 3 4 5Enter the value of x :1The sum of polynomial = 15
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 17
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.
Algorithm
1. Start2. Read the text in Array c3. FOR i← 0 to c [i] <> '\0' in steps of 1
IF c [i] = ' 'Print ' '
End IfUntil c [i] = ‘\0'
Increment iEnd UntilPrint the character
End For
4. Stop
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 19
Program#include <stdio.h>#include<string.h>void main(){
char c[50];int I;printf("Enter the text");scanf(“%[^\t\n]”, c);for(i=0;c[i]!='\0';i++){if (c[i]==' ')
printf(“%c”, c[i]);while (c[i]==' ')
i++;printf(“%c”, c[i]);}
}
Sample outputEnter the textwelcome to hkbk College of Engineeringwelcome to hkbk College of Engineering
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 20
6) Design, develop and execute a program in C to input N integer numbers inascending order into a single dimension array, and then to perform a binarysearch for a given key integer number and report success or failure in the form ofa suitable message.
Algorithm1. Start.2. Initialize Flag ← 03. Read n.4. FOR i← 0 to n-1 in steps of 1
Read numbers in ascending order in Array a[ ]END FOR.
5. Read key.6. Initialize low←0, high←n -1.7. Until low <= high
mid←(low + high)/ 2.8. IF key = a[mid]
Flag=1Goto Step 10
END IF
9. IF key > a[mid]low←mid + 1
ELSEhigh←mid – 1
END IFEND Until
10. IF Flag = 1Print “Successful Search”.
ELSEPrint “Unsuccessful Search”.
END IF11. Stop.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 22
Program#include <stdio.h>void main(){
int n,i,a[10],key,low,high,mid,Flag=0;printf("\n Enter the no. of elements : ");scanf("%d",&n);printf("\n Enter %d elements in ascending order ",n);for(i = 0 ; i < n ; i++)scanf("%d",&a[i]);printf("\n Enter the key element to search : ");scanf("%d",&key);low = 0;high = n-1;while(low <= high){mid=(low+high)/2;if(key==a[mid])
{Flag=1;break;}
if(key > a[mid])low=mid+1;
elsehigh = mid-1;
}if(Flag==1)
printf("\n Successful Search ");else
printf("\n Unsuccessful Search");}
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 23
Sample Output
Enter the no. of elements: 5Enter 5 elements in ascending order1020304050Enter the key element to search: 30Search Successful
Enter the no. of elements: 5Enter 5 elements in ascending order23456
Enter the key element to search : 7unsuccessful Search
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 24
7) Design, develop and execute a program in C to input N integer numbers into asingle dimension array, sort them in to ascending order using bubble sorttechnique, and then to print both the given array and the sorted array withsuitable headings.
Algorithm1. Start.2. Read n.3. FOR i←0 to n-1 in steps of 1
Read integer numbers in array a [ ]b[i]←a[i]
END FOR.4. FOR i←0 to n-1 in steps of 1
FOR j←0 to n-i in steps of 1IF a[j] > a[j + 1]
temp= a[j]a[j] = a[j + 1]a[j+1] = temp
END IFEND FOR
END FOR5. Print original array b[i]
FOR i←0 to n-1 in steps of 1Print b[i]
END FOR.6. Print sorted array a[i]
FOR i←0 to n-1 in steps of 1Print a[i]
END FOR.7. Stop.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 26
Program#include <stdio.h>void main(){
int n,i,j,a[10],b[10],temp;printf("\n Enter the no. of elements : ");scanf("%d",&n);printf("\n Enter %d elements ",n);for(i = 0 ; i < n ; i++){
scanf("%d",&a[i]);b[i]=a[i];
}for(i = 0 ; i < n-1 ; i++){
for(j = 0 ; j < n-i; j++){
if(a[j] > a[j+1]){
temp = a[j];a[j] = a[j+1];a[j+1] = temp;
}}
}printf("\n The original elements are\n ");for(i = 0 ; i < n ; i++)
printf("%d \n",b[i]);printf("\n The Sorted elements are ");for(i = 0 ; i < n ; i++)
printf("%d \n",a[i]);}
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 27
Sample Output
Enter the no. of elements : 5Enter 5 elements91152314The original elements are91152314The Sorted elements are13145291
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 28
8) Design, develop and execute a program in C to compute and print the wordlength on the host machine.
Algorithm
1. Start2. Initialize
Var←-1Wordlen ←0
3. Until (var)Wordlen++Var<<←1
End Until4. Print The word length of host machine5. Stop
Flowchart
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 29
Program#include<stdio.h>void main(){
int var = -1,wordlen=0;while (var){wordlen++;var <<= 1;}printf(" The Word length of this Host Machine is %d Bits", wordlen);
}
Sample outputThe Word length of this Host Machine is 32 Bits
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 31
9) Design, develop and execute a program in C to calculate the approximate valueof exp (0.5) using the Taylor Series expansion for the exponential function. Usethe terms in the expansion until the last term is less than the machine epsilondefines as FLT_EPSILON in the header file <float.h>.Print the value returned bythe mathematical function exp( ) also.
Algorithm1. Start2. Read X
3. Initialize sum←0,term←1,fact←14. for i←1; term >= FLT_EPSILON; i++
fact ← fact*isum ← sum+termterm ← pow(x,i)/fact
5. Print calculated value6. Print Library function value.7. Stop
Flowchart
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 32
Program#include<stdio.h>#include <float.h>#include <math.h>void main(){
int i;float x,sum,fact,term;printf("\nYou have this series : 1+x/1!+x^2/2!+ x^3/3! + x^4/4!+……………….x^n/n!");printf("\n\nEnter the value for X : ");scanf("%f",&x);sum = 0;term = 1;fact = 1;for(i=1;term >= FLT_EPSILON;i++){
fact =fact * i;sum = sum + term;term = pow(x,i)/fact;
}printf("\n\nThe Calculated value of e^% .3f = %f",x,sum);printf("\n\nThe Library Function Value of e^%.3f = %f",x,exp(x));
}
Sample OutputYou have this series: 1+x/1!+x^2/2!+x^3/3!+x^4/4!+…….x^n/n!Enter the value for X : 0.5The Calculated value of e^ 0.500 = 1.648721The Library Function Value of e^ 0.500 = 1.648721
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 33
10) Design, develop and execute a program in C to read two matrices A(M*N) andB(P*Q) and to compute the product of A and B if the matrices are compatible formultiplication. The program is to print the input matrices and resultant matrixwith suitable headings and format if the matrices are compatible forMultiplication. Otherwise the program must print a suitable message.(For thepurpose of demonstration, the array sizes M,N,P and Q can all be less than orequal to 3).
Algorithm1. Start.2. Read order m, n3. Read order p, q4. IF n==p THEN
FOR i ← 0 to m in steps of 1FOR j ← 0 to n in steps of 1Read a[i][j]END FOR
END FOR5. FOR i ← 0 to p in steps of 1
FOR j ← 0 to q in steps of 1Read b[i][j]
END FOREND FOR
6. Matrix multiplicationFOR i ← 0 to m in steps of 1
FOR j ← 0 to q in steps of 1c[i][j] ← 0
FOR k ← 0 to n in steps of 1c[i][j] ← c[i][j] + a[i][k] * b[k][j]END FOR
END FOREND FOR
End if7. Print matrix AFOR i ← 0 to m in steps of 1
FOR j ← 0 to n in steps of 1Print a[i][j]
END FOREND FOR
8. Print matrix BFOR i ← 0 to p in steps of 1
FOR j ← 0 to q in steps of 1Print b[i][j]
END FOREND FOR
9. Print matrix C
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 34
FOR i ← 0 to m in steps of 1FOR j ← 0 to q in steps of 1
Read c[i][j]END FOR
END FORELSE
Print “Multiplication not possible”END IF
10. Stop.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 37
Program#include <stdio.h>void main(){
int a[10][10],b[10][10],c[10][10];int m,n,p,q,i,j,k;printf("\n Enter the order of the matrix A :");scanf("%d%d",&m,&n);printf("\n Enter the order of the matrix B :");scanf("%d%d",&p,&q);if(n==p){printf("\n 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("\n Enter the elements of matrix B \n");for(i = 0 ; i < p ; i++){
for(j = 0 ; j < q ; j++)scanf("%d",&b[i][j]);
}for(i = 0 ; i < m ; i++){
for(j = 0 ; j < q ; j++){
c[i][j]=0;for(k = 0 ; k < n ; k++)
c[i][j] += a[i][k] * b[k][j];}
}printf("\n MATRIX A \n");for(i = 0 ; i < m ; i++){
for(j = 0 ; j < n ; j++){
printf(" %d \t", a[i][j]);}
printf("\n");
}
printf("\n MATRIX B \n");for(i = 0 ; i < p ; i++)
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 38
{for(j = 0 ; j < q ; j++){
printf(" %d \t", b[i][j]);}
printf("\n");
}printf("\n MATRIX C \n");for(i = 0 ; i < m ; i++){
for(j = 0 ; j < q ; j++){
printf(" %d \t", c[i][j]);}
printf("\n");
}}
elseprintf("Matrix A & B is not multiplicable");}
Sample OutputEnter the order of the matrix A :2 2Enter the order of the matrix B :2 2Enter the elements of matrix A1 23 4Enter the elements of matrix B2 34 5MATRIX A1 23 4MATRIX B2 34 5MATRIX C10 1322 29
Enter the order of the matrix A :2 3Enter the order of the matrix B :2 3Matrix A & B is not multiplicable
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 39
11) Design, develop and execute a parallel program in C to add, element-wise, twoone-dimensional arrays A and B of N integer elements and to store the result inanother one-dimensional array C of N integer elements.
Algorithm1. Start2. Read n3. For i ← 0 to n in steps of 1
Read elements of Array AEND For
4. For j ← 0 to n in steps of 1Read elements of Array B
5. For i←0, to n in steps of 1c[i]=a[i]+b[i]Print C
6. Stop.
Flow Chart
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 40
Program#include<stdio.h>#include<omp.h>void main(){
int i,j,k,n,a[10],b[10],c[10];printf("enter the size of an arrays\n");scanf("%d",&n);printf("enter array elements of A\n");for(i=0;i<n;i++)
scanf("%d",&a[i]);printf("enter array elements of B\n");for(j=0;j<n;j++)
scanf("%d",&b[j]);#pragma omp parallel forfor(i=0;i<n;i++){
c[i]= a[i]+b[i];printf ("c[%d]=%d,threadno=%d\n",i,c[i],omp_get_thread_num);
}}
Sample outputenter the size of an arrays5Enter array elements of A246810Enter array elements of B12345The array elements of C areC [0] =3C [1] =6C [2] =9C [3] =12C [4] =15
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 41
12) Design and develop a function rightrot(x,n) in C that returns the value of theinteger x rotated to the right by n bit positions as an unsigned integer. Invoke thefunction from the main with different values for x and n and print the results withsuitable headings.
Algorithm
1. Start2. Read x and n3. Call function RR(x, n)6. Print the result7. Stop
Algorithm to rotate value by n bits1. IF n==0
Return xELSE
Return ((x >> n) | (x << (32- n)))2. Return
Flowchart
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 42
Program#include<stdio.h>void main ( ){
unsigned int ans,x,n;printf("enter x and n values\n");scanf("%d%d",&x,&n);ans=rightrot(x,n);printf("\nThe value after rotating %d bit is : ",n);printf("%d",ans);
}int rightrot(unsigned int x,unsigned int n){if (n == 0)
return x;else
return ((x >> n) | (x << ( 32- n)));}
Sample OutputEnter x and n values8 3The value after rotating 3 bit is: 1
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 43
13) Design and develop a function isprime(x) that accepts an integer argumentand returns 1 if the argument is prime and 0 otherwise. The function is to useplain division checking approach to determine if a given number is prime. Invokethis function from the main with different values obtained from the user and printappropriate messages.
Algorithm
1. Start2. Read the number3. Call function prime(n)
P= prime(n)4. IF p ← 1
Print “ Number is prime”ELSE
Print “Number not a prime”End IF5. Stop.
Algorithm to check the prime number
1. FOR i ←2 to n/2 in steps of 1IF (n mod i) is equal to zero
Return 0Return 1
End For2. Return.
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 45
Program#include<stdio.h>int prime(int n){
int n,i;for(i=2;i<=n/2;i++)
if(n%i ==0)return 0;
return 1;}
void main(){int n,p;printf("enter the no\n");scanf("%d",&n);p=prime(n);if(p==1)
printf("The given no is prime\n");else
printf("The given no is not prime\n");}
Sample outputEnter the number17The given number is primeEnter the number33The given number is not prime
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 46
14) Design, develop and execute a parallel program in C to determine and printthe prime numbers which are less than 100 making use of algorithm of the sieveof Eratosthenes.
Algorithm1. Start2. Initialize n ← 1003. For i←0 to n in steps of 1
num[i] ← iEND For
4. For i←2 to in steps of 1IF num[i] != 0For j← i*i to n in steps of j← j+i
num[j] ← 0END ForEND IF
END For5. For i←0 to n in steps of 1
iF num[i] != 0Print non zero numbersEND IF
END For6. Stop
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 48
Program#include<stdio.h>#include<math.h>#include<omp.h>int main(){
int num[120], i, j,n;printf("enter the value of n\n”);scanf(“%d”,&n);#pragma omp parallel forfor(i=0;i<=n;i++){
num[i]=i;}#pragma omp parallel forfor(i=2;i<=sqrt(n);i++){
if(num[i]!=0){#pragma omp parallel forfor(j=(i*i);j<=n;j=j+i){
num[j]=0;}}
}#pragma omp parallel forprintf(“The prime numbers that are less 100\n”);for(i=0;i<=n;i++){
if(num[i]!=0)printf("\nprimeno=%d threadID=%d\n",num[i],omp_get_thread_num());
}}
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 49
Sample outputThe prime numbers that are less than 100Prime no=2 threadID=0Prime no=3 threadID=0Prime no=5 threadID=0Prime no=7 threadID=0Prime no=11 threadID=0Prime no=13 threadID=0Prime no=17 threadID=0Prime no=19 threadID=0Prime no=23 threadID=0Prime no=29 threadID=0Prime no=31 threadID=0Prime no=37 threadID=0Prime no=41 threadID=0Prime no=43 threadID=0Prime no=47 threadID=0Prime no=53 threadID=0Prime no=59 threadID=0Prime no=61 threadID=0Prime no=67 threadID=0Prime no=71 threadID=0Prime no=73 threadID=0Prime no=79 threadID=0Prime no=83 threadID=0Prime no=89 threadID=0Prime no=97 threadID=0
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 50
15) Design and develop a function reverses(s) in C to reverse the string s inplace. Invoke this function from the main for different strings and print theoriginal and reversed strings.
Algorithm1. Start2. Read the string3. Print the string4. Call function reverse(s)5. Stop
Algorithm to reverse a string1. Start2. Calculate the length
3. For i← 0, i<string length/2 in step of 1temp← str[i]str[i]←str[len]str[len]←tempdecrement length
END For4. Print the reverse string5. Stop
Flowchart
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 51
Program#include <stdio.h>#include <string.h>void reverse(char s[]){
char s[10],temp;int i,len;len = strlen(s)-1;for(i=0;i<strlen(s)/2;i++){
temp=s[i];s[i]=s[len];s[len]=temp;len--;
}printf("the reversed string is = %s",s);
}
void main(){char s[10];int i;printf("Enter String : ");scanf("%s",s);printf("the original string is=%s\n",s);reverse(s);}
Sample OutputEnter String : computerthe original string is=computerthe reversed string is=retupmoc
Enter String : magicthe original string is=magicthe reversed string is=cigam
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 52
16) Design and develop a function matchany(s1,s2) which returns the firstlocation in the string s1 where any character from the string s2 occurs, or -1 if s1contains no character from s2. Do not use the standard library function whichdoes a similar job! Invoke the function matchany(s1,s2) from the main fordifferent strings and print both the strings and the return value from the functionmatchany(s1,s2).
Algorithm1. Start2. Read string in str13. Read string in str24. Call function x = matchany(str1,str2)5. IF x is equal -1
Print no character matchingELSE
Print position and matched characterEnd if6. Stop.
Algorithm of matchany function1.For i ← 0 to s1[i] != null in steps of 1
For j ← 0 to s2[j] != null in steps of 1IF s1[i] = s2[j]
Return iEnd IF
End ForEnd For2. Return -13. Return
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 54
Program#include <stdio.h>#include <string.h>int matchany(char s1[ ], char s2[ ]);void main(){
char str1[20],str2[20];int x;printf("\nEnter first String:");scanf("%s",str1);printf("\nEnter second String:");scanf("%s",str2);x=matchany(str1,str2);if(x==-1)
printf("No charecter matching\n");else
printf("The position is %d and charecter is %c\n",x+1,str1[x]);}
int matchany(char s1[10], char s2[10]){
int i,j;for(i=0;s1[i]!='\0';i++){
for(j=0;s2[j]!='\0';j++){
if(s1[i] == s2[j])return i;
}}return -1;
}
Sample OutputEnter first String: indiaEnter second String: mandiaThe position is 1 and character is i
Enter first String: hkbkEnter second String: classNo character matching