ccp lab manual.... rasagna rajesh

36
CCP LAB MANUAL 2012-2013 Rasagna Rajesh, CSE, CITECH Page 1 VISVESVARAYA TECHNOLOGICAL UNIVERSITY Belgaum, Karnataka Computer Concepts and C Programming Lab Manual I/II Semester CSE Prepared by Mrs. RASAGNA REDDY A.P, CSE, CITECH & Ms. Nikitha Shree A.P, CSE, CITECH Department of Computer Science & Engineering CAMBRIDGE INSTITUTE OF TECHNOLOGY KR Puram, Bangalore

Upload: rasagna

Post on 28-Oct-2014

7.253 views

Category:

Documents


1 download

DESCRIPTION

This lab manual is intended to help all 1st and 2nd semester B.E, VTU students. All the programs in this manual are copies of executed ones. Please gothrough every line in the manual as the note(s) given in the manual may help in understanding the logic.

TRANSCRIPT

Page 1: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 1

VISVESVARAYA TECHNOLOGICAL

UNIVERSITY Belgaum, Karnataka

Computer Concepts and C Programming

Lab Manual

I/II Semester

CSE

Prepared by

Mrs. RASAGNA REDDYA.P, CSE, CITECH

&

Ms. Nikitha ShreeA.P, CSE, CITECH

Department of Computer Science & Engineering

CAMBRIDGE INSTITUTE OF TECHNOLOGY

KR Puram, Bangalore

Page 2: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 2

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()

{

int a,b,c;

float d,x1,x2,r;

clrscr();

printf("Enter the three coefficients:\n");

scanf("%d%d%d",&a,&b,&c);

if(a*b*c==0)

{

printf("Invalid input");

getch();

exit(0);

}

d=b*b-4*a*c;

r=sqrt(abs(d));

if(d>0)

{

x1=(-b+r)/(2.0*a);

x2=(-b-r)/(2.0*a);

printf("The roots are real and distinct....\n");

printf("The roots are...\n(1) %f\n(2) %f\n",x1,x2);

}

else if(d==0)

Page 3: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 3

{

x1=x2=-b/(2.0*a);

printf("The roots are real and equal....\n");

printf("The roots are...\n(1) %f\n(2) %f\n",x1,x2);

}

else

{

x1=-b/(2.0*a);

x2=r/(2.0*a);

printf("The roots are complex and imaginary....\n");

printf("The roots are...\n(1) %f+i %f\n(2) %f-i %f",x1,x2,x1,x2);

}

getch();

}

OUTPUT:

Enter the three coefficients:

4 8 2

The roots are real and distinct....

The roots are...

(1) -0.292893

(2) -1.707107

Enter the three coefficients:

1 -4 4

The roots are real and equal....

The roots are...

(1) 2.000000

(2) 2.000000

Page 4: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 4

Enter the three coefficients:

1 2 2

The roots are complex and imaginary....

The roots are...

(1) -1.000000+i 1.000000

(2) -1.000000-i 1.000000

Enter the three coefficients:

9 0 2

Invalid input

Algorithm: Roots of a Quadratic Equation

Step1: [Input the coefficients of quadratic equation]

Read a,b,c.

Step2:[Compute d and r]

d=b*b-4*a*c

r=√d

Step3: [Check for d value to compute roots]

if (d>0) then

print real and distinct roots

x1=(-b+r)/(2*a)

x2=(-b-r)/(2*a)

go to step4

if (d==0) then

print real and equal roots

x1 = x2= -b/(2*a)

go to step 4

if (d<0) then

Page 5: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 5

print real and imaginary

x1+i x2

x1-i x2

goto step 4

Step 4: Stop

Flowchart:

2.Design,develop and execute a program in C to implements Euclid’s 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 m,n,p,q,gcd,lcm,rem;

Page 6: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 6

clrscr();

printf("Enter two numbers:\n");

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("The LCM =%d\n",lcm);

printf("\nThe GCD =%d\n",gcd);

getch();

}

OUTPUT:

Enter two numbers:

45 90

The LCM =90

The GCD =45

Algorithm 2: To Find the GCD and LCM of two integers

Step 1: Start

Step 2: [Input two numbers m and n]

Step 3: [save the two numbers]

