Vous êtes sur la page 1sur 20

B.T.

L INSTITUTE OF TECHNOLOGY

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

I SEMESTER

COMPUTER PROGRAMMING LAB


(14CPL16-26)

LABORATORY MANUAL
CONTENTS

Sl Name of the Experiment


No. Part -A
1 Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a
Quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a C
program for the developed flowchart/algorithm and execute the same to output the possible roots
for a given set of coefficients with appropriate messages.

2 Design and develop an algorithm to find the reverse of an integer number NUM and check
whether it is PALINDROME or NOT. Implement a C program for the developed algorithm that
takes an integer number as input and output the reverse of the same with suitable messages. Ex:
Num: 2014, Reverse: 4102, Not a Palindrome.
3. a. Design and develop a flowchart to find the square root of a given number N. Implement
a C program for the same and execute for all possible inputs with appropriate messages.
Note: Don’t use library function sqrt(n).
b. Design and develop a C program to read a year as an input and find whether it is leap
year or not. Also consider end of the centuries.
4 Design and develop an algorithm for evaluating the polynomial f(x) = a4x4 + a3x3 + a2x2+ a1x + a0, for
a given value of x and its coefficients using Horner’s method. Implement a C program for the
developed algorithm and execute for different sets of values of coefficients and x.
5 Write C Program to compute Sin(x) using Taylor series approximation given by Sin(x) = x - (x 3/3!)
+ (x5/5!) - (x7/7!) + ……. Compare the result with the built- in Library function and print both the
results.
6 Develop, implement and execute a C program that reads N integer numbers and arrange them in
ascending order using Bubble Sort technique. Extend the program to perform a search operation on
these sorted numbers by accepting a key element from the user applying Binary Search method.
Report the result SUCCESS or FAILURE as the case may be.
7 Develop, implement and execute a C program that reads two matrices A (m x n ) and B (p x q ) and
Compute the product A and B. Read matrix A in row major order and matrix B in column major
order. Print both the input matrices and resultant matrix with suitable headings and in matrix format.
Program must check the compatibility of orders of the matrices for multiplication. Report
appropriate message in case of incompatibility.
8 Write and execute a C program that
i. Implements string copy operation STRCOPY(str1,str2) that copies a string str1 to another
string str2 without using library function.
ii. Reads a sentence and prints frequency of each of the vowels and total count of consonants.
9 a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as input and
returns value of the integer x rotated to the right by n positions. Assume the integers are
unsigned. Write a C program that invokes this function with different values for x and n and
tabulate the results with suitable headings.
b. Design and develop a C function isprime(num) that accepts an integer argument and returns 1 if
the argument is prime, a 0 otherwise. Write a C program that invokes this function to generate
prime numbers between the given ranges.
10 Develop a function in C called MatchAny(s1,s2) that takes two string arguments and does the
following task:
i. if s1 is substring of s2, Returns 1 along with the first location in the string s2,
ii. if s1 is equal to s2 , returns 0 and
iii. otherwise, returns -1.
Write a C program that invokes MatchAny(s1,s2) for different input strings and output both the
strings s1 & s2 with the return value in tabular form. Note: Do not use the standard library
functions.

11 Draw the flowchart and write a recursive C function to find the factorial of a number, n!, defined
by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C program to
compute the binomial coefficient nCr. Tabulate the results for different values of n and r with
suitable messages.
12 Given two text documentary files “Ramayana.in” and “Mahabharatha.in”. Write a C program to
create a new file “Karnataka.in” that appends the content of file “Ramayana.in” to the file
“Mahabharatha.in”. Display the contents of output file“Karnataka.in” on to screen. Also find number
of words and newlines in the output file.
13 Write a C program to maintain a record of “n” student details using an array of structures with four
fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data type. Print the
marks of the student given student name as input.

14 a. Write a C program using pointers to compute the sum of all elements stored in an array A[n].
Where n is the number of elements.
b. Write a C program to find sum of n elements entered by the user. Demonstrate the program
using functions malloc() to allocate memory dynamically and free() to deallocate.
15 a. Write a C program using pointers to find the median of a list of members.
b. Write a C program to find first minimum element and its position in an unsorted array. Use
pointer technology.
Program :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 co-effecienets :\n");
scanf("%d%d%d",&a,&b,&c);
if(a *b*c== 0)
{
printf("\n 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("\n The roots are real and distinct\n");
printf("\n The roots are 1) %f\n 2) %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) %f \n2) %f",x1,x2);
}
else
{
x1 = -b / (2.0 * a);
x2 = r / (2.0*a);
printf("\n The roots are complex\n");
printf("\n The roots are:\n1) %f +i %f\n2) %f -i %f",x1,x2,x1,x2);
}
getch();
}
Program: 2

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

