CCP Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

VISVESVARAYA TECHNOLOGICAL UNIVERSITY 

“Jnana Sangama”,Belgaum-590018 
 
 

COMPUTER PROGRAMMING LABORATORY


(15CPL16/26)

LAB MANUAL

For

I/II SEMESTER
(COMMON TO ALL BRANCHES)

PREPARED BY

Mr. Srinivasa R​B.E., M.Tech.,(Ph.D)


Associate Professor, Dept. of CSE

DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

RAJARAJESWARICOLLEGE OF ENGINEERING
MYSORE ROAD, BANGALORE-74
(An ISO 9001:2008 Certified Institute)
(2011-2012)
Problem 1. Design and develop a flowchart or an algorithm that takes three coefficients
(a, b, and c) of a Quadratic equation (ax​2​+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.

Algorithm

Step 1: ​Start
Step 2: ​Read a, b, c.
Step 3: ​Compute disc
Step 4: ​Distinct roots
If disc>0
x1=(-b+ sqrt(disc))/(2*a);
x2=(-b- sqrt(disc))/(2*a);
Print roots
Step 5: ​Equal roots
If disc==0
x1=-b/(2*a);
x2=x1;
Print roots

Step 6: ​Otherwise complex roots


x1=-b/(2*a);
x2=sqrt (fabs (disc))/(2*a);
Print roots

Step 7: ​Stop.

Program: -​/* To find the roots of a given quadratic equation */

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a,b,c,x1,x2,disc;
clrscr();
printf("\n Enter the co-efficients:\n");
printf("\n Enter the value for a:");
scanf("%f", &a);
printf("\n Enter the value for b:");
scanf("%f", &b);
printf("\n Enter the value for c:");
scanf("%f", &c);
disc =b*b-4*a*c;
if(disc>0) /* To find the distinct roots */
{
x1=(-b+ sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\n The roots are distinct:\nx1=%f\nx2=%f",x1,x2);
}
else if(disc==0) /* To find the equal roots */
{
x1=-b/(2*a);
x2=x1;
printf("\n The roots are equal:\nx1=%f\nx2=%f\n",x1,x2);
}
else /* To find the complex roots */
{
x1=-b/(2*a);
x2=sqrt (fabs (disc))/(2*a);
printf("\n The roots are complex:\n");
printf("\n First root = %f+i%f\n",x1,x2);
printf("\n Second root = %f-i%f\n",x1,x2);
}
getch();
}

Output 1
Enter the co-efficients :

Enter the vale for a:1


Enter the vale for b:2
Enter the vale for c:3

The roots are complex:


First root = -1.000000+i1.414214
Second root = -1.000000-i1.414214

Output 2 Output 3
Enter the co-efficients: Enter the co-efficients:

Enter the vale for a:-1 Enter the vale for a:1
Enter the vale for b:1 Enter the vale for b:2
Enter the vale for c:2 Enter the vale for c:1
The roots are distinct: The roots are equal:
x1=-1.000000 x1=-1.000000
x2=-1.000000 x2=2.000000
Problem 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

Algorithm​: To reverse a four digit number and check whether it is a palindrome or not.
Step 1:​Start
Step 2:​Read the four digit number, n
Step 3:​Keep n in a temporary variable to verify with its reverse later and decide whether it is a
palindrome or not temp=n
Step 4:​Initialize rev to zero.
Step 5:​Output the four digit number, n
Step 6:​Check if n is not equal to zero. If true, repeat step 7 to step 9 otherwise (i.e if n is equal to
zero),goto step 10.
Step 7:​Compute digit = n %10.
Step 8:​Compute rev =rev *10+digit
Step 9:​Compute n =n/10.goto step6
Step 10:​Output reverse.
Step 11:​Check if reverse is equal to num. If true, output the message “the number is a
palindrome”.
Step 12:​stop.

Program : 2 ​/* Reversing a number and to check whether it is a palindrome or not */

#include <stdio.h>
#include <conio.h>
void main()
{
int n,temp,digit,rev ;
clrscr();
printf("\n Enter the four digit integer number:");
scanf("%d", &n);
temp=n;
rev=0;
printf(“the given integer number = %d\n”, n);
while (n !=0)
{
digit=n%10;
rev =rev*10+digit;
n = n/10;
}
printf(“The reverse of given number %d is %d\n”,temp,rev);
if(rev== temp)
printf("\n The given number %d is a palindrome\n", m);
else
printf("\n The given number %d is not a palindrome\n", m);
getch();
}

Output 1:

Enter the four digit integer number 2442

The given integer number = 2442

The reverse of given number 2442 is 2442

The given number 2442 is a palindrome.

Output 2:

Enter the four digit integer number 2014

The given integer number = 2014

The reverse of given number 2014 is 4102

The given number 2014 is not a palindrome.


Program 3: (A). Design and develop a flow chart 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 sqrt (n).

Flowchart

Start

False

True
Stop

Algorithm

Step 1: ​Start
Step 2: ​Read the number, num.
Step 3: ​Keep number divided by 2 in a variable sqrt, sqrt=num/2.
Step 4: ​Initialize temporary variable, temp to zero.
Step 5: ​Check if sqrt is not equal to temp. If true, repeat step 6 and step 7. Otherwise (i.e if sqrt
is equal to temp), go to step 8.
Step 6: ​Shift the value of sqrt to temp.
Step 7: ​Compute sqrt = (num/sqrt+sqrt)/2;
Step 8: ​Output num and sqrt with the statement” the Square Root of num is sqrt”,
Step 9: ​Stop.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
float num, sqrt, temp;
printf(“ Enter the number to find it’s square root\n”);
scanf(“%f”,& num);
sqrt=num/2;
temp=0;
while(sqrt!=temp)
{
temp=sqrt;
sqrt=(num/ sqrt+sqrt)/2;
}
Printf (“the square root of %f is: %f\n”, num, sqrt);
}