p=m, q =n

Page 7: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 7

Step 4:[compute GCD of two numbers]

while(n!=0)

r=m%n

m=n

n=r

End while

Step 5:[Compute GCD and LCM]

gcd=m

lcm=p*q/gcd

Step 6: [display GCD and LCM]

Print gcd,lcm

Step 6:[Finished]

Stop

Flowchart:

Page 8: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 8

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, org, rev=0, rem=0;

clrscr();

printf("enter a number:\n");

scanf("%d",&n);

org = n;

if(n<=999||n>9999)

{

printf("\nNot a 4 digit number\n");

getch();

exit(0);

}

while(n!=0)

{

rem=n%10;

rev=rev*10+rem;

n=n/10;

}

if(org==rev)

printf("\nThe number is palindrome");

else

printf("\nThe number is not palindrome");

getch();

}

OUTPUT:

Enter a number:

1234

The number is not palindrome

Enter a number:

2332

The number is palindrome

Algorithm: [Checking whether a number is palindrome]

Step 1: Start

Step 2: [Read n from the user]

Read n

Step 3: [Assigning variable org with n value]

Page 9: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 9

Assign org=n

Step 4: [Checking whether n is 4 digit number]

If n<=999 or n>9999

Write “Not a 4 digit number"

Stop

End if

While n!=0

rem=n%10

rev=rev*10+rem

n=n/10

End while

If org=rev then

Write "The number is palindrome"

Else

Write “The number is not palindrome"

End IF

Step 5: Stop

Flowchart:

Page 10: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 10

4.Design, develop and execute a program in C to evaluate the given polynomial f(x)=a4x4+

a3x3+ a2x

2+ a1x+ a0 for given index and the coefficients using Horner’s method.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,x,a[10],poly=0;

clrscr();

printf("Enter the degree of the polynomial:\n");

scanf("%d",&n);

printf("Enter the %d coefficients\n",n+1);

for(i=0;i<=n;i++)

{

printf("a[%d]=",i);

scanf("%d",&a[i]);

}

printf("Enter the value of x:\n");

scanf("%d",&x);

poly=a[n];

for(i=n-1;i>=0;i--)

poly=poly*x+a[i];

printf("The sum of polynomial=%d\n",poly);

getch();

}

OUTPUT:

Enter the degree of the polynomial:

2

Enter the 3 coefficients

a[0]=7

a[1]=5

a[2]=3

Enter the value of x:

3

The sum of polynomial=49

(The polynomial equation is 3x2+5x+7 for degree 2.

When x is 3, sum= 3(3)2+5(3)+7=49)

Algorithm: [Evaluating a polynomial equation]

Step 1: Start

Step 2: [Reading degree of polynomial from the user]

Read n

Step 3: [Reading the coefficients]

For i=0 till =n

Read a[i]

Page 11: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 11

End for

Step 4:[Reading value of x]

Read x

Step 5:[Evaluating]

poly=a[n]

For i=n-1 till i>=0

poly=poly*x+a[i]

End for

Step 6: [Display the output to the user]

Write poly.

Step 7: Stop

Flowchart:

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>

void main()

{

int inspace=0;

char c;

clrscr();

printf("Enter a string with more spaces:\n");

while((c=getchar())!='\n')

{

if(c==' ')

{

Page 12: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 12

if(inspace==0)

{

inspace=1;

putchar(c);

}

}

if(c!=' ')

{

inspace=0;

putchar(c);

}

}

getch();

}

OUTPUT:

Enter a string with more spaces:

Welcome to CITECH.

Welcome to CITECH.

Algorithm: [Eliminating extra spaces in strings]

Step 1: Start

Step 2: [Reading string from user]

While (c=getchar())!='\n'

If c=' '

And If inspace=0 then

Assign inspace=1

Print c

End if

If c!=' ' then

Assign inspace=0

Print c

End if

End while

Step 3: Stop

Page 13: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 13

Flowchart:

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.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,key,low,high,mid,F=0,a[10];

clrscr();

printf("Enter the number of elements:\n");

scanf("%d",&n);

