Ai&ds-E Programming in C - Practical-Record
Ai&ds-E Programming in C - Practical-Record
(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.
Programming in C– Practical
(24ADU101)
SEMESTER: I
(2024-2027 Batch)
NAME:
REG.NO:
KARPAGAM ACADEMY OF HIGHER EDUCATION
(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.
DEPARTMENT OF COMPUTER APPLICATIONS
BONAFIDE CERTIFICATE
REGISTER NUMBER:
academic year July 2024 to November– 2024 and submitted for the practical examination
PAGE STAFF
S.NO DATE TITLE
NO SIGNATURE
10
11
12
EX.NO: 1 SUM, AVERAGE, STANDARD DEVIATION
Date:
AIM:
To Write a C program to find the sum, average, standard deviation for a given set of numbers.
ALGORITHM:
Step 1:.Calculate the sum of all numbers in the set.
Step 2: Calculate the average by dividing the sum by the total number of elements in the set.
Step 3: Calculate the variance, which is the average of the squared differences between each
number and the average.
Step 4: Calculate the standard deviation, which is the square root of the variance.
Step5: Stop the program
SOURCE CODE
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 100
int main() {
int n, i;
float numbers[MAX_SIZE], sum = 0.0, mean, deviation = 0.0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; ++i) {
scanf("%f", &numbers[i]);
sum += numbers[i];
}
mean = sum / n;
for (i = 0; i < n; ++i) {
deviation += pow(numbers[i] - mean, 2);
}
deviation = sqrt(deviation / n);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", mean);
printf("Standard Deviation = %.2f\n", deviation);
return 0;
}
OUTPUT :
Enter the number of elements: 5
Enter 5 numbers:
2
4
6
8
10
Sum = 30.00
Average = 6.00
Standard Deviation = 2.83
RESULT:
This program prompts the user to enter the number of elements and the elements themselves.
Then, it calculates and prints the sum, average, and standard deviation of the given set of numbers.
EX NO: 2 PRIME, PERFECT, ARMSTRONG NUMBERS
Date:
AIM:
ALGORITHM:
**Generate 'n' Prime Numbers:**
Step 1 : Start with a variable `num` initialized to 2.
Step 2 : Use a while loop to keep generating prime numbers until 'n' prime numbers are found.
Step 3 : Use a function to check if a number is prime or not.
Step 4: Increment `num` and check for the next prime number.
**Generate 'n' Perfect Numbers:**
Step 5: Start with a variable `num` initialized to 1.
Step 6: Use a while loop to keep generating perfect numbers until 'n' perfect numbers are found.
Step 7: Use a function to check if a number is perfect or not.
Step 8: Increment `num` and check for the next perfect number.
**Generate 'n' Armstrong Numbers:**
Step 8: Start with a variable `num` initialized to 1.
Step 9: Use a while loop to keep generating Armstrong numbers until 'n' Armstrong numbers are found.
Step 10: Use a function to check if a number is Armstrong or not.
Step 11: Increment `num` and check for the next Armstrong number.
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
if (result == num)
return true;
return false;
}
int main() {
int n;
printf("Enter the limit (n): ");
scanf("%d", &n);
return 0;
}
OUTPUT:
Enter the limit (n): 100
Prime numbers up to 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Perfect numbers up to 100:
6 28
Armstrong numbers up to 100:
1 2 3 4 5 6 7 8 9 153 370 371 407
RESULT:
AIM:
To Write a C program to generate Fibonacci series
ALGORITHM:
void fibonacci(int n) {
int first = 0, second = 1, next, i;
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}
OUTPUT:
Result:
Thus the above program has been executed successfully.
EX NO: 4 MAGIC SQUARE
Date:
AIM:
To Write a C program to print magic square of order n where n>3and n is odd.
ALGORITHM:
Step 1: Initialize the square with all elements as 0.
Step 2: Start from the middle column of the first row.
Step 3: Place the number 1 in the middle of the top row.
Step 4: Move diagonally up and to the right, placing the next number in the next cell. If moving
up or to the right would put you outside the square, wrap around to the opposite side.
Step 5: If the cell is already filled, move down one cell and continue.
Step 6: Repeat step 4 until all cells are filled with numbers from 1 to n^2.
SOURCE CODE:
#include <stdio.h>
void generateMagicSquare(int n) {
int magicSquare[n][n];
num++;
}
int main() {
int n;
// Prompt the user to enter the order of the magic square
printf("Enter the order of the magic square (n > 3 and n is odd): ");
scanf("%d", &n);
return 0;
}
OUTPUT:
RESULT:
This code generates a magic square of order n and prints it out
EX NO: 5 SORTING
Date:
AIM:
To Write a C program to sort the given set of numbers in ascending order.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of elements, n.
Step 3: Input n elements into an array number.
Step 4: Use nested loops to compare and swap elements to sort the array in ascending order.
Step 5: Print the sorted array.
Step 6: End the program.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a, n, number[30];
17 5 45 8 13
5 8 13 17 45
RESULT:
Thus the above program has been executed successfully
EX No: 6
PALINDROME
Date:
AIM:
To write a C program to check whether the given string is a palindrome or not using pointers
ALGORITHM:
Step 1: Start the Program
Step 2: initialize the two pointer variable ptr and rev.
Step 3: Initialize ptr to the base address of the string and move it forward to point to the last
character of the string.
Step 4: Now, initialize rev to the base address of the string and start moving rev in forward
direction and ptr in backward direction simultaneously until middle of the string is reached.
Step 5: If at any point the character pointed by ptr and rev does not match, then break from the
loop.
Step 6: Check if ptr and rev crossed each other, i.e. rev > ptr. If so, then the string is
palindrome otherwise not.
Step 7: End
SOURCE CODE:
ptr = string;
isPalindrome(str);
return 0;
}
OUTPUT:
RESULT:
Thus the program has been successfully check the given string is a palindrome or not using
pointers
EX No:7 COUNT THE NUMBER OF VOWELS
Date:
AIM:
To write a C program to count the number of Vowels in the given sentence
ALGORITHM:
#include <stdio.h>
#include <ctype.h>
int main()
{
// Initializing variable.
char str[100];
int i, vowels = 0;
// Accepting input.
printf("Enter the string: ");
// best way to read string rather than gets/fgets
scanf("%[^\n]s",&str);
//Initializing for loop.
for(i = 0; str[i]; i++)
{
//Counting the vowels.
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')
{
vowels++;
}
}
//Printing the count of vowels.
printf("Total number of vowels: = %d\n", vowels);
return 0;
}
OUTPUT:
RESULT:
Thus the program has been successfully count the number of Vowels in the given sentence.
EX No:8 FACTORIAL USING RECURSIVE FUNCTION
Date:
Aim:
Write a C program to find the factorial of a given number using recursive function
Algorithms:
Step 1: Start the program
Step 2: Ask the user to enter an integer to find the factorial
Step 3: Read the integer and assign it to a variable
Step 4: From the value of the integer up to 1, multiply each digit and update the final value
Step 5: The final value at the end of all the multiplication till 1 is the factorial
Step 6: End
SOURCE CODE:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT:
RESULT:
Thus the Program has been successfully finding the factorial of a given number using recursive
function
EX No:9 STUDENTS MARK SHEET
Date:
AIM:
To write a C program to print the students Mark sheet assuming roll no, name, and marks in
5 subjects in a structure. Create an array of structures and print the mark sheet in the University
pattern
ALGORITHM:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float m1,m2,m3,m4,m5;
} s[5];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter mark1 English: ");
scanf("%f", &s[i].m1);
printf("Enter mark2 Tamil: ");
scanf("%f", &s[i].m2);
printf("Enter mark3 C Program: ");
scanf("%f", &s[i].m3);
printf("Enter mark4 DCP: ");
scanf("%f", &s[i].m4);
printf("Enter mark5 office automation: ");
scanf("%f", &s[i].m5);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].m1);
printf("\n");
printf("Marks: %.1f", s[i].m2);
printf("\n");
printf("Marks: %.1f", s[i].m3);
printf("\n");
printf("Marks: %.1f", s[i].m4);
printf("\n");
printf("Marks: %.1f", s[i].m5);
printf("\n");
}
return 0;
OUTPUT:
RESULT:
Thus the program has been successfully print the students details.
EX No:10 MATRICES USING POINTER TO FUNCTION
Date:
AIM:
To write a function using pointers to add two matrices and to return the resultant matrix to the
calling function.
ALGORITHM:
Step 1: Start the program
Step 2: Initialize the two dimensional array variable a,b , row and col
Step 3: Initialize the Function declaration
Step 4: Read the input values
Step 5: Initialize the for loop
Step 6: Print the entered values
Step 7: Declare return 0
Step 8: End
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int a[5][5],b[5][5],row, col;
void add(int(*)[5]);
int main()
{
int c[5][5],i,j;
clrscr();
printf(“Enter row : “);
scanf(“%d”,&row);
printf(“Enter column : “);
scanf(“%d”,&col);
printf(“Enter matrix A :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter matrix B :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&b[i][j]);
}
}
add(c);
printf(“Addition :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();
return 0;
}
void add(int c[5][5])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
OUTPUT:
Enter row : 3
Enter column : 3
Enter matrix A :
333
333
333
Enter matrix B :
555
555
555
Addition :
8
8
8
8
8
8
8
8
8
RESULT:
Thus the program has been successfully executed the function using pointers to add two matrices
and to return the resultant matrix to the calling function.
EX No:11 COMPARE TWO FILES
Date:
AIM:
To write a C program which receives two filenames as arguments and check whether the file
contents are same or not. If same delete the second file.
ALGORITHM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
int compareFiles(const char *file1, const char *file2);
int deleteFile(const char *filename);
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <file1> <file2>\n", argv[0]);
return 1;
}
if (compareFiles(argv[1], argv[2])) {
printf("Files %s and %s have the same contents.\n", argv[1], argv[2]);
if (deleteFile(argv[2]))
printf("File %s deleted successfully.\n", argv[2]);
else
printf("Unable to delete file %s.\n", argv[2]);
} else {
printf("Files %s and %s do not have the same contents.\n", argv[1], argv[2]);
}
return 0;
}
int compareFiles(const char *file1, const char *file2) {
FILE *fp1, *fp2;
char buffer1[BUFFER_SIZE];
char buffer2[BUFFER_SIZE];
fp1 = fopen(file1, "r");
if (fp1 == NULL) {
perror("Error opening file 1");
exit(1);
}
fp2 = fopen(file2, "r");
if (fp2 == NULL) {
perror("Error opening file 2");
exit(1);
}
while (!feof(fp1) && !feof(fp2)) {
size_t bytesRead1 = fread(buffer1, 1, BUFFER_SIZE, fp1);
size_t bytesRead2 = fread(buffer2, 1, BUFFER_SIZE, fp2);
if (bytesRead1 != bytesRead2 || memcmp(buffer1, buffer2, bytesRead1) != 0) {
fclose(fp1);
fclose(fp2);
return 0; // Files are different
}
}
// Check if both files have reached EOF
if (feof(fp1) && feof(fp2)) {
fclose(fp1);
fclose(fp2);
return 1; // Files are the same
} else {
fclose(fp1);
fclose(fp2);
return 0; // Files are different
}
}
int deleteFile(const char *filename) {
if (remove(filename) == 0)
return 1; // Success
else
return 0; // Failure
}
OUTPUT:
File1.txt
This is file1.txt.
File2.txt
This is file1.txt.
RESULT:
Thus the program has been successfully receives two filenames as arguments and check whether
the file contents are same or not. If same delete the second file.
EX No:12 COPY A FILE AND APPEND ITS CHARACTER, WORD, AND LINE COUNTS
Date:
AIM:
To write a program which takes a file as command line argument and copy it to another file. At
the end of the second file write the total i) no.of chars ii) no.of words and iii)no.of lines.
ALGORITHM:
Step 1: Start the program
Step 2: Declare the file
Step 3: Initialize the variable
Step 4: Read the input of source file
Step 5: Initialize the if condition to check the file is exists
Step 6: Initialize the while loop to check the characters , words and lines
Step 8: Close the files
Step 9: End
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
/* Input path of files to merge to third file */
printf("Enter source file path: ");
scanf("%s", path);
/* Open source files in 'r' mode */
file = fopen(path, "r");
/* Check if file opened successfully */
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
if (ch == '\n' || ch == '\0')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
/* Increment words and lines for last word */
if (characters > 0)
{
words++;
lines++;
}
I love programming.
Working with files in C programming is fun.
I am learning C programming at Code for win.
OUTPUTS :
RESULT:
Thus the program has been successfully takes a file as command line argument and copy it to
another file. At the end of the second file write the total.