0% found this document useful (0 votes)
7 views41 pages

C LAB CS For Every University Pattern in Ug

C Lab

Uploaded by

BALAKUMAR C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views41 pages

C LAB CS For Every University Pattern in Ug

C Lab

Uploaded by

BALAKUMAR C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Ex.

No
SUM, AVERAGE, STANDARD DEVIATION
Date

Aim
To write a C program to compute the Sum, Average, and Standard Deviation
of n numbers entered by the user.
Algorithm
Step 1 : Start
Step 2 : Declare necessary variables:
Integer variables for loop control (i, n).
Floating-point variables for storing input numbers (x[]), sum (s1), sum
of squares difference (s2), average (avg), variance (var), and standard
deviation (sd).
Step 3 : Read the value of n (number of elements).
Step 4 : Prompt the user to enter n numbers.
Step 5 : Initialize s1 = 0.
Step 6 : Using a loop from i = 0 to n-1:
Read each number into the array x[i].
Add it to s1.
Step 7 : Compute the average:
avg = s1 / n.
Step 8 : Initialize s2 = 0.
Step 9 : Using a loop from i = 0 to n-1:
Calculate (x[i] - avg) * (x[i] - avg) and add it to s2.
Step 10 : Compute the variance:
var = s2 / n.
Step 11 : Compute the standard deviation:
sd = sqrt(var).
Step 12 : Display the Sum, Average, and Standard Deviation.
Step 13 : Stop.

1
//SUM, AVERAGE, STANDARD DEVIATION

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float x[50],avg,sd,var,s1=0,s2=0;
clrscr();
printf("\nENTER THE N VALUE\n");
scanf("%d",&n);
printf("\nENTER %d NUMBERS ONE BY ONE\n",n);
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
s1=s1+x[i];
}
avg=s1/n;
for(i=1;i<n;i++)
{
s2=s2+(x[i]-avg)*(x[i]-avg);
}
var=s2/n;
sd=sqrt(var);
printf("\nTHE SUM =%5.2f",s1);
printf("\nTHE AVERAGE =%5.2f",avg);
printf("\nTHE STANDARD DEVIATION =%5.2f",sd);
getch();
}

2
//OUTPUT

3
Ex. No
N PRIME NUMBERS
Date

Aim
To write a C program to print all prime numbers up to a given limit n.

Algorithm
Step 1 : Start
Step 2 : Declare integer variables: i, j, k, and n.
Step 3 : Read the value of n (the upper limit for prime numbers).
Step 4 : Display a heading message: “PRIME NUMBERS”.
Step 5 : Repeat steps (6–9) for each number i from 1 to n:
o Set k = 1 (assume the number is prime).
o For each j from 2 to i-1:
 If i % j == 0 then
 Set k = 0 (number is not prime).
o End inner loop.
o If k is still 1, print the number i.
Step 6 : End outer loop.
Step 7 : Stop

4
// N PRIME NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,k,n;
clrscr();
printf("\n\t\t\tENTER THE LIMIT\n\t\t\t");
scanf("\n%d",&n);
printf("\n\t\t\tPRIME NUMBERS\n");
for(i=1;i<=n;i++)
{
k=1;
for(j=2;j<i;j++)
{
if(i%j==0)
k=0;
}
if(k)
printf("\t\t\t%d\n",i);
}
getch();
}

5
//OUTPUT

6
Ex. No
FIBANOCCI SERIES
Date

Aim
To write a C program to generate the Fibonacci series up to n terms.
Algorithm
Step 1 : Start
Step 2 : Declare integer variables: i, j, k, l, n.
Step 3 : Read the value of n (number of terms to be generated).
Step 4 : Display the heading: “FIBONACCI SERIES”.
Step 5 : Initialize:
a. j = -1
b. k = 1
Step 6 : Repeat the following steps for i = 1 to n:
a. Compute the next term: l = j + k.
b. Print l.
c. Update values:
i. j = k
ii. k = l
Step 7 : End loop.
Step 8 : Stop