void main()
{
int n,org,rev=0;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
org = n;

while(n != 0)
{
rem = n%10.
rev= rev*10+rem;
n = n / 10;
}

if(org == rev)
printf("\n The Number is Palindrome");
else
printf("\n The Number is not Palindrome");
getch();
}

Sample Output

Enter a number : 123


The Number is not Palindrome
Program: 3

a) Design and develop a flowchart to find the square root of a given number N. Implement a
C program for the same and execute for all possible inputs with appropriate messages. Note:
Don’t use library function sqrt(n).

#include<stdio.h>
#include<conio.h>

int main()
{
float i,j;
float num;
j=0.0001;
clrscr();

printf("ENTER ANY NUMBER : ");


scanf("%f",&num);

for(i=0;i<num;i=i+j)
{
if((i*i)>num)
{
i=i-j;
break;
}
}
printf("%.2f",i);
getch();
return 1;
}

Or

#include<stdio.h>
#define MAX_NUMBER 5000

int find_sqrt(int number)


{
int i,product = 0;
for(i=0;i<MAX_NUMBER;i++)
{
product = i*i;
if(product==number)
return i;
else if(product>number)
break;
}
return 0;
}

int main()
{
int n=0,result=0;
printf("enter the number to find the sqrt\n");
scanf("%d",&n);
if(n<0)
{
printf("enter only +ve integer value");
return 0;
}
result = find_sqrt(n);
if(result)
printf("sqrt of %d is %d\n",n,result);
else
printf("not a proper value for finding the sqrt\n");
}

b) Design and develop a C program to read a year as an input and find whether it is leap year
or not. Also consider end of the centuries.

#include <stdio.h>

int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}
Program :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 Horner’s method

include<stdio.h>
#include<math.h>
void main()
{
int size,i;
float a[20],sum=0,x;
clrscr();

printf("enter the size of the array");


scanf("%d",&size);
printf("enter the elements of the array\ size+1 n");
for(i=0;i<size;i++)
scanf("%f",&a[i]);
printf("enter the x value");
scanf("%f",&x);

sum=a[size]*x;
for(i=size-1;i>0;i--)
sum=(sum+a[i])*x;
sum=sum+a[0];
printf("results of the polynomial evaulation is %f",sum);
getch();
}

Sample input :

Enter the degree of the polynomial : 2

Enter the elements


3
5
7

Enter the value of x :2

The sum of polynomial = 29

NOTE:

Sol: If the degree of polynomial is 2 ,then our polynomial equation will be


3x2 + 5x + 7.
Value of x = 2 then
3(2)2 + 5 (2) +7
The sum of polynomial = 29
Program: 5
double sine(double x){
double sign, y;
static double sin;
sin = x;
sign= -1;
for(y = 3; y < 13; y += 2){
sin = sin + sign * pow(x, y) / fact(y);
sign*=-1;
}
return sin;
}

Program: 6

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("\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 = 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("\n The original elements are ");
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]);

getch();
}
Sample Output

Enter the no. of elements : 5

Enter 5 elements
3
6
1
0
9

The original elements are :


3
6
1
0
9

The Sorted elements are :


0
1
3
6
9
Program: 7
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 <conio.h>

void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
clrscr();
printf("\n Enter the order of the matrix A :");
scanf("%d%d",&m,&n);
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 order of the matrix B :");
scanf("%d%d",&p,&q);
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]);
if(n != p)
{
Printf(“\n Matrix multiplication not possible\n”);
getch();
exit(0);
}
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]);
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();
}

Sample Output

Enter the order of the matrix A :2 2

Enter the elements of matrix A


1
2
3
4

Enter the order of the matrix B :2 2

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
Program: 8

i Implements string copy operation STRCOPY(str1,str2) that copies a string str1 to


another string str2 without using library function

#include<stdio.h>

void copy_string(char*, char*);

main()
{
char source[100], target[100];

printf("Enter source string\n");


gets(source);

copy_string(target, source);

printf("Target string is \"%s\"\n", target);

return 0;
}

void copy_string(char *target, char *source)


{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}

ii. Reads a sentence and prints frequency of each of the vowels and total count of
consonants.

/*
* C program to read a sentence and count the total number of vowels
* and consonants in the sentence.
*/
#include <stdio.h>

