Department of Computer Science & Engineering Computer Programming Laboratory Lab Manual - 15Cpl16 Semester-I/Ii 2016-2017
Department of Computer Science & Engineering Computer Programming Laboratory Lab Manual - 15Cpl16 Semester-I/Ii 2016-2017
SEMESTER-I/II
2016-2017
TABLE OF CONTENTS
SYLLABUS
CREDITS - 02
Course objectives: To provide basic principles C programming language. To provide design & develop of C
programming skills. To provide practical exposures like designing flowcharts, algorithms, how to debug
programs etc.
Laboratory Session-1: Write-up on Functional block diagram of Computer, CPU, Buses, Mother Board,
Chip sets, Operating System & types of OS, Basics of Networking & Topology and NIC.
Laboratory Session-2: Write-up on RAM, SDRAM, FLASH memory, Hard disks, Optical media, CD-
ROM/R/RW, DVDs, Flash drives, Keyboard, Mouse, Printers and Plotters. Introduction to flowchart,
algorithm and pseudo code.
Note: These TWO Laboratory sessions are used to fill the gap between theory classes and practical
sessions. Both sessions are to be evaluated as lab experiments.
LABORATORY EXERCISES
Implement the following programs with WINDOWS / LINUX platform using appropriate C compiler.
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.
3a. 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).
3b. Design and develop a C program to read a year as an input and find whether it is leap year
4
4. Design and develop an algorithm to evaluate polynomial for a given value of x and
its coefficients using Horner’s method. Implement a C program for the same and execute the
program with different set of values of coefficients and x.
5. Draw the flowchart and Write a C Program to compute Sin(x) using Taylor series
approximationgiven by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + …….
Compare your result with the built- in Library function. Print both the results with
appropriate messages.
7. Develop, implement and execute a C program that reads two matrices A (m x n ) and
B (p x q ) and Compute product of matrices A and B. Read matrix A and matrix B in row
major order and in column major order respectively. Print both the input matrices and
resultant matrix with suitable headings and output should be in matrix format only.
Implement the following programs with WINDOWS / LINUX platform using appropriate
C compiler.
10.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 range.
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 university information files “studentname.txt” and “usn.txt” that contains
students Name and USN respectively. Write a C program to create a new file called
“output.txt” and copy the content of files “studentname.txt” and “usn.txt” into output
file in the sequence shown below . Display the contents of output file “output.txt” on
to the screen.
Student Name USN
Name 1 USN1
Name 2 USN2
…. ….
…. ….
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). Assume appropriate data type
for each field. Print the marks of the student, given the student name as input.
14. Write a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
COURSE OBJECTIVE
COURSE OUTCOME
1. Know the location of the fire extinguisher and the first aid box and how to use
them in case of an emergency.
2. Read and understand how to carry out an activity thoroughly before coming to
the laboratory.
3. Report fires or accidents to your lecturer/laboratory technician immediately.
4. Report any broken plugs or exposed electrical wires to your lecturer/laboratory
technician immediately.
DON’TS
LIST OF EXPERIMENTS
AIM: To find the roots of the quadratic equation (ax2+bx+c=0) with different possible input
values for a, b and c.
ALGORITHM :
ALGM: Quadratic Equation [This algorithm takes three coefficients as input and computes the roots]
Steps:
1. [Initialize] Start
2. [Input coefficients of quadratic equation]
read a ,b, c
3. [Check for valid coefficients]
If a =0 and b= 0 then
print “Roots cannot be determined”
[Check for linear equation]
else a=0then
root1 ← (-c/b)
print “Linear equation”,root1
goto step 5
4. [Compute discriminate value]
disc ← b*b-4*a*c
5. [Based on discriminate value, classify and calculate all possible roots and print them]
FLOWCHART:
start
Read a,b,c
Is Is a=0
Print
&
a=0? “Invalidroots”
F b=0? T
F
Disc <-b*b-4*a*c
Is disc Is disc
= 0? > 0?
F F
T T
Print “linear Print “Real Print “Real and Print “imaginary
equation” & equal roots” distinct roots” roots”
stop
PROGRAM :
/* Program to calculate all possible roots of a quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a, b, c, disc;
float root1,root2,real,imag;
clrscr();
printf("Enter a,b,c values\n");
scanf("%f%f%f",&a,&b,&c);
if( (a == 0) && (b == 0) &&(c==0))
{
printf("Invalid coefficients\n");
printf(" Try Again with valid inputs !!!!\n");
getch();
}
getch();
}
EXPECTED OUTPUT:
1. Enter a, b, c values
0 0 1
Invalid coefficients
Try Again with valid inputs!!!!
2. Enter a, b, c values
1 2 3
The roots are Real and Imaginary
Root1 = -1.000 + I 1.414
Root2 = -1.000 – I 1.414
3. Enter a, b, c values
1 2 1
The roots are real and equal
Root1 = -1.000
Root2 = -1.000
4. Enter a, b, c values
1 5 3
The roots are Real and Distinct
Root1 = -0.697
Root2 = -4.303
5. Enter a, b, c values
0 1 2
Linear equation
Root = -2.000
ALGORITHM :
ALGM: Palindrome [This algorithm takes an integer number as input and output the reverse of the same. Also
checks the number is palindrome or not]
Steps:
1. [Initialize] Start
2. [Input the original number]
read num
3. [Set number num to a variable n]
n ← num
4. [Iterate until num is not equal to 0.
If num value becomes 0, control comes out of the loop. So num’s original value is lost. So, num
value is stored in other variable n in step 3.
In step 4, reverse of the number is calculated.]
while ( num != 0) do
remainder ← num mod 10
num ← num/10
rev ← rev * 10 +remainder
5. [Print reverse number]
print rev
6. [Check if original number & reverse number are same. If it is, number is palindrome. Otherwise,
not palindrome]
if (rev = n) then
print palindrome
else
print not a palindrome
endif
7. [Finished]
End
FLOWCHART:
start
Read num
r num = 0
n= num
While(num!=0)
False
True
False
rem =num%10 If n= rnum
True
rnum = rnum*10+rem
num = num/10
stop
PROGRAM :
EXPECTED OUTPUT:
1. Enter the number
1001
The reverse number is 1001
1001 is a palindrome
SQUARE ROOT
ALGORITHM:
SQUARE ROOT [this algorithm takes an integer number as an input and prints the square root
of the number]
Input: the integer number
Step1: Initialize
Step3: if(n>=0)
then
Else
Step4: stop
FLOWCHART:
start
Read n
True
For s=1,2,….to s*s<=n
False
Decrement s
False True
Is (x*x)>
(double)n?
X<- x-0.001
stop
PROGRAM :
/* Program to calculate square root of the given number without using built in function */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() // Uses Brute-Force Method
{
int s;
double x,d,n;
clrscr();
printf("Enter the number\n");
scanf("%lf",&n);
if(n>=0)
{
for(s=1;s*s<=n;s++); //calculating decimal part of the square root
s--;
for(d = 0.001;d < 1.0;d += 0.001) // calculating the fractional part
{
x = (double)s + d;
if((x*x > (double)n))
{
x = x - 0.001;
break;
}
}
printf("The square root of the given number is %.3lf\n", x);
printf("The square root as per built in function sqrt()is %.2lf", sqrt(n));
}
else
{
printf( "No square root to a negative number");
}
getch();
}
EXPECTED OUTPUT:
1. Enter the number
16
The square root of the given number is 4.000
The square root as per built in function sqrt() is 4.00
ALGORITHM:
start
Enter valid
year
if((year%4==0
) &&
year%100!=0)
|| (year%400
==0))
Print the given year is leap Print the given year is not
year leap year
stop
PROGRAM :
/* Program to find whether given year is leap year or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("Enter valid year\n");
scanf("%d",&year);
if((year%4==0) && (year%100!=0) || (year%400 ==0)) //check for leap year
{
printf("%d is leap year", year);
}
else
{
printf("%d is not a leap year",year);
}
getch();
}
EXPECTED OUTPUT:
1. Enter valid year
2012
2012 is leap year
Evaluation of Polynomial
AIM: To find the value of the polynomial Design and develop an algorithm for evaluating
the polynomial f(x) = a4 x4 + a3 x3 + a2 x2+ a1 x + a0, for a given value of x and its
coefficients using Horner’s method.
ALGORITHM :
ALGM: EVAL_POLYNOMIAL f(x) = a4x4+ a3x3+ a2x2+ a1x+ a0using Horner’s method
Steps:
1. [Initialize] Start
2. [Input the number of coefficients n]
read n
3. Set sum to 0
4. [Read all coefficients a1, a2, a3, a4and constanta0]
For each value i in array a(i)do
read n+1 coefficients
endfor
5. [Input variable x]
read x
6. [Iterate from n to 0. Calculate each term a4x4, a3x ,a2x2,a1x, a0 . ]
For each value iin array a(i) do
sum ← sum * x + a[i]
endfor
7. Print sum
8. [Finished]
End
FLOWCHART:
start
Read n
false
for(i=0; i<=n; i++)
true
Read a[i]
Read x
Sum = a[i]*x
false
For(i=n-1; i>0; i--)
true
Sum = (sum+a[i]*x)
Sum = sum+a[0]
Write sum
stop
PROGRAM :
/* Evaluating the polynomial f(x) = a4x4+ a3x3+ a2x2+ a1x+ a0 using Horner’s method (n, i, sum, a[], x) */
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,sum=0,a[10],x;
clrscr();
printf("enter the number of co-efficients n>=0\n");
scanf("%d",&n);
printf("enter the n+1 co-efficients\n");
for(i=n;i>=0;i--)
{
printf("Enter a[%d] th coefficient : ",i);
scanf("%d",&a[i]);
}
printf("enter value of x\n");
scanf("%d",&x);
for(i=n;i>0;i--)
{
sum=sum*x+a[i];
}
sum=sum+a[0];
printf("the value of sum is %d",sum);
getch();
}
EXPECTED OUTPUT:
1. enter the number of co-efficients n>=0
4
enter the n+1 co-efficients
Enter a[4] th coefficient : 1
Enter a[3] th coefficient : 2
Enter a[2] th coefficient : 3
Enter a[1] th coefficient : 4
Enter a[0] th coefficient : 5
enter value of x
1
the value of sum is 15
SINE SERIES
𝑥 𝑥3 𝑥5
AIM: To compute Sin(x) using Taylor series approximation given by Sin(x)= 1! − + −.
3! 5!
ALGORITHM:
ALGORITHM: SINE SERIES[this algorithm takes degree as an input to print the sine function
value]
Input : Degree of polynomial
Step 1: start
X <- degree*(PI/180)
FLOWCHART:
start
Read degree
PI <- 3.142
X <- degree*(PI/180)
Nume <- x
Deno <- 1
Sum <- x
For i = 3,5,7,…to n
False true
nume –nume*x*x
True false
stop
PROGRAM:
/* Program to calculate sine value of given angle */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.142
int main()
{
int i, degree;
float x, sum=0,term,nume,deno;
clrscr();
printf("Enter the value of degree");
scanf("%d",°ree);
x = degree * (PI/180); //converting degree into radian
nume = x;
deno = 1;
i=2;
do
{ //calculating the sine value.
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
sum=sum+term;
i=i+2;
} while (fabs(term) >= 0.00001); // Accurate to 4 digits
printf("The sine of %d is %.3f\n", degree, sum);
printf("The sine function of %d is %.3f", degree, sin(x));
getch();
}
EXPECTED OUTPUT:
1. Enter the value of degree
0
The sine of 0 is 0.000
The sine function of 0 is 0.000
ALGORITHM :
ALGM: Bubble Sort [ This algorithm takes a list of unordered numbers and arrange them in
ascending order using Bubble Sort method]
Steps: 1. [Initialize] Start
2. [Input number of elements]
read n
3. [Input unsorted elements in array]
read elements in array a[ ]
4. print elements of array a[ ]
5. [Iterate array a[ ] in two loops. Outer loop gives number of passes. Inner loop does swap t ask.In
each pass, compare each pair of adjacent items. If former element is greater than latter one, swap
them.]
[Iterate array a[ ] with
for each value i in array a[i] to n do
for each value j in array a[j] to n-1 do
[Compare each pair of adjacent elements]
if (a[j] > a[j+1])then
[Swap these elements using temp variable]
temp ← a[j]
a[j] ← a[j+1]
a[j+1] ← temp
endif
endfor
endfor
6. Print array with sorted elements
7. [Finished] End.
FLOWCHART:
start
Read n
true
read a[i]
true
true
false
If
a[j]>a[j-1]
true
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
False
Write a[i]
stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,a[10],temp;
clrscr();
printf("Enter the no. of elements : \n");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("The original elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
for(i= 0 ; i < n-1 ; i++) // Number of Passes
{
for(j= 0 ; j< n-i+1; j++) // Comparisons
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("\n The Sorted elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
getch();
}
Output:
AIM: To read two matrices A(m x n )and B(p x q) and Compute the product A and B.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix\n");
scanf("%d %d",&p,&q);
if(n!=p)
printf(“Matrix multiplication is not possible”);
else
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of the second matrix\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]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n A- matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n B- matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("The product of two matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}
EXPECTED OUTPUT:
1. Enter the size of first matrix
2 3
Enter the size of second matrix
3 2
Enter the elements of first matrix
1 2 3 4 5 6
Enter the elements of the second matrix
1 2 3 4 5 6
A- matrix is
1 2 3
4 5 6
B- matrix is
1 2
3 4
5 6
The product of two matrix is
22 28
49 64
ALGORITHM:
1 for i = 1 to n + 1
2 do e[i, i - 1] = qi-1
3 w[i, i - 1] = qi-1
4 for l =1 to n
5 do for i = 1 to n - l + 1
6 do j = i + l - 1
7 e[i, j] = ∞
8 w[i, j] = w[i, j - 1] + pj + qj
9 for r = i to j
10 do t = e[i, r - 1] + e[r + 1, j] + w[i, j]
11 if t < e[i, j]
12 then e[i, j] = t
13 root[i, j] = r
14 return e and root
PROGRAM :
/* progrm to search an name using binary search */
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char name[10][20], key[20];
int n, i, low, high, mid, found=0;
clrscr();
printf("Enter the number of names to read, n=");
scanf("%d", &n);
printf("Enter the names in ascending order\n");
for(i=0;i<n;i++)
scanf("%s", name[i]);
printf("Enter the name to be search:");
scanf("%s", key);
low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low + high)/2;
if(strcmp(name[mid],key)==0)
found=1;
else if(strcmp(name[mid],key)<0)
low=mid+1;
else
high=mid-1;
}
if(found == 1)
printf("Name found in position : %d",mid+1);
else
printf("Name not found");
getch();
}
EXPECTED OUTPUT:
1. Enter the number of names to read, n= 5
Enter the names in ascending order
Amar
Chethan
Girish
Manoj
Yadu
Enter the name to be search:
Chethan
Name found in position :2
STRING COPY
AIM: TO execute a C program that Implements string copy operation STRCOPY(str1,str2) that
copies a string str1 to another string str2 without using library function.
ALGORITHM:
ALGM: EVAL_POLYNOMIAL [To copy string i/p to its o/p, replacing each
string of one or more blanks by a single blank].
Inputs: str1 and str2.
Output: str1 -> str2.
1.Start
2. Read the text in Array c
3. 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
FLOWCHART:
start
stop
Program :
AIM: To Read a sentence and prints frequency of each of the vowels and total count of
consonants.
ALGORITHM:
ALGM: VOWELS AND CONSONANTS COUNT[To read a sentence and prints frequency of
each of the vowels and total count of consonants.]
Input: Sentence.
Output: Print vowels(a,e,i,o,u) ,consonants(other than vowels) and print their count.
STEP 1.Set countCo:=0[C is the counter variable for consonants]
STEP 2.Set countVo:=0[counter for vowels ]
STEP 3.[find length of STR] Set L:=LENGTH(STR).
STEP 4:TOLOWER(STR,L) [change all characters in string to lower case]
STEP 5. Repeat for K:=0 to L-1:
If STR[K]='a' OR STR[K]='e' OR STR[K]='i' OR STR[K]='o' OR STR[K]='u'.Then:
Set countVo:=countVo+1
Else
Set countCo:=countCo+1
[End of If-Else]
[End of for loop]
STEP 6.PRINT countVo,countCo [display results]
STEP 7.EXIT/END
FLOWCHART: start
If is
alpha(s[i])
If it consists vowels otherwise
stop
PROGRAM :
/* program to prints frequency of vowels and total count of consonants */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[100],ch;
int i,vc=0,cc=0;
clrscr();
printf("Enter the sentence\n");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
{
ch=tolower(s[i]);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vc++;
else
cc++;
}
}
printf("No of vowels=%d\n",vc);
printf("No of consonants=%d",cc);
getch();
}
OUTPUT:
1. Enter the sentence
Welcome to drsmce
No of vowels=5
No of consonants=10
10. i)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.
ii) Design and develop a C function isprime(num) that accepts an integer argument
and returns 1
if the argument isprime, a 0 otherwise. Write a C program that invokes this function
to generate
prime numbers between the given ranges
Design and develop a C function Right Shift(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.
Algorithm:
ALGM: RIGHT ROTATE BY N POSITIONS [A ‘C’ function Right Shift(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.]
Flowchart:
start
Enter x and n
X=x>>1 X=x>>1
X=x+32768
stop
Program :
/* program to rotate right */
#include<stdio.h>
#include<conio.h>
#include<math.h>
//function to right rotate
unsignedint RightShift(unsignedint x,unsignedint n)
{
int i;
for(i=0;i<n;i++)
{
if(x%2==0)
x=x>>1;
else
{
x=x>>1;
x=x+32768;
}
}
return(x);
}
void main()
{
unsigned int x,y,n,i,value;
char input;
clrscr();
do
{
printf("\nEnter the number and the no of bits to be” “rotated\n");
scanf("%u %u",&x,&n);
y=x;
value=RightShift(x,n);
printf("\nCALULATED VALUE rightrot of %u,%u=%u",y,n,value);
printf("\nTocontinue press 'y' else press 'n'\n");
input=getche();
}while(input!='n');
getch();
}
Output:
10 ii). 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 must use plain
division checking approach to determine if a given number isprime.
Invoke this function from the main with different values obtained from
the user and print appropriate messages
Algorithm:
Complexity O(n)
Prime(num)
1 Set i=2
2 while i<=num/2
3 if num mod i = 0
4 print "Not a Prime number" and exit;
5 i=i+1
6 if (i==(num/2)+1)
7 Print "Prime number"
Flowchart:
start
True false
If(isprime(n))
stop
Program:
//Function to check the given number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter value of n\n");
scanf("%d",&n);
if(isprime(n));
printf("%d is prime number\n");
else
printf("%d is not a prime number\n") ;
getch();
}
return 0;
}
}
return 1;
}
Output:
1. Enter value of n
3
3 is prime number
2. Enter value of n
4
4 is not a prime number
Draw the flow chart and write a Recursive C function to find the factorial of 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 co-efficient nCr. Tabulate the results for different
values of n and r using suitable messages.
Algorithm:
Enter n, r
res = fact(n)/(fact(n-r)*fact(r))
T F
If n<=0
stop
Program :
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==0)
{
return 1;
}
return (n*fact(n-1));
}
int main()
{
int n,r,res;
clrscr();
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
res=fact(n)/(fact(n-r)*fact(r));
printf("The NCR is = %d",res);
getch();
}
Output:
1. Enter the value of n and r
4 2
The NCR is =6
2. Enter the value of n and
7 3
The NCR is =6
ALGORITHM:
step1:start
Step3: while(!feof((fp1)&&!feof(fp2))
Step4:Both files the not created the print the file not found
a). if only one file is created then print files not found.
b).if both files are created the print files are found.
Step5: stop
FLOWCHART:
start
While(!feof(fp1) &&
!feof(fp2))
Both the files are not Both files only one file is
stop
Program
/* Program on merge two files */
#include <stdio.h>
#incude<conio.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char usn[20], name[20];
clrscr();
fp1=fopen("studname.txt", "r");
if(fp1 == NULL)
printf(" File not found");
fp2=fopen("studusn.txt", "r");
if(fp2 == NULL)
printf(" File not found");
fp3=fopen("output.txt","w");
while( !feof(fp1) && !feof(fp2) )
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s",usn);
fprintf(fp3,"%15s %10s\n", name, usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("\n----------------------------\n");
printf(" Name USN \n");
printf("----------------------------\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s \n",usn);
printf("%-15s %10s \n", name,usn);
}
fclose(fp3);
getch();
}
Output:
Name USN
Amar 1RN10CS005
Suresh 1RN10CS001
Kiran 1RN10CS036
Shashi 1RN10CS072
ALGORITHM:
Step1: start
Then
Else
Step4: stop
FLOWCHART: start
Enter n
If(strcmp(s[i].name,
sname)==0)
stop
Program:
/* program to maintain a record of student using structrue */
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno, marks;
char name[20], grade;
};
Void main()
{
int i,n,found=0;
struct student s[10];
char sname[20];
clrscr();
printf("Enter the number of student details n=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the %d student details \n",i+1);
printf("enter the roll number:");
scanf("%d",&s[i].rollno);
Output:
Enter the number of student details n=3
100 vishwa 90 A
101 madhu 50 B
102 kiran 45 C
Write a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
Mean : is the average of the numbers. i.e the sum of a collection of numbers divided by the
number of numbers in the collection
Standard deviation (SD): measures the amount of variation or dispersion from the average. For
a finite set of numbers, the standard deviation is found by taking the square root of the average of
the squared differences of the values from their average value.
ALGORITHM:
ALGM: COMPUTATION OF SUM, MEAN AND STANDARD DEVIATION [to compute
the sum, mean and standard deviation of all elements stored in an array of n real numbers.]
Step 1:start
Sum=sum+*ptr;
Ptr++;
Mean=sum/n;
Ptr=a;
For(i=0;i<n;i++)
Sumstd=sumstd+pow((*ptr-mean),2);
Ptr++;
Step5:stop
FLOWCHART:
start
enter n
stop
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
clrscr();
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
Output:
Enter the no of elements
5
Enter the array elements
5 3 9 8 7
Sum=32.000
Mean=6.400
Standard deviation=2.154