7
//FIBANOCCI SERIES

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,n;
clrscr();
printf("\nENTER THE NUMBER OF TERMS\n");
scanf("\n%d", &n);
printf("\nFIBONACCI SERIES\n");
j=-1;
k=1;
for(i=1;i<=n;i++)
{
l=j+k;
printf("\n%d",l);
j=k;
k=l;
}
getch();
}

8
//OUTPUT

9
Ex. No
MAGIC SQUARE
Date

Aim
To write a C program to generate and display a Magic Square of a given odd
order using the Siamese method (rule-based placement of numbers).

Algorithm
Step 1 : Start
Step 2 : Define SIZE as the maximum size of the square (e.g., 20).
Step 3 : Declare variables:
o order → size of the magic square (must be odd).
o i, j, k, p, q, mid → control variables.
o magic[SIZE][SIZE] → 2D array to store the square.
Step 4 : Read the value of order from the user.
Step 5 : If order is even:
o Print an error message “ORDER MUST BE ODD”.
o Exit the program.
Step 6 : Initialize all elements of the magic matrix to 0.
Step 7 : Compute the middle column index: mid = order / 2.
Step 8 : Place the first number (1) at position (row = 0, col = mid).
Step 9 : For every next number i = 2 to order * order:
o Move one step upward (row--) and rightward (col++).
o If row goes out of bounds (row < 0), wrap it to row = order - 1.
o If column goes out of bounds (col >= order), wrap it to col = 0.
o If the new cell is already filled:
 Reset column to previous (col = q).
 Move row one step downward (row = p + 1).
o Place the current number i in the new position.
Step 10 : After filling all numbers, print the Magic Square in a formatted grid with
borders.
Step 11 : Stop.

10
11
// MAGIC SQUARE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 20
void main()
{
int order, i, j, k, p, q, mid, magic[SIZE][SIZE];
clrscr();
printf("ENTER ORDER TO MAGIC SQUARE");
scanf("%d",&order);
if(order%2==0)
{
printf("ORDER MUST BE ODD\n");
printf("EXITING....");
exit(0);
}
mid = order/2;
for(i=0;i< order; i++)
{
for(j=0; j< order; j++)
{
magic[i][j]=0;
}
}
k=mid;
j=0;
for(i=1; i<= order*order; i++)
{
magic[j][k] = i;
p = j--;
q = k++;
if(j< 0)
{
12
j = order-1;
}
if(k>order-1)
{
k=0;
}
if(magic[j][k] != 0)
{
k = q;
j = p+1;
}
}
printf("GENERATED MAGIC SQUARE IS:\n");
for(i=1;i<=6*order;i++)
{
printf("-");
}
printf("\n");
for(j=0;j< order;j++)
{
printf("|");
for(k=0; k< order; k++)
{
printf("%4d |", magic[j][k]);
}
printf("\n");
for(i=1; i<=6*order; i++)
{
printf("-");
}
printf("\n");
}
getch();
}
13
//OUTPUT

14
Ex. No
ASCENDING ORDER
Date

Aim
To write a C program to sort a list of numbers in ascending order using a simple
comparison-based sorting method.

Algorithm
Step 1 : Start
Step 2 : Declare an integer array a[100] and variables n, i, j, temp.
Step 3 : Read the number of elements n from the user.
Step 4 : Input n values into the array a.
Step 5 : Repeat steps (6–8) for each element in the array:
a. For i = 0 to n-1:
i. For j = 0 to n-1:
1. If a[j] > a[i], then swap:
a. temp = a[i]
b. a[i] = a[j]
c. a[j] = temp
Step 6 : After completing the loops, the array elements are arranged in
ascending order.
Step 7 : Print the sorted list.
Step 8 : Stop.

15
//ASCENDING ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,temp;
clrscr();
printf("ENTER THE NUMBER OF ELEMENTS \n");
scanf("%d",&n);
printf("ENTER THE VALUES \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nSORTING A VALUES ");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
getch();
}

16
//OUTPUT

17
Ex. No
PALINDROME CHECKING
Date