Output
First run:
Enter the number to find its square root
9
The square root of 9.000000 is 3.00000000

Second run:
Enter the number to find its square root
7
The square root of 7 .00000000 is 2.645751

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

Algorithm
Step 1:​ Start
Step 2:​ Read year to check if it is leap year, year
Step 3:​ Check if year is divisible by 400 or if year is not divisible by 100 and is divisible by 4, if
true go to step 4 else go to step 5.
Step 4:​ Output the message “this is a leap year”.
Step 5:​ Output the message” this is not a leap year”
Step 6:​ stop
Program

#include<stdio.h>
void main()
{
int year;
printf(“enter a year to check if it is a leap year\n”);
scanf(“%d”,& year);
if(year%400==0 | | (year%100 != 0 && year%4 == 0))
{
printf(“\n year %d is a leap year\n”, year);
}
else
{
printf(“\n year %d is not a leap year\n”, year);
}
}
Output

Enter a year to check if it is a leap year


2012
Year 2012 is a leap year

Enter a year to check if it is a leap year


2014
Year 2014 is not a leap year

Program 4. Design, develop an algorithm and execute a program in C to evaluate the given
polynomial f(x) = a​4​x​4 + a​3​x​3 + a​2​x​2 + a​1​x​1 + a​0 for given value of x and the coefficients using
Horner’s method​.

Algorithm:​ To evaluate the polynomial using Horner’s method.

Step 1: ​Start
Step 2: ​Read the order of the polynomial (i.e.. n).
Step 3: ​Read (n+1) co-efficients of the polynomial and store in an array
Step 4: ​Read the value of x.
Step 5: ​Initialize sum (i.e. f(x)) to a[n]*x.
Step 6: ​Initialize index i to n-1.
Step 7: ​Check if its greater than 0.if true,then goto step 8.Otherwise,goto step9.
Step 8: ​compute sum.sum= (sum+a[i])*x.Decrement i and goto step 7.
Step 9: ​compute sum.sum= sum+a[0].
Step 10: ​output sum (i.e. f(x)).
Step 11:​ stop