printf("Enter %d elements in ascending order\n",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter the key element to search\n");

scanf("%d",&key);

low=0;

high=n-1;

while(low<=high)

{

mid=(low+high)/2;

if(key==a[mid])

Page 14: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 14

{

F=1;

break;

}

if(key>a[mid])

low=mid+1;

else

high=mid-1;

}

if(F==1)

printf("Search successful....\n");

else

printf("Search failed....\n");

getch();

}

OUTPUT:

Enter the number of elements:

5

Enter 5 elements in ascending order

7 9 10 16 24

Enter the key element to search

16

Search successful...

Enter the number of elements:

5

Enter 5 elements in ascending order

1 6 9 12 15

Enter the key element to search

3

Search failed....

Algorithm: Binary search

Step 1: [Read the number of elements and array a[]]

Read n

For i=0 to n

Read a[i]

Step 2: [Input the key element to be searched]

Read key

Step 3: [Initialization]

Low=0

High=n-1

Step 4: [Searching the key element]

While low<=high

Mid=(low+high)/2

If key=a[mid]

Page 15: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 15

Write “Search successful”

Stop

End if

If key<a[mid]

High=mid-1

Else

Low=mid+1

End while

Write “Unsuccessful search”

Step 6: Stop

Flowchart:

Page 16: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 16

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.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,j,a[10],b[10],temp;

clrscr();

printf("Enter the no.of elements\n");

scanf("%d",&n);

printf("Enter the %d elements\n",n);

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

b[i]=a[i];

}

for(i=1;i<n;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("The original element are\n");

for(i=0;i<n;i++)

printf("%d\n",b[i]);

printf("The sorted elements are\n");

for(i=0;i<n;i++)

printf("%d\n",a[i]);

getch();

}

OUTPUT:

Enter the no.of elements

5

Enter the 5 elements

9 6 7 2 8

The original element are

9

6

7

2

8

Page 17: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 17

The sorted elements are

2

6

7

8

9

Algorithm: Bubble Sort

Step 1: Start

Step 2: [Reading n value and array to be sorted from the user]

Read n

Read n elements in a[]

After reading each element assign to b[]

b[i]=a[i]

Step 3: [Comparing elements to sort]

Compare current element with the previous

If a[j]>a[j+1]

Then swap both elements

temp=a[j]

a[j]=a[j+1]

a[j+1]=temp

Step 4:[Displaying the original and sorted to the console]

Write n elements of b[]

Write n elements of a[]

Step 5: Stop

Flowchart:

Page 18: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 18

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()

{

int count=0;

unsigned int n;

clrscr();

n=~0;

while(n!=0)

{

n=n>>1;

count++;

}

printf("The word length on host machine is %u",count);

getch();

}

OUTPUT:

The word length on host machine is 16

Algorithm: Word length on host machine.

Step 1: Start.

Step 2: [Assign value to n]

n=~0

while n!=0

do right shift n by 1 and place in n.

Increment the count.

When n<=0

Terminate the while loop and goto Step 3

Step 3:[Display value to console]

Write count.

Step 4: Stop

Page 19: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 19

Flowchart:

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

#include<float.h>

#include<math.h>

int fact(int n)

{

int i,sum=1;

for(i=n;i>1;i--)

sum*=i;

return sum;

}

float expression(float x)

{

int i=1;

float value=0,num=1;

while(num>FLT_EPSILON)

{

value+=num;

num=(pow(x,i)/(fact(i)));

i++;

}

return value;

}

Page 20: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 20

void main()

{

float y;

clrscr();

printf("The value of FLT_EPSILON of float.h is %f\n",FLT_EPSILON);

y=expression(0.5);

printf("The value of exp(0.5) is %f",y);

getch();

}

OUTPUT:

The value of FLT_EPSILON of float.h is 0.000000

The value of exp(0.5) is 1.648721

Algorithm: [Taylor series to find exp(0.5)]

Step 1:Start

Step 2: [Calling the function expression(0.5)]

y=expression(0.5)

num=1

while num>FLT_EPSILON

assign

value+=num;

num=(pow(x,i)/(fact(i)))

When num < or equal FLT_EPSILON then the “value” is assigned to y.

Step 3: [Writing y to the console]

Display y.

Step 4: Stop

Flowchart:

Page 21: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 21