Aim
To write a C program to check whether a given string is a Palindrome using
pointer manipulation.
Algorithm
Step 1 : Start
Step 2 : Declare a character array str[30] and two character pointers p1 and p2.
Step 3 : Read a string from the user.
Step 4 : Initialize pointers:
o p1 = str + strlen(str) - 1 (points to last character).
o p2 = str (points to first character).
Step 5 : Repeat until p1 >= p2:
o If *p1 == *p2, then move the pointers:
 p1-- (move left).
 p2++ (move right).
o Else, break the loop (string is not palindrome).
Step 6 : After loop:
o If p2 > p1, then all characters matched → string is a Palindrome.
o Otherwise → string is Not a Palindrome.
Step 7 : Display the result.
Step 8 : Stop.

18
//PALINDROME CHECKING
#include <stdio.h>
#include <string.h>
void main()
{
char str[30],*p1,*p2;
clrscr();
printf("ENTER THE STRING \n");
gets(str);
p1=str+strlen(str);
p2=str;
p1--;
while(p1>=p2)
{
if(*p1==*p2)
{
p1--;
p2++;
}
else
{
break;
}
}
if(p2>p1)
{
printf("THE STRING -- %s -- IS PALINDROME",str);
}
else
{
printf("THE STRING -- %s -- IS NOT A PALINDROME",str);
}
getch();
}
19
//OUTPUT

20
Ex. No
COUNTING VOWELS
Date

Aim
To write a C program to count the number of vowels in a given string.
Algorithm
Step 1 : Start
Step 2 : Declare:
o An integer variable count = 0 to store the number of vowels.
o An integer variable i = 0 for traversal.
o A character array str[30] to store the input string.
Step 3 : Read a string from the user.
Step 4 : Traverse the string using a loop until the null character '\0' is reached.
o For each character str[i]:
 If it matches any of the uppercase vowels ('A', 'E', 'I', 'O', 'U'),
increment count.
o Increment i to check the next character.
Step 5 : After the loop, print the value of count.
Step 6 : Stop.

21
//COUNTING VOWELS

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count=0,i=0;
char str[30];
clrscr();
printf("ENTER A STRING ");
gets(str);
while(str[i]!='\0')
{
if((str[i]=='A')||(str[i]=='E')||(str[i]=='I')||(str[i]=='O')||(str[i]=='U'))
count=count+1;
i=i++;
}
printf("NUMBER OF VOWELS=%d",count);
getch();
}

22
//OUTPUT

23
Ex. No
FACTORIAL VALUE
Date

Aim
To write a C program to calculate the factorial of a given number using a user-
defined function.
Algorithm
Step 1 : Start
Step 2 : Declare integer variables m and n.
Step 3 : Read the value of n (the number whose factorial is to be calculated).
Step 4 : Call the function fact(n) and store the result in m.
Step 5 : Inside the function fact(a):
o Initialize f = 1.
o For i = 1 to a:
 Multiply f = f * i.
o Return f.
Step 6 : Print the factorial value stored in m.
Step 7 : Stop.

24
//FACTORIAL VALUE

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n;
clrscr();
printf("ENTER THE VALUE FOR N ");
scanf("%d",&n);
m=fact(n);
printf("FACTORIAL VALUE = %d ",m);
getch();
}
int fact(int a)
{
int f=1,i;
for(i=1;i<=a;i++)
f=f*i;
return f;
}

25
//OUTPUT

26
Ex. No
STUDENT MARK SHEET
Date

Aim
To write a C program using structures to read and display the marksheet of n
students including their name, roll number, marks in 5 subjects, total, and average.
Algorithm
Step 1 : Start
Step 2 : Define a structure stu with the following members:
a. int m1, m2, m3, m4, m5 → marks in 5 subjects
b. int total → total marks
c. float avg → average marks
d. int rollno → roll number
e. char name[25] → student name
Step 3 : Declare an array of structures stu[50] to hold details of up to 50
students.
Step 4 : Read the number of students n.
Step 5 : For each student i = 0 to n-1:
a. Input name and rollno.
b. Input marks for 5 subjects.
c. Compute total marks:
i. total = m1 + m2 + m3 + m4 + m5
d. Compute average marks:
i. avg = total / 5
Step 6 : After reading all details, display the marksheet for each student:
a. Print college/heading (GAC UDT).
b. Print student details: Name, Roll Number, 5 subject marks, Total, and
Average.
c. If it is not the last student, prompt “Press any key to go next
marksheet”.
d. If it is the last student, print “The End”.
Step 7 : Stop.

