C Lab Record FINAL CS-IT-BCA To Print 2023 Batch
C Lab Record FINAL CS-IT-BCA To Print 2023 Batch
SCOLLEGEOFARTSANDSCIENCE
(Affiliated toBharathiarUniversity)
SIRUVANIMAINROAD,PERUR
COIMBATORE-641010
DEPARTMENTOFCOMPUTERSCIENCE
COMPUTERPRACTICALRECORDNOTE
SemesterI–Programming Lab–C
NAME :
REGNO :
DEGREE :
S.M.SCOLLEGEOFARTSANDSCIENCE
(AffiliatedtoBharathiarUniversity)
DEPARTMENTOFCOMPUTERSCIENCE
REGISTERNUMBER
Certifiedthatthisisabonafiderecordofworkdoneby
SubmittedforthepracticalexaminationheldatS.M.SCollegeofArts andScienceon
_________
STAFF-IN-CHARGE HEADOFTHEDEPARTMENT
INTERNALEXAMINER EXTERNALEXAMINER
CONTENTS
PAGEN
S.NO DATE NAMEOFTHEPROGRAM SIGNATURE
O
FIND THE SUM,
1 AVERAGE,VARIANCEANDS
TANDARDDEVIATION.
2 PRIMENUMBERS.
3 FIBONACCISERIES.
4 MAGICSQUARE.
5 ASCENDINGORDER.
6 PALINDROME.
7 NUMBEROFVOWELS.
8 FACTORIAL.
9 STUDENT’SMARKSHEET.
10 ADDTWOMATRICES.
11 COMPARE TWOFILES.
12 FILEHANDLING.
EX.NO:01 FINDTHE SUM,AVERAGE,
VARIANCEANDSTANDARDDEVIATION
DATE:
AIM:
ALGORITHM:
[1]
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[50];
int i,n;
float sum=0,avg,variance,stddev,d,varsum=0;
printf("\n Enter number of values\n");
scanf("%d",&n);
printf("\n Enter the values\n");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
sum=sum+x[i];
}
avg=sum/(float)n;
for(i=0;i<n;i++)
{
d=x[i]-avg;
varsum=varsum+pow(d,2);
}
variance=varsum/(float)n;
stddev=sqrt(variance);
printf("\n Sum =%.2f",sum);
printf("\n Average =%.2f",avg);
printf("\n Standard Deviation =%.2f",stddev);
getch();
}
[2]
[3]
OUTPUT
Sum = 205.00
Average =41.00
Standard Deviation = 25.38
RESULT:
[4]
EX.NO:02
PRIMENUMBERS
DATE:
AIM:
ALGORITHM:
[5]
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,n,count; clrscr();
printf("enter the range:"); scanf("%d",&n);
printf("the prime number %d:",n); for(num=1;num<=n;num++)
{
count=0; for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++; break;
}
}
if(count==0 && num!=1) printf("%d",num);
}
getch();
}
[6]
OUTPUT
RESULT:
[7]
EX.NO:03
FIBONACCISERIES
DATE:
AIM:
ALGORITHM:
[8]
PROGRAM:
#include <stdio.h>
int main()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ") ;
if(n==1)
printf("%d",t1);
else if(n>1)
printf("%d, %d, ", t1, t2);
else
printf("Enter number greater than 0");
for (i = 3; i <= n; i++) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
[9]
OUTPUT
Enter the number of terms: 6
Fibonacci Series: 0, 1, 1, 2, 3, 5,
RESULT:
[10]
EX.NO:04
MAGICSQUARE
DATE:
AIM:
ALGORITHM:
[11]
PROGRAM:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void magicSquare(int n)
{
int x[50][50];
int row = n / 2;
int col = n - 1;
int limit=n*n;
int i,j,num;
// set all slots as 0
memset(x, 0, sizeof(x));
for (num = 1; num <= limit;)
{
if (row == -1 && col == n) // 3rd condition
{
row = 0;
col = n - 2;
}
else {
// 1st condition
if (row < 0)
row = n - 1;
// 1st condition
if (col == n)
col = 0;
}
if (x[row][col]) // 2nd condition
{
row++;
col =col- 2;
[12]
continue;
}
else
{
x[row][col] = num; // set number
num++;
}
row--; // 1st condition
col++;
}
// Print magic square
printf("The Magic Square \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%3d ", x[i][j]);
printf("\n");
}
}
int main()
{
int n;
printf("Enter order of Magic Square:");
scanf("%d",&n);
if(n>3 && n%2!=0)
magicSquare(n);
else
printf("n value must be odd and greater than 3");
getch();
return 0;
}
[13]
OUTPUT
RESULT:
[14]
EX.NO:05
ASCENDINGORDER
DATE:
AIM:
ALGORITHM:
[15]
PROGRAM:
#include <stdio.h>
void main()
{
int i,j,temp,n,x[30];
printf("Enter total number of values:");
scanf("%d",&n);
printf("Enter the values:");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("Ascending order of given values are:\n");
for(i=0;i<n;i++)
printf("%d\t",x[i]);
}
[16]
OUTPUT
Enter total number of values:5
Enter the values:10
3
9
6
2
Ascending order of given values are:
2 3 6 9 10
RESULT:
[17]
EX.NO:06
PALINDROME
DATE:
AIM:
ALGORITHM:
[18]
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char str[1000];
char *ptr1, *ptr2;
printf("Enter the String:");
scanf("%s",str);
ptr1=str;
while(*ptr1!='\0')
{
++ptr1;
}
--ptr1;
for(ptr2=str;ptr1>=ptr2;)
{
if(*ptr1==*ptr2)
{
ptr1--;
ptr2++;
}
else
break;
}
if(ptr2>ptr1)
printf("String is Palindrome");
else
printf("String is not a Palindrome");
}
[19]
OUTPUT
RESULT:
[20]
EX.NO:07
NUMBEROFVOWELS
DATE:
AIM:
ALGORITHM:
[21]
PROGRAM:
#include<stdio.h>
void main()
{
char str[100];
int i,count=0;
printf("Enter the sentence to count vowels:");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'
||str[i]=='I'||str[i]=='O'||str[i]=='U')
{
count++;
}
}
printf("\nNumber of vowels in given sentence: %d\n",count);
}
OUTPUT
RESULT:
[22]
EX.NO:08
FACTORIAL
DATE:
AIM:
ALGORITHM:
[23]
PROGRAM:
#include<stdio.h>
long fact(int);
void main()
{
int num;
long fact_value;
printf("Enter a number to find factorial: ");
scanf("%d",&num);
fact_value=fact(num);
printf("Factorial of %d is %ld",num,fact_value);
}
long fact(int n)
{
if(n>=1)
return(n*fact(n-1));
else
return(1);
}
OUTPUT
Enter a number to find factorial: 5
Factorial of 5 is 120
RESULT:
[24]
EX.NO:09
STUDENT’SMARKSHEET
DATE:
AIM:
ALGORITHM:
[25]
PROGRAM:
#include<stdio.h>
struct student
{
char name[50];
char rollno[10];
int m1,m2,m3,m4,m5;
};
int main()
{
struct student s[50];
int i,n;
printf("Enter number of students:");
scanf("%d",&n);
printf("Enter Records of Students\n");
for(i=0;i<n;i++)
{
printf("\nStudent: %d\n",i+1);
printf("Enter Roll Number:");
scanf("%s",s[i].rollno);
printf("Enter Name:");
scanf("%s",s[i].name);
printf("Enter Mark 1:");
scanf("%d",&s[i].m1);
printf("Enter Mark 2:");
scanf("%d",&s[i].m2);
printf("Enter Mark 3:");
scanf("%d",&s[i].m3);
printf("Enter Mark 4:");
scanf("%d",&s[i].m4);
printf("Enter Mark 5:");
scanf("%d",&s[i].m5);
}
for(i=0;i<n;i++)
{ printf("\n\t========================================");
[26]
printf("\n\t\tSMS CAS\n");
printf("\t\t\t Coimbatore\n");
printf("\t==========================================\n");
printf("\t\tStatement of marks\n");
printf("Branch : CS\t\t\t Student Roll number:%s",s[i].rollno);
printf("\n===============================================================\n");
printf("Sub Code\t Subject\t Max Marks\t Passing Marks\t Awarded\t Result\n");
printf("================================================================\n");
printf("00S1\t\tSubject1\t100\t\t35\t\t%d",s[i].m1);
if(s[i].m1>=35)
printf("\t\tPass");
else
printf("\t\tFail");
printf("\n00S2\t\tSubject2\t100\t\t35\t\t%d",s[i].m2);
if(s[i].m2>=35)
printf("\t\tPass");
else
printf("\t\tFail");
printf("\n00S3\t\tSubject3\t100\t\t35\t\t%d",s[i].m3);
if(s[i].m3>=35)
printf("\t\tPass");
else
printf("\t\tFail");
printf("\n00S4\t\tSubject1\t100\t\t35\t\t%d",s[i].m4);
if(s[i].m4>=35)
printf("\t\tPass");
else
printf("\t\tFail");
printf("\n00S5\t\tSubject5\t100\t\t35\t\t%d",s[i].m5);
if(s[i].m5>=35)
printf("\t\tPass");
else
printf("\t\tFail");
printf("\n============================================================\n");
}
}
OUTPUT
[27]
Enter number of students:2
Enter Records of Students
Student: 1
Enter Roll Number:23CS001
Enter Name:RAJ
Enter Mark 1:56
Enter Mark 2:43
Enter Mark 3:32
Enter Mark 4:70
Enter Mark 5:85
Student: 2
Enter Roll Number:23CS002
Enter Name:MEENA
Enter Mark 1:92
Enter Mark 2:85
Enter Mark 3:64
Enter Mark 4:72
Enter Mark 5:56
========================================
SMS CAS
Coimbatore
==========================================
Statement of marks
Branch : CS Student Roll number:23CS001
===================================================================================
Sub Code Subject Max Marks Passing Marks Awarded Result
===================================================================================
00S1 Subject1 100 35 56 Pass
00S2 Subject2 100 35 43 Pass
00S3 Subject3 100 35 32 Fail
00S4 Subject1 100 35 70 Pass
00S5 Subject5 100 35 85 Pass
================================================================================
SMS CAS
Coimbatore
==========================================
Statement of marks
Branch : CS Student Roll number:23CS002
===================================================================================
Sub Code Subject Max Marks Passing Marks Awarded Result
===================================================================================
00S1 Subject1 100 35 92 Pass
00S2 Subject2 100 35 85 Pass
00S3 Subject3 100 35 64 Pass
00S4 Subject1 100 35 72 Pass
00S5 Subject5 100 35 56 Pass
================================================================================
RESULT:
[28]
EX.NO:10
ADD TWOMATRICES
DATE:
AIM:
ALGORITHM:
[29]
PROGRAM:
#include <stdio.h>
void matrixAdd(int m1[3][3], int m2[3][3], int res[3][3]);
int main()
{
int r=3,c=3;
int i,j;
int x[3][3], y[3][3], res[3][3];
// Input elements in first matrix
printf("Enter elements in first matrix of size %dx%d: \n", r, c);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
// (*(x + i) + j) is equal to &x[i][j]
scanf("%d", (*(x + i) + j));
}
}
// Input element in second matrix
printf("\nEnter elements in second matrix of size %dx%d: \n", r, c);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
// (*(y + i) + j) is equal to &y[i][j]
scanf("%d", (*(y + i) + j));
}
}
[30]
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
// *(*(res + i) + j) is equal to res[i][j]
printf("%d ", *(*(res + i) + j));
}
printf("\n");
}
return 0;
}
void matrixAdd(int x[3][3], int y[3][3], int res[3][3])
{
int i, j;
// Iterate over each matrix elements
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
// res[i][j] = x[i][j] + y[i][j]
*(*(res + i) + j) = *(*(x + i) + j) + *(*(y + i) + j);
}
}
}
[31]
OUTPUT
Enter elements in first matrix of size 3 * 3
1 23
456
789
RESULT:
[32]
EXNO:11
COMPARETWOFILES
DATE:
AIM:
ALGORITHM:
[33]
PROGRAM:
#include <stdio.h>
#include <string.h>
void comparefiles(char filename1[50],char filename2[50]);
int main()
{
char f1[50],f2[50];
printf("Enter 1st file name: ");
gets(f1);
printf("Enter 2nd file name: ");
gets(f2);
comparefiles(f1,f2);
return 0;
}
void comparefiles(char filename1[50],char filename2[50])
{
FILE* ptr1, *ptr2;
char ch1,ch2;
int flag=1;
// Opening file1 in reading mode
ptr1 = fopen(filename1, "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in reading mode
ptr2 = fopen(filename2, "r");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// Comparing two files.
do {
ch1 = fgetc(ptr1);
ch2 = fgetc(ptr2);
if(ch1!=ch2)
[34]
{
flag=0;
break;
}
} while (ch1 != EOF || ch2 != EOF);
// Closing the file
fclose(ptr1);
fclose(ptr2);
if(flag==1)
{
if(remove(filename2)==0)
printf("\nContents are same. Second File Deleted successfully");
else
printf("\nUnable to delete the file");
}
else
printf("\nFile Contents are not same");
}
OUTPUT
RESULT:
[35]
EXNO:12
FILEHANDLING
DATE:
AIM:
ALGORITHM:
[36]
PROGRAM:
#include <stdio.h>
int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
// Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in write mode
ptr2 = fopen("sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
letters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if(letters>0)
{
words++;
lines++;
[37]
}
fprintf(ptr2,"\nTotal Number of Characters:");
fprintf(ptr2,"%d",letters);
fprintf(ptr2,"\nTotal Number of Words:");
fprintf(ptr2,"%d",words);
fprintf(ptr2,"\nTotal Number of Lines:");
fprintf(ptr2,"%d",lines);
printf("Contents written in sample.txt file successfully");
// Closing the file
fclose(ptr1);
fclose(ptr2);
getch();
return 0;
}
OUTPUT
RESULT:
[38]