Program​/* to evaluate the given polynomial f(x) = a​4​x​4​ + a​3​x​3​ + a​2​x​2​ + a​1​x​1​ + a​0​*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
double a [30],x,sum;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",& n);
printf("\nEnter n + 1 values:\n");
for(i=0;i<=n;i++)
{
scanf("%lf",& a[i]);
}
printf("\nEnter the value of x:");
scanf("%lf",& x);
sum=a[n]*x; /* Evaluate the polynomial */
for(i=n-1;i>=1;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("\nEvaluated result = %lf", sum);
getch();
}

Output

Enter the value of n: 5


Enter n + 1 values: 1 2 3 4 5 6
Enter the value of x: 1
Evaluated result = 21.000000

Program 5. Draw the flowchart and writ a c program to compute sin(x) using Taylor
series approximation given by sin(x) =x-(x3/3!)+ (x5/5!)- (x7/7!)+……..
Compare your result with the built – in library function. Print both the result with
appropriate messages.

Flowchart

Start

false
true

Stop

false

Program

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n,i;
float sum=0 ,rad ;
clrscr();
printf("enter the degree and number of terms\n");
scanf("%d %d",& x,& n);
rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if((i-1)%4==0)
sum =sum+pow(rad,i)/fact(i);
else
sum =sum-pow(rad,i)/fact(i);
}
printf("Without using library function sin(x)=%f\n", sum);
printf("With library sin(x)=%f\n",sin(rad));
getch();
}
int fact(int x)
{
if(x==1||x==2)
return x;
else
return (x*fact(x-1));
}

Output

Enter the degree and number of terms


46 4
Without using library function sin(x) =0.72
With library sin(x) =0.72

Problem 6.Design, develop and execute a program in C to input N integer numbers into a
single dimensional array sort them in ascending order using bubble sort technique and
print both the given array and the sorted array with suitable headings.

Algorithm:​ To sort the array of integers in ascending order using bubble sort technique.

Step 1: ​start
Step 2: ​read the number of elements into an array (i.e.. n).
Step 3:​ Initialize index I to zero.
Step 4: ​Check if I is less than n (total number of elements in array).if true, then goto step5.
Otherwise, goto step 6.
Step 5: ​Read the i​th​ term in the array, a[i].increment I and goto step 4.
Step 6: ​output the unsorted array elements.
Step 7: ​Initialize index I to zero.
Step 8: ​Check if I is less than n (total number of elements in the array).If true,then goto step
9.otherwise,goto step 10.
Step 9​: output the i​th​ item in the array,a[i].Increment I and goto step 8.
Step 10: ​Initialize j to 1.
Step 11:​check if is less than n.if true,goto step 12.otherwise,goto step 15.
Step 12:​initialize index I to zero.
Step 13:​check if I is less than(n-pass)(i.e. there are(n-pass)comparisons to be done in each
pass).if true,goto step 14.otherwise,increment pass and goto step 11.
Step 14:​check if a[i] is greater than a[i+1](i.e 2 adjacent elements are compared).If true,then
swap the two elements using temporary variable.otherwise,goto step 13.
Step 15:​Output the sorted array elements.
Step 16:​Initialize index I to zero
Step 17:​Check if I is less than n(total number of elements in the array).if true,then goto​ step
18.otherwise,goto step 19.
Step 18:​output the ith term in the array,a[i].increment I and goto step 17.
Step 19:​stop.

Program:​/* Sorting n numbers in ascending order using bubble sort */

#include <stdio.h> /* including header file */