void main()
{
char sentence[80];
int i, vowels = 0, consonants = 0, special = 0;

printf("Enter a sentence \n");


gets(sentence);
for (i = 0; sentence[i] != '\0'; i++)
{
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')
{
special = special + 1;
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
}

Program: 9

a) Design and develop a C function RightShift(x ,n) that takes two integers x and n as
input and returns value of the integer x rotated to the right by n positions. Assume the
integers are unsigned. Write a C program that invokes this function with different
values for x and n and tabulate the results with suitable headings.
#include<stdio.h>

#include<stdlib.h>

#define MAX 15

void rightShift(unsigned int numbers[], unsigned int positions[],int n);

int main()

unsigned int numbers[MAX],positions[MAX],n;

int i;

system("clear");

printf("\nThis programs takes two sets of inputs : \n");

printf("____________________________________________\n ");

printf("\t1. Set of numbers to be right shifted and \n\t2. Set of positions by which the
numbers are to be right shifted \n\n");

printf("Output is the Set of given numbers right shifted by corresponding given positions
\n");

printf("\nHow many numbers you want to Right Shift:");

scanf("%d",&n);

printf("\n Enter the numbers and corresponding positions\n");

printf("___________________________________________________ ");
for(i=0;i<n;i++)

printf("\n NUMBER\t :");scanf("%d",&numbers[i]);

printf(" POSITION :");scanf("%d",&positions[i]);

}
rightShift(numbers,positions,n);

return 0;

void rightShift(unsigned int numbers[], unsigned int positions[],int n)

int i;

unsigned int result[MAX];

printf("\nNUMBERS\t\tPOSITIONS\tRIGHT SHIFTED RESULTS \n");

printf("_____________________________________________________ ");

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

result[i]=numbers[i]>>positions[i];

printf("\n%d\t\t%d\t\t\t%d",numbers[i],positions[i],result[i]);

printf("\n");

b) Design and develop a C function isprime(num) that accepts an integer argument and
returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this
function to generate prime numbers between the given ranges.
/*PROGRAM FOR PRIME NUMBER using FUNCTION **/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int prime(int x);
int n,f;
clrscr();
printf(" ENTER THE ANY NUMBER \n");
scanf("%d",&n);
f=prime(n);
if(f==1)
printf(" NUMBER IS PRIME \n");
else
printf("NUMBER IS NOT PRIME \n");
getch();
}

int prime(int x)
{
int i,a,r=0;
a=sqrt(x);
if((x==1)||(x==2))
return(0);
else
{
for(i=2;i<=a;i++)
if(x%i==0)
return(0);
return(1);
}
}
Program:10

Develop a function in C called MatchAny(s1,s2) that takes two string arguments and does the
following task:
i. if s1 is substring of s2, Returns 1 along with the first location in the string s2,
ii. if s1 is equal to s2 , returns 0 and
iii. otherwise, returns -1.
Write a C program that invokes MatchAny(s1,s2) for different input strings and output both the
strings s1 & s2 with the return value in tabular form. Note: Do not use the standard library
functions.

#include <stdio.h>
int matchany(char s1[], char s2[]);

void main()
{
char s1[20],s2[20];
int position;

printf("Enter the first String: ");


gets(s1);

printf("Enter the second String: ");


gets(s2);

position=matchany(s1,s2);
printf("The First String is : %s\n",s1);
printf("The Second String is : %s\n",s2);

if (position == -1)
printf("No character of %s is present in %s\n",s2,s1);
else
printf("Postion=%d\n",position);
}

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


{
int i,j;
char symbol;

for (i=0; s1[i]; i++)


{
symbol=s1[i];
for(j=0; s2[j]; j++)
{
if(symbol==s2[j])return (i+1);
}
}
return -1;
}

Program: 11
Draw the flowchart and write a recursive C function to find the factorial of a number, n!, defined
by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C program to
compute the binomial coefficient nCr. Tabulate the results for different values of n and r with
suitable messages.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,r,ncr;
clrscr();
printf(“\n enter n and r:”);
scanf(“%d%d”,&n,&r);
ncr=fact(n)/(fact(n-r)*fact(r));
printf(“\n Binomial co-efficient : %d”,ncr);
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}

Program: 12

Given two text documentary files “Ramayana.in” and “Mahabharatha.in”. Write a C program
to create a new file “Karnataka.in” that appends the content of file “Ramayana.in” to the file
“Mahabharatha.in”. Display the contents of output file“Karnataka.in” on to screen. Also find
number of words and newlines in the output file.
include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fs1, *fs2, *ft;

char ch, file1[20], file2[20], file3[20];

printf("Enter name of first file\n");


gets(file1);

printf("Enter name of second file\n");


gets(file2);

printf("Enter name of file which will store contents of two files\n");


gets(file3);

fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");

if( fs1 == NULL || fs2 == NULL )


{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}

ft = fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

while( ( ch = fgetc(fs1) ) != EOF )


fputc(ch,ft);

while( ( ch = fgetc(fs2) ) != EOF )


fputc(ch,ft);

printf("Two files were merged into %s file successfully.\n",file3);

fclose(fs1);
fclose(fs2);
fclose(ft);

return 0;
}

Program 13
Program :14
a)
b)

Program: 15
a)
b)

Vous aimerez peut-être aussi