Cpl Manual
-
Upload
sarini-vedula -
Category
Documents
-
view
284 -
download
0
Transcript of Cpl Manual
-
7/28/2019 Cpl Manual
1/55
HKBK College ofEngineeringDepartment 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 given
quadratic equation, for non-zero coefficients.
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.
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.
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 Horners
method.
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.
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.
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.
8. Design, develop and execute a program in C to compute and print the word length on the
host 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 the
expansion until the last term is less than the machine epsilon defines as FLT_EPSILON in the
header file . 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 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)
-
7/28/2019 Cpl Manual
2/55
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 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.
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.
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.
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.
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).
1. Note: In the practical examination the student has to answer two questions. One
question from Part A and one question from Part B, will be selected by the student by
lots. 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, provided
the 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 Procedure
Writing program & procedure for the assigned problems along with
algorithms / flowchart
Part A 5*
Part B 5*
2 Conduction
Execution of the program and showing the results in proper format
Part A 10
Part B 203 Viva-voce** 10
Total Max. Marks 50
Minimum Passing Marks (40% of Max. Marks) 20
-
7/28/2019 Cpl Manual
3/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 3
PART A
-
7/28/2019 Cpl Manual
4/55
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.
Algorithm
1. Start.
2. Input co-efficient of equation a, b, c .
3. IF any or all the coefficients are zeroPrint Invalid input
ELSE
d b2
- 4ac
r |d|
IF d > 0
r1 (-b +r)/ (2a)
r2 (-b -r)/ (2a)
Print Roots are REAL and DISTINC
Print r1, r2
ELSE IF d < 0
r1 -b/ (2a)
r2 r/ (2a)
Print Roots are COMPLEX
Print r1 +i r2, r1 - i r2
ELSE
r1-b/(2a)
Print Roots are EQUAL
Print r1, r1
END IF
END IFEND IF.
4. Stop
-
7/28/2019 Cpl Manual
5/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 5
Flowchart
-
7/28/2019 Cpl Manual
6/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 6
Program
#include
#include
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);
}
}
}
-
7/28/2019 Cpl Manual
7/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 7
Sample Output
1. Enter the three co-efficient :
1 4 4
The roots are real and equal
The roots are:
X1=X2=2.0000
2. Enter the three co-efficient :
1 - 5 6
The roots are real and distinct
The roots are:
X1=3.0000
X2=2.0000
3. Enter the three co-efficient :
2 3 4
The roots are real and imaginary
The roots are:
1) -0.750000 +i 1.198958
2) -0.750000 - i 1.198958
4. Enter the three co-efficient :
1 0 5
Invalid Input
-
7/28/2019 Cpl Manual
8/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 8
2) Design, develop and execute a program in C to implement Euclids 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 n
m n
n rem
END until
5. GCD m
6. LCM p * q / GCD.
7. Print p, q, GCD, LCM.
8. Stop
-
7/28/2019 Cpl Manual
9/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 9
Flowchart
-
7/28/2019 Cpl Manual
10/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 10
Program
#include
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 Output
Enter two numbers:
34
66
The LCM = 1122
The GCD = 2
-
7/28/2019 Cpl Manual
11/55
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 9999
Print Not a 4 digit number
Goto step 7.
5. Until n 0
rem n % 10
rev rev *10 + rem.
n n / 10.
END until
6. IF a EQUAL TO revPrint Palindrome.
ELSE
Print Not a Palindrome.
7. Stop.
-
7/28/2019 Cpl Manual
12/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 12
Flowchart
-
7/28/2019 Cpl Manual
13/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 13
Program
#include
void main()
{
int n,rev=0,rem,a;
printf("Enter a number : ");
scanf("%d",&n);a = n;
if(n9999)
{
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:
201
Not a four digit number
2. Enter a number:
5642
The Number is not Palindrome
3. Enter a number:
8118
The Number is Palindrome
-
7/28/2019 Cpl Manual
14/55
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 using
Horners 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.
-
7/28/2019 Cpl Manual
15/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 15
Flowchart
-
7/28/2019 Cpl Manual
16/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 16
Program
#include
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
-
7/28/2019 Cpl Manual
17/55
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. Start
2. Read the text in Array c3. FOR i 0 to c [i] '\0' in steps of 1
IF c [i] = ' '
Print ' '
End If
Until c [i] = \0'
Increment i
End Until
Print the character
End For
4. Stop
-
7/28/2019 Cpl Manual
18/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 18
Flowchart
-
7/28/2019 Cpl Manual
19/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 19
Program
#include
#include
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 output
Enter the text
welcome to hkbk College of Engineering
welcome to hkbk College of Engineering
-
7/28/2019 Cpl Manual
20/55
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.
Algorithm
1. Start.
2. Initialize Flag 0
3. 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 low0, highn -1.
7. Until low a[mid]
lowmid + 1
ELSE
highmid 1
END IF
END Until10. IF Flag = 1
Print Successful Search.
ELSE
Print Unsuccessful Search.
END IF
11. Stop.
-
7/28/2019 Cpl Manual
21/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 21
Flowchart
-
7/28/2019 Cpl Manual
22/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 22
Program
#include
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 a[mid])
low=mid+1;
else
high = mid-1;
}
if(Flag==1)printf("\n Successful Search ");
else
printf("\n Unsuccessful Search");
}
-
7/28/2019 Cpl Manual
23/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 23
Sample Output
Enter the no. of elements: 5
Enter 5 elements in ascending order
10
20
3040
50
Enter the key element to search: 30
Search Successful
Enter the no. of elements: 5
Enter 5 elements in ascending order
2
3
4
5
6
Enter the key element to search : 7
unsuccessful Search
-
7/28/2019 Cpl Manual
24/55
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.
Algorithm
1. Start.
2. Read n.
3. FOR i0 to n-1 in steps of 1
Read integer numbers in array a [ ]
b[i]a[i]
END FOR.
4. FOR i0 to n-1 in steps of 1
FOR j0 to n-i in steps of 1
IF a[j] > a[j + 1]
temp= a[j]
a[j] = a[j + 1]a[j+1] = temp
END IF
END FOR
END FOR
5. Print original array b[i]
FOR i0 to n-1 in steps of 1
Print b[i]
END FOR.
6. Print sorted array a[i]
FOR i0 to n-1 in steps of 1Print a[i]
END FOR.
7. Stop.
-
7/28/2019 Cpl Manual
25/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 25
Flowchart
-
7/28/2019 Cpl Manual
26/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 26
Program
#include
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]);
}
-
7/28/2019 Cpl Manual
27/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 27
Sample Output
Enter the no. of elements : 5
Enter 5 elements
91
1
523
14
he original elements are
91
1
52
3
14
The Sorted elements are
1
3
14
52
91
-
7/28/2019 Cpl Manual
28/55
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. Start
2. InitializeVar-1
Wordlen 0
3. Until (var)
Wordlen++
Var
-
7/28/2019 Cpl Manual
29/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 29
Program
#include
void main()
{
int var = -1,wordlen=0;
while (var)
{wordlen++;
var
-
7/28/2019 Cpl Manual
30/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 30
PART-B
-
7/28/2019 Cpl Manual
31/55
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 .Print the value returned bythe mathematical function exp( ) also.
Algorithm
1. Start
2. Read X
3. Initialize sum0,term1,fact1
4. for i1; term >= FLT_EPSILON; i++
fact fact*i
sum sum+term
term pow(x,i)/fact
5. Print calculated value
6. Print Library function value.
7. Stop
Flowchart
-
7/28/2019 Cpl Manual
32/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 32
Program
#include
#include
#include
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 Output
You 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.648721
The Library Function Value of e^ 0.500 = 1.648721
-
7/28/2019 Cpl Manual
33/55
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).
Algorithm
1. Start.
2. Read order m, n
3. Read order p, q
4. IF n==p THEN
FOR i 0 to m in steps of 1
FOR j 0 to n in steps of 1
Read a[i][j]
END FOR
END FOR5. FOR i 0 to p in steps of 1
FOR j 0 to q in steps of 1
Read b[i][j]
END FOR
END FOR
6. Matrix multiplication
FOR i 0 to m in steps of 1
FOR j 0 to q in steps of 1
c[i][j] 0
FOR k 0 to n in steps of 1
c[i][j] c[i][j] + a[i][k] * b[k][j]
END FOR
END FOR
END FOR
End if
7. Print matrix A
FOR i 0 to m in steps of 1
FOR j 0 to n in steps of 1
Print a[i][j]
END FOR
END FOR
8. Print matrix B
FOR i 0 to p in steps of 1
FOR j 0 to q in steps of 1
Print b[i][j]
END FOR
END FOR
9. Print matrix C
-
7/28/2019 Cpl Manual
34/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 34
FOR i 0 to m in steps of 1
FOR j 0 to q in steps of 1
Read c[i][j]
END FOR
END FOR
ELSE
Print Multiplication not possibleEND IF
10. Stop.
-
7/28/2019 Cpl Manual
35/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 35
Flowchart
-
7/28/2019 Cpl Manual
36/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 36
-
7/28/2019 Cpl Manual
37/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 37
Program
#include
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]);
}
print ("\n");
}
printf("\n MATRIX B \n");
for(i = 0 ; i < p ; i++)
-
7/28/2019 Cpl Manual
38/55
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");
}
}
else
printf("Matrix A & B is not multiplicable");
}
Sample Output
Enter the order of the matrix A :2 2
Enter the order of the matrix B :2 2
Enter the elements of matrix A1 2
3 4
Enter the elements of matrix B
2 3
4 5
MATRIX A
1 2
3 4
MATRIX B
2 3
4 5
MATRIX C
10 13
22 29
Enter the order of the matrix A :2 3
Enter the order of the matrix B :2 3
Matrix A & B is not multiplicable
-
7/28/2019 Cpl Manual
39/55
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.
Algorithm
1. Start
2. Read n3. For i 0 to n in steps of 1
Read elements of Array A
END For
4. For j 0 to n in steps of 1
Read elements of Array B
5. For i0, to n in steps of 1
c[i]=a[i]+b[i]
Print C
6. Stop.
Flow Chart
-
7/28/2019 Cpl Manual
40/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 40
Program
#include
#include
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
-
7/28/2019 Cpl Manual
41/55
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. Start
2. Read x and n
3. Call function RR(x, n)
6. Print the result
7. Stop
Algorithm to rotate value by n bits
1. IF n==0
Return x
ELSEReturn ((x >> n) | (x
-
7/28/2019 Cpl Manual
42/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 42
Program
#include
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
-
7/28/2019 Cpl Manual
43/55
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. Start
2. Read the number
3. 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 1
IF (n mod i) is equal to zero
Return 0
Return 1
End For
2. Return.
-
7/28/2019 Cpl Manual
44/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 44
Flowchart
-
7/28/2019 Cpl Manual
45/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 45
Program
#include
int prime(int n)
{
int n,i;
for(i=2;i
-
7/28/2019 Cpl Manual
46/55
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.
Algorithm
1. Start
2. Initialize n 100
3. For i0 to n in steps of 1
num[i] i
END For
4. For i2 to in steps of 1
IF num[i] != 0
For j i*i to n in steps ofj j+i
num[j] 0
END For
END IF
END For5. For i0 to n in steps of 1
iF num[i] != 0
Print non zero numbers
END IF
END For
6. Stop
-
7/28/2019 Cpl Manual
47/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 47
Flowchart
-
7/28/2019 Cpl Manual
48/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 48
Program
#include
#include
#include
int main()
{
int num[120], i, j,n;printf("enter the value of n\n);
scanf(%d,&n);
#pragma omp parallel for
for(i=0;i
-
7/28/2019 Cpl Manual
49/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 49
Sample output
The prime numbers that are less than 100
Prime no=2 threadID=0
Prime no=3 threadID=0
Prime no=5 threadID=0
Prime no=7 threadID=0
Prime no=11 threadID=0Prime no=13 threadID=0
Prime no=17 threadID=0
Prime no=19 threadID=0
Prime no=23 threadID=0
Prime no=29 threadID=0
Prime no=31 threadID=0
Prime no=37 threadID=0
Prime no=41 threadID=0
Prime no=43 threadID=0
Prime no=47 threadID=0
Prime no=53 threadID=0
Prime no=59 threadID=0
Prime no=61 threadID=0
Prime no=67 threadID=0
Prime no=71 threadID=0
Prime no=73 threadID=0
Prime no=79 threadID=0
Prime no=83 threadID=0
Prime no=89 threadID=0
Prime no=97 threadID=0
-
7/28/2019 Cpl Manual
50/55
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.
Algorithm
1. Start
2. Read the string
3. Print the string
4. Call function reverse(s)
5. Stop
Algorithm to reverse a string
1. Start
2. Calculate the length
3. For i 0, i
-
7/28/2019 Cpl Manual
51/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 51
Program
#include
#include
void reverse(char s[])
{
char s[10],temp;
int i,len;len = strlen(s)-1;
for(i=0;i
-
7/28/2019 Cpl Manual
52/55
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).
Algorithm
1. Start
2. Read string in str1
3. Read string in str2
4. Call function x = matchany(str1,str2)
5. IF x is equal -1
Print no character matching
ELSE
Print position and matched character
End if
6. Stop.
Algorithm of matchany function
1.For i 0 to s1[i] != null in steps of 1
For j 0 to s2[j] != null in steps of 1
IF s1[i] = s2[j]
Return i
End IF
End For
End For
2. Return -1
3. Return
-
7/28/2019 Cpl Manual
53/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 53
Flowchart
-
7/28/2019 Cpl Manual
54/55
Computer Programming Lab Manual
Noor-E-Saba, Dept. of CSE@HKBKCE Page 54
Program
#include
#include
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 Output
Enter first String: india
Enter second String: mandia
The position is 1 and character is i
Enter first String: hkbk
Enter second String: class
No character matching
-
7/28/2019 Cpl Manual
55/55
Computer Programming Lab Manual
END