#include <conio.h>
void main()
{
int n, i, pass ,temp ,a[20]; /* Declaring variables*/
clrscr(); /*Clearing the Screen */
printf("\nEnter the size of an array\n");
scanf("%d",&n);
printf("\nEnter the array element to be sorted:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]); /* Reading elements of array */
printf("\n Unsorted array element are: \n");
for(i =0; i<n; i++)
{
printf("a[%d] = %d\t",i, a[i]); /* Printing elements of array */
}
for(pass=1;pass<n; pass++) /* This loop performs required number of passes */
{
for(i=0; i<n-pass; i++) /* This loop performs all comparisons in a pass */
{
if ( a[i] > a[i+1] )
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("\nSorted elements are:\n");
for(i =0; i<n; i++)
{
printf("a[%d]=%d\t", i , a[i]); /* Printing sorted array */

}
getch();
}

Output

Enter the number of items: 5

Enter the 5 numbers one by one:


23 0 45 -9 12
Elements of the array before sorting are:
23 0 45 -9 12

Sorted elements are:


-9 0 12 23 45
Problem 7.Design, develop and execute a program in C to read two matrices A (M x N) and
B (P x Q) and compute the product of A and B if the matrices are compatible for
multiplication. The program must 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)

Algorithm:​ To compute product of two matrices.

Step 1:​Start
Step 2:​Read the size of the two matrices m,.n,p,q.
Step 3:​check if n is not equal to p.if true,goto step 4.otherwise,goto step 5
Step 4:​print the message matrix multiplication not possible.goto step 15.
Step 5:​Read the elements into matrix A(i.e.. a[3][3])and B(i.e.. b[3][3])and also print them.
Step 6:​Initialize index row to 0.
Step 7:​check if row is less than m.if true,then goto step 8.otherwise,goto step 14.
Step 8:​initialize index col to 0.
Step 9​: checkif col is less than q.if true,then goto step 10.otherwise,goto step 7.
Step 10:​initialize c[row][col] to 0
Step 11:​Initialize index k to 0.
Step 12:​check if k is less than n.if true,then goto step 13.otherwise,goto step 9.
Step 13:​compute c[row][col]=c[row][col]+a[row][k]*b[k][col].increment k and goto step 12.
Step 14:​print the matrix c(i.e.. c[3][3])
Step 15:​stop

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int row,col,m,n,p,q,k;
clrscr();
printf("Enter the order of first matrix: ");
scanf("%d%d",&m,&n);
printf("\nEnter the order of second matrix: ");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("\n Order mismatch");
exit(0);
}
else
printf("\nEnter the elements for matrix A:\n");
for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
scanf("%d",&a[row][col]);
}
}
printf("\nEnter the elements for matrix B:\n");
for(row=0;row<p;row++)
{
for(col=0;col<q;col++)
{
scanf("%d",&b[row][col]);
}
}
printf("\n The elements of matrix A are:\n");
for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
printf("%3d",a[row][col]);
}
printf("\n\n");
}
printf("\n The elements of matrix B are:\n\n");
for(row=0;row<p;row++)
{
for(col=0;col<q;col++)
{
printf("%3d",b[row][col]);
}
printf("\n\n");
}
for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
c[row][col]=0;
for(k=0;k<n;k++)
{
c[row][col]+=a[row][k]*b[k][col];
}
}
}
printf("\n Product of two matrix is :\n\n");
for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
printf("%3d",c[row][col]);
}
printf("\n\n");
}
getch();
}
Output 1
Enter the order of first matrix: 2 3

Enter the order of second matrix: 2 2

Order mismatch
Output 2

Enter the order of first matrix: 2 2

Enter the order of second matrix: 2 2

Enter the elements for first matrix:


5
0
3
7
Enter the elements for second matrix:
1
9
3
2
Given first matrix is:
5 0

3 7

Given second matrix is:

1 9

3 2

Product of two matrix is:

5 45

24 41

Output 3

Enter the order of first matrix: 2 3

Enter the order of second matrix: 3 1

Enter the elements for first matrix:


3 5 1 2 6 8
Enter the elements for second matrix:
1 9 3
Given first matrix is:
3 5 1

