I Year C Programming Lab
I Year C Programming Lab
I Year C Programming Lab
CORE PRACTICAL
C PROGRAMMING - LAB
DECEMBER-2021
Register No : ______________
Class : _______________
CERTIFICATE
Date:
PAGE
S.NO DATE CONTENT SIGNATURE
NO
MATRIX MULTIPLICATION
4
6 BINARY SEARCH
8 PERMUTATION OF A STRING
11 STUDENT MARKSHEET
Aim:
To find the sum, average and standard deviation for a given set of numbers.
Algorithm:
Step 2: Get the number of values for which the sum, average and standard deviation is to
be calculated.
Step 3: Get the value one by one, add the values and store the result in sum.
#include<stdio.h>
#include<curses.h>
#include<math.h>
void main()
{
int i,x,n,sum=0,sum1=0;
float avg,var,std;
printf("\n\n\t CALCULATION OF SUM AVERAGE AND STANDARD
DEVIATION\n");
printf("\n Enter n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter x value:");
scanf("%d",&x);
sum=sum+x;
sum1=sum1+x*x;
}
avg=(float)sum/n;
var=(float)sum1/n-avg*avg;
std=(float)var;
printf("\n sum is %d",sum);
printf("\n average is %4.2f",avg);
printf("\n std.deviation is %4.2f",std);
}
Output
Enter n value:5
Enter x value:10
Enter x value:20
Enter x value:30
Enter x value:40
Enter x value:50
sum is 150
average is 30.00
std.deviation is 200.00
Result
Aim:
Algorithm:
Step 2: Get the decimal number which is to be converted to this equivalent binary
number.
#include<stdio.h>
#include<curses.h>
void main()
{
int dec,quo;
int bin [100],j,i=1;
printf("\n\n\t\t DECIMAL TO BINARY CONVERSION");
printf("\n\n\t\t ***********************************");
printf("\n Enter decimal value:");
scanf("%d",&dec);
quo=dec;
while(quo!=0)
{
bin[i++]=quo%2;
quo=quo/2;
}
printf("\n The binary value for %d is ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",bin[j]);
}
}
Output
Result
Aim:
Algorithm:
Step 4: Calculate the binomial coefficient value from n and r using the formula.
n!/r!(n-r)!
Step 5: The factorial values are calculated by calling the necessary functions.
#include<stdio.h>
long int factorial(int);
main()
{
long int n,r,ncr;
printf("\n\t\tBINOMIAL COEFFICIENT nCr USING FUNCTIONS");
printf("\n\t\t****************************************");
printf("\nEnter the value for n:");
scanf("%ld",&n);
printf("\n Enter the value for r:");
scanf("%ld",&r);
ncr=factorial(n)/(factorial(r)*factorial(n-r));
printf("\n The factorial of given number is :%ld\n",ncr);
}
long int factorial (int n)
{
long int fact ;
if((n==0)||(n==1))
return(1);
else
fact =n*factorial(n-1);
return(fact);
}
Output
Result
Aim:
Algorithm:
Step 5: Check whether the dimensions are valid for performing matrix multiplication.
Step 6: Check whether the dimensions are not valid. print the error message else if
Step 8: In display(), display the two matrices and result the matrices.
#include<stdio.h>
#include<curses.h>
int a[5][5],b[5][5],c[5][5],x[5][5];
int m,n,p,q,r,s,i,j,k,sum=0;
void read();
void mult();
void display();
void main()
{
printf("\n\t MATRIX MULTIPLICATION\n");
read();
}
void read()
{
printf("\nEnter the number of rows and columns for A matrix:");
scanf("%d%d",&m,&n);
printf("\nEnter the number of rows and columns for X matrix:");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("\nInvalid values for matrix multiplication\n");
}
else
{
printf("\nEnter the values for A matrix one by one\n");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the values for X matrix one by one\n");
for(i=1;i<=p;i++)
for(j=1;j<=q;j++)
scanf("%d",&x[i][j]);
mult();
}
}
void mult()
{
for(i=1;i<=m;i++)
for(j=1;j<=q;j++)
{
sum=0;
for(k=1;k<=p;k++)
{
sum=sum+a[i][k]*x[k][i];
}
c[i][j]=sum;
}
display();
}
void display()
{
printf("\nMATRIX A:\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n MATRIX X:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
printf("%d",x[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n EVALUTION OF AX MATRIX \n");
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
printf("%d",c[i][j]);
printf("\t");
}
printf("\n");
}
}
Output
MATRIX MULTIPLICATION
MATRIX A:
1 1
1 1
MATRIX X:
6 6
6 6
EVALUTION OF AX MATRIX
12 12
12 12
Result
Aim:
Algorithm:
Step 2: Get the value of n upto which the Fibonacci series of numbers have to be
displayed.
Step 3: If n<=0,print the message that shows n value must be greater than zero.
#include<stdio.h>
#include<curses.h>
int fibonacci(int);
void main()
{
int n,i;
printf ("***FIBONACCI SERIES\n");
printf("Enter the number of fibonacci numbers to be printed:");
scanf("%d",&n);
if (n==0)
{
printf("Enter n value greater than zero\n");
}
for(i=0;i<n;i++)
{
printf("%d\n",fibonacci(i));
}
}
int fibonacci(int n)
{
if (n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}
Output
***FIBONACCI SERIES***
Enter the number of fibonacci numbers to be printed:20
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
Result
Aim:
To write a program to implement binary search to find the a particular name from a list.
Algorithm:
Step 2: Declare the necessary variable get the total number of names to be in the list.
Step 4: Arrange the names in ascending using strcmp and strcpy functions.
Step 5: Get the name which has to be searched in the list of names.
Step 6: Using while loop and strcmp function search required name and calculate the
position.
Step 7: Print whether the required naming the list, print the result with the search names
position or else print that the name has not been in the list.
#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{
char name[50][15],dummy[15],sname[15];
int i,j,n,upp,low,mid,sb=1;
char choice;
printf("\n\t\t\tBINARY SEARCH");
printf("\n\t\t\t*************");
printf("\nEnter the number of names in the list:");
scanf("%d",&n);
printf("\n Enter the names one by one:\n");
for(i=0;i<n;i++)
scanf("%s[^\n]",name[i]);
printf("\n Enter the name to be search:\n");
scanf("%s[^\n]",sname);
printf("\nNames in the list:");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(strcmp(name[i],name[j])<0)
{
strcpy(dummy,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],dummy);
}
printf("\nSorted name list:");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
low=0;upp=n;
while(low<=upp)
{
mid=(low+upp)/2;
if(strcmp(sname,name[mid])==0)
{
sb=0;
printf("\n%s is found in position:%d\n",sname,mid);
break;
}
else if(strcmp(sname,name[mid])>0)
low=mid+1;
else if(strcmp(sname,name[mid])<0)
upp=mid-1;
}
if(sb==1)
printf("\n%s is not found in the list",sname);
}
Output
BINARY SEARCH
****************
Enter the number of names in the list:5
Result
The above program is successfully executed and verified.
Write a c program to count the number of vowels and consonants in a given line of test using
pointers.
Ex.No : 7 VOWELS AND CONSONANTS
Aim:
To write a program to count the number of vowels and consonants in a given line of text
using pointers.
Algorithm:
Step 5: Using if statement, count the number of vowels and consonants in a given line of
text.
#include<stdio.h>
#include<ctype.h>
#include<curses.h>
int main()
{
char str[100];
char*ptr;
int cntV,cntC,punt,space,digit;
printf("\n\t\t VOWELS AND CONSONANTS");
printf("\n\t\t *********************");
printf("\n Enter a line of text:");
scanf("%[^\n]",str);
ptr=str;
cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A'||*ptr=='E'||*ptr=='I'||*ptr=='O'||*ptr=='U'||*ptr=='a'||*ptr=='e'||*ptr=='i'||*ptr==
'o'||*ptr=='u')
cntV++;
else if(ispunct(*ptr))
punt++;
else if(isspace(*ptr))
space++;
else if(*ptr>='a'&&*ptr<='z'||(*ptr>='A'&&*ptr<='Z'))
cntC++;
else
digit++;
ptr++;
}
printf("\n Total number of VOWELS:%d,CONSONANTS:%d\n",cntV,cntC);
}
Output
Result
Aim:
Algorithm:
Step 3: Get the string for which the premutation home to be generation.
Step 6: In swap() function the letters are swaped to generate the premutation.
PERMUTATION OF A STRING
***************************
Enter a string: ABCD
Permutations:
ABCD
ABDC
ACBD
ACDB
ADCB
ADBC
BACD
BADC
BCAD
BCDA
BDCA
BDAC
CBAD
CBDA
CABD
CADB
CDAB
CDBA
DBCA
DBAC
DCBA
DCAB
DACB
DABC
Result
Write a program to check whether the person is eligible for the above given post
Ex.No : 9 PHYSICAL FITNESS CRITERIA
Aim:
To write a program to check whether the person is eligible for the given post.
Algorithm:
Step 2: Declare the variables to get age, height, weight, chest details in Integer data type
and sex in character data type.
Step 4: Using nested if-else statement check whether the eligible for the given post.
#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{
int age,ht,wt,c1,c2,c3;
char sex;
char name[20];
printf("\t Physical fitness criteria for the post of district office(fire and rescue)\n");
printf("Enter name:");
fgets(name,20,stdin);
printf("\nEnter candidate sex(M/F):");
scanf("%c",&sex);
if(sex=='M')
{
printf("Enter age:");
scanf("%d",&age);
printf("Enter height:");
scanf("%d",&ht);
printf("Enter weight:");
scanf("%d",&wt);
printf("Enter chest (full Inspiration):");
scanf("%d",&c1);
printf("Enter chest (normal)):");
scanf("%d",&c2);
printf("Enter chest (expansion):");
scanf("%d",&c3);
if((age>=21)&&(age<=32)&&(ht>=165)&&(wt>=56)&&(c1>=89)&&(c2>=84)&&(c3>
=85))
{
printf("Eligible for the post of District Officer(fire and rescue)\n");
}
else
{
printf("Not eligible for the post of District Officer(fire and rescue)\n");
}
}
else
{
printf("Enter valid sex for the candidate\n");
}
}
Output
Physical fitness criteria for the post of district office(fire and rescue)
Result
*********
* *
* *
* *
* *
* *
*********
Ex.No : 10 HALLOW SQUARE STAR PATTERN
Aim:
Algorithm:
#include<stdio.h>
#include<curses.h>
int main()
{
int i,j,n;
printf("\n\t\t Hallow Square Star Pattern");
printf("\n\t\t **********************");
printf("\n\t\t Enter number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output
******
* *
* *
* *
* *
******
Result
Aim:
To print the student mark sheet assuming register number, names and marks in five subjects
using structures.
Algorithm:
Step 3: Get the student details such as name,register no,dob,month and year of passing.
Step 4: Get the total number of students whose details are to be enter.
Step 6: Using for loop print the student details and also calculate student total marks.
Step 7: Using if statement check whether mark is greater than 40 if mark is greater than
#include<stdio.h>
#include<curses.h>
#include<string.h>
struct student
{
char regno[20];
char name[20];
char dob[15];
int marks[5];
int total;
}stu[20];
main()
{
int i,j,n,sno;
char result[5];
char myp[50];
char sub[50][50]={"Tamil","English","Maths","ctheory","clab"};
printf("\n\n\tSTUDENTS MARKSHEET");
printf("\n\n\t******************");
printf("\n Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter %d students details",i+1);
printf("\n Enter student name:");
scanf("%s",stu[i].name);
printf("\n Enter the student regno:");
scanf("%s",stu[i].regno);
printf("\n Enter the student DOB:");
scanf("%s",stu[i].dob);
printf("\nEnter the month and year of passing:");
scanf("%s[^/n]",myp);
printf("\nEnter the five subject marks:");
for(j=0;j<5;j++)
{
scanf("%d",&stu[i].marks[j]);
}
}
for(i=0;i<n;i++)
{
printf("\n\t\t VELLALAR COLLEGE FOR WOMEN(AUTONOMOUS),ERODE-12\n");
printf("\n\t\t\t I-B.SC Computer science(SF)'A'\n");
printf("\n\t\t NAME:%s\t\tDOB:%s\t",stu[i].name,stu[i].dob);
printf("\n\t\tREGNO:%s\t\tMONTH AND YEAR OF PASSING:%s",stu[i].regno,myp);
printf("\n\t\t____________________________________________________");
printf("\n\t\t s.no\t subject\t marks\t Result");
printf("\n\t\t\t__________________________________________________");
for(j=0;j<5;j++)
{
if (stu[i].marks[j]<40)
strcpy(result,"fail");
else
strcpy(result,"pass");
stu[i].total=stu[i].total+stu[i].marks[j];
printf("\n\t\t%d\t%s\t\t%d\t%s\t",j+1,sub[j],stu[i].marks[j],result);
}
printf("\n\t\t\t\tTOTAL:%d\n",stu[i].total);
}
}
Output
STUDENTS MARKSHEET
***********************
Enter the number of students:2
Enter 1 students details
Enter student name: ARTHI
Enter the student regno: 101
Enter the student DOB: 25/02/2000
Enter the month and year of passing: APR-2021
Enter the five subject marks: 75 65 85 90 80
TOTAL:395
VELLALAR COLLEGE FOR WOMEN(AUTONOMOUS),ERODE-12
NAME:BHARATHI DOB:11/09/2000
TOTAL :400
Result
NUCLEOTIDES IN A DNA
Aim:
Algorithm:
Step 3: Enter the length of DNA sequence initially and then get the DNA sequence
Step 4: Using for loop and else if ladder check whether each character is A|C|T|G.
Step 5: If any of the character is A|G|T|C replace it with T|C|A|G also count the
Step 6: Calculate the percentage of each character using the count value.
#include<stdio.h>
#include<curses.h>
void main()
{
int i,n;
printf("\n\t\t COMPLEMENTARY SEQUENCE AND PERCENTAGE OF
NUCLEOIDS IN A DNA \n");
printf("\n\n Enter the length of the sequence:\n");
scanf("%d",&n);
char seq[n],com[100]="";
int a=0,t=0,g=0,c=0;
float aper,tper,cper,gper;
printf("\n Enter sequence:");
scanf("%s",seq);
printf("\n The given sequence:%s",seq);
for(i=0;i<n;i++)
{
if(seq[i]=='A')
{
com[i]='T';
a=a+1;
}
else if (seq[i]=='T')
{
com[i]='A';
t=t+1;
}
else if (seq[i]=='G')
{
com[i]='C';
g=g+1;
}
else if(seq[i]=='C')
{
com[i]='G';
c=c+1;
}
}
printf("\n\n COMPLEMENTARY SEQUENCE IN A DNA:%s\n",com);
printf("\n PERCENTAGE OF NUCLEOTIDES IN A DNA");
aper=(float)a/n*100;
tper=(float)t/n*100;
cper=(float)c/n*100;
gper=(float)g/n*100;
printf("\n A:%5.2f \t T:%5.2f \t C:%5.2f \t G:%5.2f \n",aper,tper,cper,gper);
}
Output
Enter sequence:AATTCCGGAT
Result