10. Design, develop and execute a program in C to read two matrices A(M*N) and B(P*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 the format if the matrices are compatible for multiplication, otherwise the

program must print the 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];

int m,n,p,q,i,j,k;

clrscr();

printf("Enter the order of the matrix A\n");

scanf("%d%d",&m,&n);

printf("Enter the order of the matrix B\n");

scanf("%d%d",&p,&q);

if(n!=p)

{

printf("Matrix multiplication not possible\n");

getch();

exit(0);

}

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 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 ",a[i][j]);

Page 22: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 22

printf("\n");

}

printf("\n MATRIX B\n");

for(i=0;i<p;i++)

{

for(j=0;j<q;j++)

printf("%d ",b[i][j]);

printf("\n");

}

printf("\n MATRIX C\n");

for(i=0;i<m;i++)

{

for(j=0;j<q;j++)

printf("%d ",c[i][j]);

printf("\n");

}

getch();

}

OUTPUT:

Enter the order of the matrix A

2 2

Enter the order of the matrix B

2 3

Enter the elements of matrix A...

1 2

3 4

Enter the elements of matrix B...

1 2 3

4 5 6

MATRIX A

1 2

3 4

MATRIX B

1 2 3

4 5 6

MATRIX C

9 12 15

19 26 33

OUTPUT:

Enter the order of the matrix A

2 2

Enter the order of the matrix B

Page 23: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 23

3 3

Matrix multiplication not possible

Algorithm: [Matrix multiplication]

Step 1: Start

Step 2:[Read input arrays from the user]

Read order of matrices a[][] and b[][] i.e m*n and p*q

Read a[][] and b[][] arrays.

Step 3: [check for possibility of multiplication]

If n!=p

Write Matrix multiplication not possible.

Else

Goto Step 4

Step 4:[Multiplying two matrices]

c[i][j]=0

c[i][j]=c[i][j]+(a[i][k]*b[k][j])

Step 5:[Write the result to the console]

Write output array c[] and both input arrays a[] and b[].

Step 6: Stop

Flowchart:

Page 24: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 24

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

#include<stdio.h>

#include<omp.h>

int main()

{

int a[10],b[10],c[10],i,n;

omp_set_num_threads(3);

printf("\nEnter the number of elements\n");

scanf("%d",&n);

printf("\nEnter the element of 1st array\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter the elements of 2nd array\n");

for(i=0;i<n;i++)

scanf("%d",&b[i]);

printf("The contents of array A are....\n");

for(i=0;i<n;i++)

{

printf("a[%d]=%d\n",i,a[i]);

}

printf("The contents of array B are....\n");

for(i=0;i<n;i++)

{

printf("b[%d]=%d\n",i,b[i]);

}

printf("Adding the elements....\n");

#pragma omp parallel for shared(c)

for(i=0;i<n;i++)

{

c[i]=a[i]+b[i];

printf("c[%d]=%d,and thread id executing it

is=%d\n",i,c[i],omp_get_thread_num());

}

printf("The new array is....\n");

for(i=0;i<n;i++)

{

printf("%d\t",c[i]);

}

}

OUTPUT:

[cseise@localhost ~]$ vi p11.c

[cseise@localhost ~]$ cc -fopenmp p11.c

Page 25: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 25

[cseise@localhost ~]$ ./a.out

Enter the number of elements

5

Enter the element of 1st array

2 4 6 8 10

Enter the elements of 2nd array

2 3 4 5 6

The contents of array A are....

a[0]=2

a[1]=4

a[2]=6

a[3]=8

a[4]=10

The contents of array B are....

b[0]=2

b[1]=3

b[2]=4

b[3]=5

b[4]=6

Adding the elements....

c[4]=16,and thread id executing it is=2

c[2]=10,and thread id executing it is=1

c[3]=13,and thread id executing it is=1

c[0]=4,and thread id executing it is=0

c[1]=7,and thread id executing it is=0

The new array is....

4 7 10 13 16

[cseise@localhost ~]$

Algorithm: [Parallel program to add two arrays values concurrently]

Step 1: start

Step 2: [Reading n value and arrays from the user]

Read n.

Read n elements into a[] and b[] .

Step 3: [Adding elements of both a[] and b[] concurrently]

Add all n elements from both a[] and b[] concurrently and placed in c[]

c[i]=a[i] + b[i]

Step 4: [Writing values to console]

Write c[] and thread number.

Step 5: Stop

Page 26: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 26

Flowchart:

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

#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)+32768;

}