2 6 8
Given second matrix is:
1
9
3
Product of two matrix is:
51
80

Problem 8​. ​Develop, implement and execute a C program to search a Name in a list
of names using Binary searching Technique.

Algorithm

Step 1: ​Start
Step 2: ​Read the number of names n.
Step 3: ​Read the names in ascending order and store in array
Step 4: ​Read the name to be searched.
Step 5: ​Compute low=0 and high=n-1
Step 6: ​Check if low<=high. If true go to step 7.
Otherwise go to 13
Step 7: ​Compute mid=low+high/2
Step 8: ​Check if strcmp(key,a[mid]) is equal to zero.
If true, goto step 9.otherwise goto step10.
Step 9​: Output the message “key found at the position”
Step 10: ​Check if strcmp (key, a [mid]) is greater than zero.
If true, go to step 11. Otherwise go to step 12.
Step 11: ​Compute high=high and low= mid+1. Then go to step 12
Step 12: ​Compute low=low and high=mid-1. Then go to step 6.
Step 13: ​Output the message “Name not found”
Step 14: ​Stop

Program​: /* Searching for a given element in an array using binary search method​. */

#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i,n,low,high,mid;
char a[50][50],key[20];
clrscr();
printf("enter the number of names\n");
scanf("%d",&n);
printf("enter the names in ascending order\n");
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
}
printf("enter the name to be searched \n");
scanf("%s",key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(key,a[mid])==0)
{
printf("key found at the position %d\n",mid+1);
getch();
exit(0);
}
else if(strcmp(key,a[mid])>0)
{
high=high;
low=mid+1;
}
else
{
low=low;
high=mid-1;
}
}
printf("name not found\n");
getch();
}
Output 1: Output 2:
Enter the number of names Enter the number of names
5 5
Enter the names in ascending order Enter the names in ascending order
Dhoni Dravid Ganguly Sachin Virat Dhoni Dravid Ganguly Sachin Virat
Enter name to be searched Enter name to be searched
Pujara Sachin
Name not found Key found at the position 4

Program 9: 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. Read a sentence and print frequency of vowels and total count of consonants.

Algorithm:

Step 1: ​Start
Step 2: ​Read the string s.
Step 3: ​call the function my_strcpy
Step 4: ​Print s2.
Step 5: ​stop.

Program i

#include<conio.h>
#include<stdio.h>
void my_strcpy (char s1[50],char s2[50]);
void main()
{
char str1[50],str2[50];
clrscr();
printf("enter a string:\n");
gets(str1);
my_strcpy(str1,str2);
printf("Destination string =%s\n”,str2);
getch();
}
void my_strcpy(char s1[50],char s2[50])
{
int i=0;
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
return;
}

Output
Enter a string
RRCE
String generated after copy is
RRCE

9. ii. Algorithm

Step 1: ​Start
Step 2: ​Read the string str
Step 3: ​checking each character by isalpha() function
Step 4: ​Print number of vowels and consonants.
Step 5: ​Stop

Program ii
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],ch;
int i,vowel_count=0,cons_count=0;
clrscr();
printf("enter a sentence:\n");
gets(str);
for(i=0;i<strlen(str);i++)
{
if(isalpha(str[i]))
{
ch=tolower(str[i]);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vowel_count++;
else
cons_count++;
}
}
printf("no of vowels in the given string is %d\n",vowel_count);
printf("no of consonants in the given string is %d\n",cons_count);
getch();
}

Output
Enter a sentence
Sachin is master blaster
no of vowels in the given string is 7
no of consonants in the given string is 14

Program10.
A. Design and develop 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.

Algorithm

Step 1: ​Start
Step 2: ​Read x.
Step 3: ​Read n.
Step 4: ​for i←0 to n in step of 1
If x is even number
x=x>>1
else x←x>>1, x+=32768
endif
Step 5: ​Print x
Step 6: ​Stop

