C Lab Record-CSE
C Lab Record-CSE
REGISTER NUMBER :
BRANCH :
BONAFIDE CERTIFICATE
Selvan/Selvi................................................................. with
Algorithm
1. Read a number.
2. Find the square of the number (square=number*number).
3. Find the cube of the number (cube= number*number*number).
4. Print the square and cube of the number.
5. Terminate the Program.
Program
/* Finding the Square and Cube of a number */
#include <stdio.h>
#include <conio.h>
void main()
{
int num, square, cube;
clrscr();
printf("Enter a integer : ");
scanf("%d", &num);
square = num * num;
cube = num * square;
printf("Given number = %d, Square Value = %d, Cube value = %d\n", num, square,
cube);
getch( );
}
Result
Thus the C Program to find the square and cube of a number is executed and the
Output is obtained.
getch( );
}
Result :
Thus the C Program to find Simple Interest and Compound Interest is executed
and the Output is obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 5 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No 1c FINDING THE AREA OF A TRIANGLE
Date:
Aim:
To write a C Program to find the Area of the Triangle.
Algorithm:
1. Read three sides a, b, and c.
2. Calculate s = (a + b + c)/2;
3. Calculate the area = sqrt(s * (s-a) * (s-b) * (s-c));
4. Print the Area of the Triangle
5. Terminate the Program
Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a, b, c, s, area;
clrscr();
printf("Enter the Three Sides : ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c)/2;
area = sqrt(s * (s-a) * (s-b) * (s-c));
printf("\nArea of the Triangle = %.2f", area);
getch( );
}
Result :
Thus the C Program to find Area of the Triangle is executed and the Output is
obtaine
Result:
Thus the C Program to check whether the person is eligible for voting or not is
executed and the Output is obtained.
Result
Thus the Program to check whether the given number is odd or even is executed and the
Output is obtained.
Result
Thus the Program to find largest among three numbers is executed and the Output is
obtaine
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, j, flag, count = 0;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
if(n < 2)
{
printf("There are no primes upto %d\n", n);
exit(0);
}
printf("The Prime numbers are\n");
for (i=2; i<=n; i++)
{
flag = 0;
for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ",i);
count++;
}
24CS1511 – PROGRAMMING PRACTICES LAB 11 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
}
printf("\nNumber of primes upto %d = %d",n,count);
getch( );
Result
Thus the C program to find n prime numbers using loop has been executed and
the output was obtained.
Result
Thus the C program to find the sum of digits and reverse for a given number was
written, entered, executed and the output was obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 13 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex No: 3 c PRINTING THE AVERAGE OF NUMBERS UNTIL ZERO IS
ENTERED
Date:
Aim
To write a C Program to read in values until a zero is entered, then print
average.
Algorithm
1. Read a number
2. If number is equal to zero go to step 4 and calculate the average.
3. Else calculate the sum and read the next number.
4. Print the average.
5. Terminate the Program.
Program
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int count = 0, sum = 0, value;
float average;
clrscr();
/* read in values until a zero is entered, then print average */
printf("Enter a integer: ");
scanf("%d", &value);
while(value != 0)
{
sum = sum + value;
count++;
printf("Enter a integer: ");
scanf("%d", &value);
}
if(count > 0)
{
average = sum / count;
printf("The average of the values is %.2f\n", average);
}
else
printf("No Values has been entered");
getch( );
Result :
Thus the C Program to find average of numbers is executed and the output is
obtained.
Program
#include <stdio.h>
#include <conio.h>
main()
{
int n1=1, n2=1, n3, ctr=2, n;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
printf("%d %d ", n1, n2);
while(ctr<=n)
{
n3 = n1 + n2;
printf("%d ", n3);
n1 = n2;
n2 = n3;
ctr++;
};
getch( );
}
Result
Thus the C program to print the Fibonacci Series is done and output Obtained.
AIM:
To write a C Program to print numbers in Ascending and Descending order.
ALGORITHM:
Step 1: Start
Step 2: Declare variables
Step 3: Get the no. of terms N.
Step 4: Get the numbers using for loop and store it in array A[10].
Step 5: Set two for loops to arrange the given numbers in ascending order in the
array A[10].
Step 5.1: Check IF(A[I]>A[j]) then exchange the current two numbers and
continue
the same process until end of the array A[10].
Step 6: Print the numbers in Ascending order and Descending order using for
loop.
Step 7: Stop
Program:
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("\nASCENDING AND DESCENDING ORDER OF THE GIVEN NUMBERS");
printf("\n ---------------------------------------------------------------------------");
printf("\nEnter the no. of terms :");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nAscending order :");
for(i=0;i<n;i++)
24CS1511 – PROGRAMMING PRACTICES LAB 17 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
printf("%d\t",a[i]);
printf("\nDescending order:");
for(i=n-1;i>=0;i--)
printf("%d\t",a[i]);
getch();
}
RESULT:
Thus a C program to print numbers in Ascending and Descending order was executed and the output
was obtained.
second
matrix”.
Step 11: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,o,p;
clrscr();
printf("\nMATRIX MULTIPLICATION\n");
printf("\n ----------------------------- \n");
printf("\nEnter the rows & columns of first matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the rows & columns of second matrix: ");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix-->");
RESULT:
Thus a C program for the implementation of matrix multiplication was executed
and the output was obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 22 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex. No.5 PROGRAMS USING STRINGS
AIM:
To write a C program to illustrate the concept of string handling functions.
ALGORITHM:
Step 1: Start the program.
Step 2: Get two strings as input.
Step 3: Use the string compare function for checking the two strings are equal.
Step 4: Use the string concatenation function for joining first string with second string.
Step 5: Find the length of the strings using the strlen function.
Step 6: Print the reverse of a string using strrev function.
Step 7: Show the upper case and lower case of a string using strupr and strlwr functions.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int s;
char string1[10],string2[10];
clrscr();
printf("Enter string 1 : ");prp
scanf("%s",&string1);
printf("Length of the string is : %d",strlen(string1)); // string length
printf("Uppercase of the string is : %s",strupr(string1)); // string uppercase
printf("Lowercase of the string is : %s",strlwr(string1)); // string lowercase
printf("Reverse of the string is : %s",strrev(string1)); // string reverse
strcpy(string2,string1);
printf("The copied string : %s",string2); // string copy
strcat(string2,string1);
printf("The concatenated string : %s",string2); //string concatenation
s=strcmp(string2,string1);
printf("string comparison\n"); // string comparison
if(s==0)
24CS1511 – PROGRAMMING PRACTICES LAB 23 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
{
printf("strings 1&2 are equal");
}
else
{
printf("strings 1&2 are not equal\n");
}
getch();
}
RESULT:
Thus a C program for the implementation of String functions were executed and
the output was obtained.
Program:
#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{
int a=10,b=20;
int c=add(10,20); //function call
printf("Addition:%d\n",c);
getch();
}
int add(int a,int b) //function body
{
int c;
c=a+b;
return c;
getch( );
Result :
Thus the C Program to add two numbers for the given number is executed and the
output is verified.
24CS1511 – PROGRAMMING PRACTICES LAB 25 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
FINDING THE FACTORIAL OF A NUMBER
Ex No 6 b
Date:
Aim:
To write a C Program to find the Factorial of a given number.
Algorithm:
1. Read the number n.
2. Pass the value as an argument inside the fuction
3. If n>=1
Return n*factorial(n-1)
Else
Return 1
4. Print the Factorial of the given number
5. Terminate the Program
Program
#include<stdio.h>
long int factorial(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
long int factorial(int n) {
if (n>=1)
return n* factorial(n-1);
else
return 1;
getch( );
}
Output:
Enter the Number : 5
Factorial of 5 = 120
Result :
Thus the C Program to find Factorial of a given number is executed and the
Output is obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 26 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAM USING FUNCTIONS AND POINTERS
Ex no : 7 a PROGRAM TO PERFORM SWAPPING USING FUNCTION.
Date:
Aim:
To write a C program to perform swapping using function.
Algorithm:
1. Start the program
2. Declare and get the two integer variables a and b.
3. call the swap () function
In swap definition use the temporary variable and assign temp =a
a=b
b=temp
4. Print the a and b value.
5. Display the result
6. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(inta,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
getch( );
}
Result:
Thus the C program to perform swapping using function has been successfully executed
and verified.
24CS1511 – PROGRAMMING PRACTICES LAB 27 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Ex no 7 b PROGRAM USING POINTERS
Aim
To write a program to perform pointer operations
Algorithm:
1. Declare the variable and pointer variable
2. Assign values to the variable
3. Assigning the address of variable var to the pointerb * p.
4. The p can hold the address of var because var is * an integer type variable.
5. Print all the values and address
6. Terminate the process
Program:
#include <stdio.h>
int main()
{
/* Pointer of integer type, this can hold the
* address of a integer type variable.
*/
int *p;
p= &var;
Result:
Thus the program to perform pointer operations has been successfully executed
and verified.
24CS1511 – PROGRAMMING PRACTICES LAB 28 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING STRUCTURES AND POINTERS
Date:
Aim
To write a C Program to Generate salary slip of employees using structures and
pointers.
Algorithm
1. Start
2. Declare variables
3. Read the number of employees .
4. Read allowances, deductions and basic for each employee.
5. Calculate net pay= (basic+ allowances)-deductions
6. Display the output of the Pay slip calculations for each employee.
7. Stop
Program
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int empno ;
char name[10], answer ;
int bpay, allow, ded, npay ;
} e[100];
void main()
{
int i, n;
clrscr() ;
printf("Enter No. of employees : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("Enter the name : ") ;
scanf("%s",&e[i].name) ;
printf("Enter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t\t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i=0; i<n; i++)
{
printf("%d \t %s \t\t %d \t %d \t %d \t %d \n", e[i].empno, e[i].name, e[i].bpay,
e[i].allow, e[i].ded, e[i].npay);
}
getch() ;
}
24CS1511 – PROGRAMMING PRACTICES LAB 29 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
Result
Thus a C Program for Salary Slip of employees was executed and the output was
obtained
24CS1511 – PROGRAMMING PRACTICES LAB 30 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY
PROGRAMS USING STRUCTURES AND UNIONS
Result
Thus a C program for internal marks of students was executed and the output was
obtained.
void createAndWrite();
void readFile();
void appendFile();
void copyFile();
void deleteFile();
int main() {
int choice;
do {
printf("\n=== File Operations Menu ===\n");
printf("1. Create and Write to a File\n");
printf("2. Read from a File\n");
printf("3. Append to a File\n");
printf("4. Copy a File\n");
printf("5. Delete a File\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createAndWrite();
break;
case 2:
readFile();
break;
case 3:
return 0;
}
void createAndWrite() {
FILE *file;
char filename[100], content[1000];
void readFile() {
FILE *file;
char filename[100], ch;
fclose(file);
}
void appendFile() {
FILE *file;
char filename[100], content[1000];
void copyFile() {
FILE *srcFile, *destFile;
char srcFilename[100], destFilename[100], ch;
fclose(srcFile);
fclose(destFile);
printf("File '%s' copied to '%s' successfully.\n", srcFilename, destFilename);
}
void deleteFile() {
char filename[100];
if (remove(filename) == 0) {
printf("File '%s' deleted successfully.\n", filename);
} else {
printf("Error: Could not delete file '%s'. File may not exist.\n", filename);
}
}
Result
Thus a C program for various file operations in C was executed and the output was
obtained.
24CS1511 – PROGRAMMING PRACTICES LAB 36 | Page
PPPPPPPRACTICESPLLHJLABORATORY LABORATORY