27
//STUDENT MARK SHEET

#include<stdio.h>
#include<conio.h>
void main()
{
struct
{
int m1,m2,m3,m4,m5,total,rollno;
float avg;
char name[25];
}stu[50];
int i,n;
clrscr();
printf("\nHOW MANY STUDENTS : ");
scanf("%d",&n);
printf("\n ENTER %d STUDENT DETAILS\n",n);
for(i=0;i<n;i++)
{
printf("\nENTER NAME : ");
scanf ("%s",stu[i].name);
printf("\nENTER ROLLNO : ");
scanf("%d",&stu[i].rollno);
printf("\nENTER 5 MARKS\n");
scanf("%d%d%d%d
%d",&stu[i].m1,&stu[i].m2,&stu[i].m3,&stu[i].m4,&stu[i].m5);
stu[i].total=stu[i].m1+stu[i].m2+stu[i].m3+stu[i].m4+stu[i].m5;
stu[i].avg=stu[i].total/5;
}
for(i=0;i<n;i++)
{
printf("\n\n GAC UDT");
printf("\n\n Name = %s \n\n rollno = %d",stu[i].name,stu[i].rollno);
printf("\n\n m1 m2 m3 m4 m5");
printf("\n %d %d %d %d
%d",stu[i].m1,stu[i].m2,stu[i].m3,stu[i].m4,stu[i].m5);

28
printf("\n\n TOTAL = %d \n\n AVERAGE = %f",stu[i].total,stu[i].avg);
if(i<n-1)
printf("\n\n\t\t PRESS ANY KEY TO GO NEXT MARKSHEET");
else
printf("\n\n\t\t THE END ");
getch();
}
getch();
}

29
//OUTPUT

30
Ex. No
MATRIX ADDITION
Date

Aim
To write a C program to perform the addition of two matrices using functions and
pointers.
Algorithm
Step 1 : Start
Step 2 : Declare three 2D arrays:
o a[10][10] → first matrix
o b[10][10] → second matrix
o c[10][10] → resultant matrix
Step 3 : Read the order of the matrix: m (rows) and n (columns).
Step 4 : Define a function read_array(m, n, mat) to input elements into a matrix
using pointers.
o Loop i = 0 to m-1, j = 0 to n-1
o Read element into mat[i][j].
Step 5 : Define a function print_array(m, n, mat) to print a matrix using pointers.
o Loop i = 0 to m-1, j = 0 to n-1
o Print each element mat[i][j].
Step 6 : Define a function add_matrix(m, n, a, b, c) to add two matrices using
pointers.
o Loop i = 0 to m-1, j = 0 to n-1
o Compute: c[i][j] = a[i][j] + b[i][j].
Step 7 : In main():
o Read matrix a.
o Read matrix b.
o Call add_matrix() to compute sum into c.
o Call print_array() to display the resultant matrix.
Step 8 : Stop.

31
//MATRIX ADDITION
#include<stdio.h>
#include<conio.h>
void read_array(int m, int n, int (*mat)[10])
{
int i, j;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", (*(mat + i) + j));
}
void print_array(int m, int n, int (*mat)[10])
{
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", (*(*(mat + i) + j)));
}
printf("\n");
}
}
void add_matrix(int m, int n, int (*a)[10], int (*b)[10], int (*c)[10])
{
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
(*(*(c + i) + j)) = (*(*(a + i) + j)) + (*(*(b + i) + j));
}
}
}
void main()
{
int a[10][10], b[10][10], c[10][10];

32
int m, n, i, j;
clrscr();
printf("ENTER THE SIZE OF ARRAY (3*3)\n");
scanf("%d %d", &m, &n);
printf("ENTER THE ELEMENTS OF MATRIX A \n", (m * n));
read_array(m, n, a);
printf("ENTER THE ELEMETS OF MATRIX B \n", (m * n));
read_array(m, n, b);
add_matrix(m, n, a, b, c);
printf("SUM OF TWO MATRIX IS\n");
print_array(m, n, c);
getch();
}