Program

#include<stdio.h>
#include<conio.h>
Unsigned int right_rotate(unsigned int x, int n);

void main()
{
unsigned int x;
unsigned int res;
int n;
clrscr();
printf("Enter an unsigned integer<=65535\n");
scanf("%u",&x);
printf("Rotate %u how many times:",x);
scanf("%d",&n);
res=right_rotate(x,n);
printf("right_rotate(%u,%d)=%u\n",x,n,res);
getch();
}

unsigned int right_rotate(unsigned int x,int n)


{
int i;

for(i=1;i<=n;i++)
{
if(x%2==0)
x=x>>1;
else
x=x>>1,x+=32768;
}
return x;
}

Output 1:Output 2:
Enter an unsigned integer<=65535 Enter an unsigned integer<=65535
8 32768
Rotate 8 how many times:4 Rotate 32768 how many times:15
rightrot(8,4)=32768 rightrot(32768,15)=1

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

Step 1: ​Start
Step 2: ​Initialize flag to zero.
Step 3: ​Read the range.
Step 4: ​Check the range.
Step 5: ​Call the function isprime.
Step 6: ​Check if the flag is equal to zero, if true
print “there are no prime numbers between this range”
Step 7: ​Stop
/* for isprime*/
Step 1: ​Start
Step 2​: Initialize i.
Step 3: ​Check if num==0 or num==1 if true return 0.
Step 4: ​for(i=2;i<=num-1;i++)
{
if(num%i==0)
return(0);
}
Return 1
Step 5: S​top
Program
#include<stdio.h>
#include<conio.h>
int isprime(int n)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
{
return 0;
}
}
return 1;
}
void main()
{
int i,n1,n2,flag=0;
clrscr();
printf("enter two numbers(range)\n");
scanf("%d %d",&n1,&n2);
printf(“primes between %d to %d \n”,n1,n2);
for(i=n1;i<=n2;i++)
{
if(isprime(i))
{
printf("%d\t",i);
flag=1;
}
}
if(flag==0)
{
printf("there are no prime numbers between this range\n");
}
getch();
}

Output 1

Enter two numbers (range)


4
4
There are no prime numbers between this range.

Output 2

Enter two numbers (range)


1
10
2 3 5 7

Program.11Draw 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 tabulate the results for
different values of n and r with suitable messages.

Flowchart
Start

false

true

Stop
Program

#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==0)
{
return 1;
}
return(n*fact(n-1));
}
void main()
{
int n,r,result;
clrscr();
printf("enter the value of n and r\n");
scanf("%d%d",&n,&r);
result=fact(n)/(fact(n-r)*fact(r));
printf("the ncr value is %d",result);
getch();
}

Output:1

Enter the value of n and r


5
5
The NCR is 1

Output:2

Enter the value of n and r


6
3
The NCR is 20

Program 12
Given two university information files “studentname.txt”and “usntxt”that contains
students name and usn respectively.write a cprogram 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
………. …….
………. …….

Algorithm
Step 1: S​tart
Step2: ​Declare file descriptions FILE *fp1,*fp2,*fp3
Step 3: ​Open the files studentname.txt and usn.txt in read mode
And open output.txt in write mode
Step 4: ​if error occurs while open any file then
Printf(“error occurs while openingstudentname.txt and usn.txt\n”)
Step 5: ​Reada char by char from usn.txt file in to output.txt till end of file
Step 6:​start reading char by char from studentname.txt in to output.txt till end of file
Step 7:​if not end of file go to step4 else step7
Step 8:​Display the output
Step 9: ​Stop

Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp1, *fp2, *fp3;
char usn[20], name[20];
clrscr();
fp1=fopen("studname.txt","r");
if(fp1==NULL)
{
printf("studname.txt not found \n");
exit(0);
}
fp2=fopen("studusn.txt ","r");
if(fp2==NULL)
{
printf("studusn.txt not found\n");
exit(0);
}
fp3=fopen("outputr.txt","w");
if(fp3==NULL)
{
printf("outputr.txt not found\n");
exit(0);
}
while(!feof(fp1)&&!feof(fp2))
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s",usn);
fprintf(fp3,"\n%s %s",name,usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("outputr.txt","r");
printf("\n_____________________________________\n");
printf("NAME__________________USN\n");
printf("\n_____________________________________\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s",usn);
printf("%-15s %10s\n",name,usn);
}
fclose(fp3);
getch();
}