return x;

}

void main()

{

unsigned int x,res,n;

clrscr();

printf("Enter the unsigned integer:\n");

scanf("%u",&x);

Page 27: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 27

printf("Enter the number of rotations:\n");

scanf("%d",&n);

res=rightrot(x,n);

printf("After rotating %d times result=%u",n,res);

getch();

}

OUTPUT:

Enter the unsigned integer:

5

Enter the number of rotations:

1

After rotating 1 times result=32770

(NOTE: If the host machine is 16 bit.

0000 0000 0000 0101 is binary format of 5.

Since 5 is odd 5 is right shifted by number of positions entered and result is added to 32768

i.e the result is 21+32768=32770)

Enter the unsigned integer:

4

Enter the number of rotations:

7

After rotating 7 times result=2048

(NOTE: 0000 0000 0000 0100 is 7 in binary representation. 7 when rotated result is 0000

1000 0000 0000

When this binary format is converted back to decimal the result is 211

= 2048)

Algorithm: [Rotating a number by n bit positions]

Step 1: Start

Step 2: [Reading input values from user]

Read x and n

Step 3: [Calling rightrot()]

Res=rightrot(x,n)

Step 4: [Writing res value]

Write res.

Step 5: Stop

Page 28: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 28

Flowchart:

13. Design and develop the function isprime(x) that accepts an integer argument and

returns 1 if 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>

#include<conio.h>

int isprime(x)

{

int i;

if(x==0||x==1)

return -1;

for(i=2;i<=(x/2);i++)

{

if(x%i==0)

return 0;

}

return 1;

}

void main()

{

int x, res=0;

clrscr();

printf("Enter a number");

scanf("%d",&x);

res = isprime(x);

Page 29: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 29

if(res==1)

printf("%d is a prime number",x);

else if(res==0)

printf("%d is not a prime number ",x);

else

printf("%d is neither prime nor composite",x);

getch();

}

OUTPUT:

Enter a number

5

5 is a prime number

Enter a number

4

4 is not a prime number

Enter a number1

1 is neither prime nor composite

Algorithm: To check whether a number is prime

Step 1: Start

Step 2: [Reading input value]

Read x

Step 3: [Calling function isprime()]

Res=isprime()

Step 4: [Checking for return value from function]

If res=1 then

Write x is prime number.

Goto step 5

If res=0 then

Write x is not prime.

Goto step 5

If x==-1 then

Write x is neither prime nor composite.

Goto step 5

Step 5: Stop

Page 30: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 30

Flowchart:

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 Sieve of Eratosthenes.

(Note: Execute the program in Linux)

#include<stdio.h>

#include<omp.h>

int main()

{

int num[100],i,j;

omp_set_num_threads(3);

for(i=0;i<=99;i++)

{

num[i]=i+1;

printf("%d\t",num[i]);

}

printf("\n");

#pragma omp parallel for

for(i=1;i<=99;i++)

{

if(num[i]!=0)

{

for(j=(i+1);j<=99;j++)

{

Page 31: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 31

if(num[j]!=0)

{

if((num[j]%num[i])==0)

num[j]=0;

}

}

}

}

printf("The prime numbers are...\n");

#pragma omp parallel for

for(i=0;i<=99;i++)

{

if(num[i]!=0)

printf("%d\t Thread executing it is %d\n",num[i],omp_get_thread_num());

}

}

OUTPUT:

[cseise@localhost ~]$ vi p14.c

[cseise@localhost ~]$ cc -fopenmp p14.c

[cseise@localhost ~]$ ./a.out

1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29 30 31 32 33 34 35 36 37

38 39 40 41 42 43 44 45 46 47 48 49

50 51 52 53 54 55 56 57 58 59 60 61

62 63 64 65 66 67 68 69 70 71 72 73

74 75 76 77 78 79 80 81 82 83 84 85

86 87 88 89 90 91 92 93 94 95 96 97

98 99 100

The prime numbers are...

71 Thread executing it is 2

73 Thread executing it is 2