33
//OUTPUT

34
Ex. No
FILE CONTENT CHECKING
Date

Aim
To write a C program to compare two text files and check whether they are identical
or different.
Algorithm
Step 1 : Start
Step 2 : Declare:
a. File pointers fp1 and fp2
b. Character variables ca and cb to store file characters
c. Character arrays fname1[40] and fname2[40] to store file names
Step 3 : Read the names of the two files from the user.
Step 4 : Open both files in read mode ("r").
Step 5 : If either file cannot be opened, display an error message and exit.
Step 6 : Read the first character from both files: ca = getc(fp1) and cb =
getc(fp2).
Step 7 : Repeat until either file reaches EOF or characters differ:
a. If ca == cb, read the next character from each file.
b. Else, break the loop (files are different).
Step 8 : After the loop:
a. If ca == cb (both reached EOF together), display “FILES ARE
IDENTICAL”.
b. Otherwise, display “FILES ARE DIFFERENT”.
Step 9 : Close both files.
Step 10 : Stop

35
//FILE CONTENT CHECKING

#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fp1,*fp2;
int ca,cb;
char fname1[40],fname2[40];
clrscr();
printf("\nENTER THE FIRST FILE NAME---->");
fflush(stdin);
gets(fname1);
printf("\nENTER THE SECOND FILE NAME---->");
fflush(stdin);
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"r");
if(fp1==NULL)
{
printf("\n CANNOT OPEN THE FILE %s FOR READING\n",fname1);
exit(1);
}
else
if(fp2==NULL)
{
printf("\n CANNOT OPEN %s FOR READING\n",fname2);
exit(1);
}
else
{
ca=getc(fp1);
cb=getc(fp2);
while(ca!=EOF&&cb!=EOF&&ca==cb)
{
ca=getc(fp1);

36
cb=getc(fp2);
}
if(ca==cb)
{
printf("\nFILE ARE IDENTICAL");
}
else
{
if(ca!=cb)
printf("\nFILE ARE DIFFER");
}
fclose(fp1);
fclose(fp2);
}
getch();
}

37
//OUTPUT

38
Ex. No
FILE CHARACTERS COUNT
Date

Aim
To write a C program to count the number of characters and lines in a given text file.
Algorithm
Step 1 : Start
Step 2 : Declare:
a. File pointer fp
b. Character variable ch to read characters from the file
c. Integer variables nc (number of characters) and nlines (number of
lines)
d. Character array fname[32] to store the file name
Step 3 : Initialize counters: nc = 0, nlines = 0.
Step 4 : Read the file name from the user.
Step 5 : Open the file in read mode ("r").
Step 6 : If the file cannot be opened, display an error message and exit.
Step 7 : Read the first character from the file using ch = getc(fp).
Step 8 : Repeat until EOF is reached:
a. Increment character count nc++.
b. If ch == '\n', increment line count nlines++.
c. Read the next character ch = getc(fp).
Step 9 : Close the file.
Step 10 : If nc != 0, display the number of characters and lines.
Step 11 : Else, display “File is empty”.
Step 12 : Stop

39
//FILE CHARACTERS COUNT
#include<stdio.h>
#include<conio.h>
void main()
{
FILE*fopen(),*fp;
int ch,nc,nlines; char fname[32];
nlines=0; nc=0;
clrscr();
printf("ENTER FILE NAME---->");
fflush(stdin);
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("\n CANNOT OPEN THE FILE %s FOR READING\n",fname);
exit(0);
}
ch=getc(fp);
while(ch!=EOF)
{
if(ch=='\n')
nlines++;
nc++;
ch=getc(fp);
}
fclose(fp);
if(nc!=0)
{
printf("\nTHERE ARE %d CHARACTERS %s\n",nc,fname);
printf("\nTHERE ARE %d LINE\n",nlines);
}
else
printf("\n FILE: %s EMPTY\n",fname);
getch();
}

40
//OUTPUT

41

You might also like