Output
________________________________________
NAME USN
________________________________________
Dhoni 11RR14EE01
Rahul 11RR14EE02
Program 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, give the student name as input.
Algorithm
Step 1:​Start
Step 2: ​Read n where n is number if students​.
Step 3:​Read students details (Roll number, name, marks, and grade) one by one
Step 4:​Read student name and print details if the record exists.
Step 5:​Stop.
/*C program to maintain a record of n student details */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int rollno, marks;
char name[20], grade[1];
};
void main()
{
int i, n, found=0;
char sname[20];
struct student s[10];
clrscr();
printf("enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the %d student details\n",i+1);
printf("enter the roll numbers:\n");
scanf("%d",&s[i].rollno);
printf("enter the student name:\n");
scanf("%s",s[i].name);
printf("enter the marks:\n");
scanf("%d",&s[i].marks);
printf("enter the grade:\n");
scanf("%s",s[i].grade);
}
printf("\n student details rae\n");
printf("\n rollno\t name\t\t\t marks\t grade\n");
for(i=0;i<n;i++)
printf("%d\t %s\t\t\t %d\t %s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
printf("\n enter the student name to print the marks:\n");
scanf("%s",sname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,sname)==0)
{
printf("\n marks of the student is %d\n",s[i].marks);
found=1;
}
}
if(found==0)
printf("student name not found\n");
getch();
}
Output 1
Enter the number of students
2
Enter the 1 student details
Enter the roll no :1rr10cs001
Enter the student name: Ramu
Enter the marks:75
Enter the grade:A
Enter the 2 student details
Enter the roll no : 1rr10cs002
Enter the student name: Sita
Enter the marks: 80
Enter the grade: A
Student details are
Rollno Name Marks Grade
1rr10cs001 Ramu 75 A
1rr10cs002 Sita 80 A
Enter the student name to print the marks:
Sita
Marks of the student is: 80
Output 2
Enter the number of students
2
Enter the 1 student details
Enter the roll no
1rr10cs001
Enter the student name
Ramu
Enter the marks
75
Enter the grade
A
Enter the 2 student details
Enter the roll no
1rr10cs002
Enter the student name
Sita
Enter the marks
80
Enter the grade
A
Student details are
Rollno Name Marks Grade
1rr10cs001 Ramu 75 A
1rr10cs002 Sita 80 A
Enter the student name to print the marks:
Gita
Student name not found
Program 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.
Algorithm
Step 1: ​Start
Step 2: ​Read value of n where n is the number of term up to which sum, mean and standard
deviation of all elements stored in an array of n real numbers.
Step 3: ​Read the number one by one in an array
Step 4: ​Assign the array address to the pointer for manipulation
Step 5: ​Using pointer access the array elements and find the sum
Step 6: ​Print the sum
Step 7: ​Find the average.
Step 8: ​Print average
Step 9: ​Find the standard deviation
Step 10: ​Print standard deviation
Step 11​: Stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float x[20],sum,mean;
float variance,deviation;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter n real values\n");
for(i=0;i<n;i++)
{
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+*(x+i);
}
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;
deviation=(float)sqrt(variance);
printf("mean(average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("deviation=%f\n",deviation);
getch();
}

Output
Please enter the values of n
5
Enter the n real values
15 25 80 40 20
Mean (Average) =36.0000
Variance = 554.000
Deviation= 23.537

You might also like