79 Thread executing it is 2

83 Thread executing it is 2

89 Thread executing it is 2

97 Thread executing it is 2

37 Thread executing it is 1

41 Thread executing it is 1

43 Thread executing it is 1

47 Thread executing it is 1

53 Thread executing it is 1

59 Thread executing it is 1

Page 32: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 32

61 Thread executing it is 1

67 Thread executing it is 1

1 Thread executing it is 0

2 Thread executing it is 0

3 Thread executing it is 0

5 Thread executing it is 0

7 Thread executing it is 0

11 Thread executing it is 0

13 Thread executing it is 0

17 Thread executing it is 0

19 Thread executing it is 0

23 Thread executing it is 0

29 Thread executing it is 0

31 Thread executing it is 0

[cseise@localhost ~]$

Algorithm:Parallel_prime

Step 1:[read the values of num[i] from 0 to 99]

Read num[i]

Step 2:[print the numbers from 1 to 100]

Print num[i]

Step 3:[filter the prime numbers using algorithm sieve of Eratosthenes]

For i=1 to 99 do

If num[i]!=0

For j=(i+1) to 99 do

If num[j]!=0

If num[j]%num[i]==0

num[j]=0

end if

end if

end for

end if

end for

Step 4:[Display prime numbers]

For i=0 to 99 do

If num[i]!=0

Print num[i],thread id

End if

End for

Step 5:[finish]

stop

Page 33: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 33

FLOWCHART:

15. Design, 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>

#include<string.h>

void reverse(char src[],char dest[])

{

int i,n;

n=strlen(src);

for(i=0;i<n;i++)

Page 34: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 34

{

dest[n-1-i]=src[i];

}

dest[n]='\0';

}

void main()

{

char s1[]="RAMA",d1[10];

char s2[]="SITA",d2[10];

char s3[]="RAVANA",d3[10];

clrscr();

reverse(s1,d1);

reverse(s2,d2);

reverse(s3,d3);

printf("Source string\tReversed string\n");

printf("_______________________\n");

printf("%s\t\t%s\n",s1,d1);

printf("%s\t\t%s\n",s2,d2);

printf("%s\t\t%s\n",s3,d3);

getch();

}

OUTPUT:

Source string Reversed string

_______________________

RAMA AMAR

SITA ATIS

RAVANA ANAVAR

Algorithm: Reversing strings

Step 1: Start

Step 2: [Assigning values to strings]

S1=”RAMA”

S2=”SITA”

S3=”RAVANA”

Step 3: [Calling reverse()]

Reverse(s1,d1)

Reverse(s2,d2)

Reverse(s3,d3)

Step 4: [Writing the values to console]

Write s1, d1

Write s2, d2

Write s3, d3

Step 5: Stop

Page 35: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 35

Flowchart:

16. Design and develop a funtion 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>

int matchany(char s1[],char s2[])

{

int i,j;

for(i=0;s2[i]!='\0';i++)

for(j=0;s1[j]!='\0';j++)

{

if(s2[i]==s1[j])

return i;

}

return -1;

}

void main()

{

int pos;

char str1[10],str2[10];

clrscr();

printf("Enter string 1:\n");

scanf("%s",str1);

printf("Enter string 2:\n");

scanf("%s",str2);

Page 36: CCP LAB MANUAL.... RASAGNA RAJESH

CCP LAB MANUAL 2012-2013

Rasagna Rajesh, CSE, CITECH Page 36

pos=matchany(str1,str2);

if(pos==-1)

printf("No letters in string 2 matches with letters in string 1\n");

else

printf("The letter \"%c\" at the position %d of string2 matches with the letter in

string1\n",str2[pos],pos+1);

getch();

}

OUTPUT:

Enter string 1:

RAMA

Enter string 2:

SITA

The letter "A" at the position 4 of string2 matches with the letter in string1

Algorithm: [Strings match]

Step 1: Start

Step 2:[Reading input values]

Read str1, str2

Step 3: [Call the function matchany()]

pos=matchany(str1,str2)

Step 5:[checking for pos value]

If pos==-1

Write “strings does not match”

Goto step 5

Else

Write “String 2 matches with string 1 at” pos

Goto Step 5

Step 4: Stop

Flowchart: