Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

C programming file(1)

The document outlines a series of programming experiments focused on various algorithms and data structures, including factorial calculation, geometric series, recursive problems, matrix operations, file handling, and string manipulation. Each experiment includes an aim, source code, and expected output. The experiments are designed to enhance programming skills in C language through practical applications.

Uploaded by

Kush Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C programming file(1)

The document outlines a series of programming experiments focused on various algorithms and data structures, including factorial calculation, geometric series, recursive problems, matrix operations, file handling, and string manipulation. Each experiment includes an aim, source code, and expected output. The experiments are designed to enhance programming skills in C language through practical applications.

Uploaded by

Kush Goyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

INDEX

S.NO EXPERIMENTS SIGN


1 TO FIND FACTORIAL AND DIVISOR OF A NUMBER
2 TO FIND SUM OF GEOMETRIC SERIES
3 TO RECURSIVELY SOLVE THE TOWER OF HANOI
PROBLEM
4 TO RECURSIVELY FIND FIRST M FIBONNACI
NUMBER
5 WRITE A MENU DRIVEN PROGRAM FOR MATRICES
TO DO THE FOLLOWING OPERATION
DEPENDING ON WHETHER THE OPERATION
REQUIRES ONE OR TWO MATRICES
1.) ADDITION OF TWO MATRICES
2.) SUBTRACTION OF TWO MATRICES
3.) FINDING UPPER AND LOWER TRIANGULAR
MATRICES
4.) TRANSPOSE OF A MATRIX
5.) PRODUCT OF TWO MATRICES.
6 WRITE A PROGRAM TO COPY ONE FILE TO OTHER,
USING COMMAND LINE ARGUMENTS.
7 AN ARRAY OF RECORD CONTAINS INFORMATION
OF MANAGERS AND WORKERS OF A COMPANY.
PRINT ALL THE DATA OF MANAGERS AND
WORKERS IN SEPARATE FILES
8 WRITE A PROGRAM TO PERFORM THE
FOLLOWING OPERATORS AN STRINGS WITHOUT
USING STRING
FUNCTIONS
1. TO FIND THE LENGTH OF STRING.
2. TO CONCATENATE TWO STRINGS.
3. TO FIND REVERSE OF A STRING.
4.TO COPY ONE STING TO ANOTHER STRING.
S.NO EXPERIMENTS SIGN
9 WRITE A PROGRAM TO STORE RECORDS OF AN
STUDENT IN STUDENT FILE. THE DATA MUST BE
STORED USING BINARY FILE. READ THE RECORD
STORED IN “STUDENT.TXT” FILE IN BINARY CODE.
EDIT THE RECORD STORED IN BINARY FILE.
APPEND A RECORD IN THE STUDENT FILE.

10 WRITE A PROGRAM TO COUNT THE NUMBER OF


-LOWERCASE
-UPPERCASE
-NUMBERS AND
-SPECIAL CHARACTERS
PRESENT IN THE CONTENT OF FILE.
EXPERIMENT 1

AIM: Write a program to find divisor or factorial of a given number.

SOURCE CODE:

#include<stdio.h>
int main()
{
int i, fact=1, number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
OUTPUT:
EXPERIMENT 2

AIM: Write a program to find sum of geometric series.

SOURCE CODE:

#include <stdio.h>
#include<math.h>
int main() {

int a, n, r;
float tn, sum = 0;

printf(" Please Enter First Number of an G.P Series: ");


scanf("%d", &a);
printf(" Please Enter the Total Numbers in this G.P Series: ");
scanf("%d", &n);
printf(" Please Enter the Common Ratio: ");
scanf("%d", &r);

sum = (a * (1 - pow(r, n ))) / (1- r);


tn = a * (pow(r, n - 1));

printf("\n The Sum of Geometric Progression Series = %.2f", sum);


printf("\n The tn Term of Geometric Progression Series = %.2f \n", tn);
return 0;
}
OUTPUT:
EXPERIMENT 3

AIM: Write a recursive program for tower of Hanoi problem.

SOURCE CODE:

#include <stdio.h>
void towers(int, char, char, char);

int main()
{
int num;

printf("Enter the number of disks: ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi
are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg,
topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
OUTPUT:
EXPERIMENT 4

AIM: Write a recursive program to print the first m Fibonacci number.

SOURCE CODE:

#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);

printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}

else
{
sum=first + second;
first=second;
second=sum;

}
printf(" %d",sum);
}
return 0;
}
OUTPUT:
EXPERIMENT 5

AIM: Write a menu driven program for matrices to do the following


operations depending on whether the operation requires one or two
matrices.

1. Addition of two matrices


2. Subtraction of two matrices
3. Finding upper and lower triangular matrices
4. Transpose of a matrix
5.Product of two matrices

SOURCE CODE:

#include <stdio.h>
int op = 0;
void out(int mat1[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf(" %d", mat1[i][j]);
}
printf("\n");
}
printf("\n");
}

void addin(int mat1[3][3], int mat2[3][3], int mat3[3][3])


{

printf("\nAdding the given matrices:\n");


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("\n");
out(mat3);
}
void subin(int mat1[3][3], int mat2[3][3], int mat3[3][3])
{

printf("\nSubtracting the given matrices:\n");


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mat3[i][j] = mat1[i][j] - mat2[i][j];
}
}
printf("\n");
out(mat3);
}

void multin(int mat1[3][3], int mat2[3][3], int mat3[3][3])


{
printf("\nMultiplying the given matrices:\n");
int cof;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cof = 0;
for (int k = 0; k < 3; k++)
{
cof = cof + mat1[i][k] * mat2[k][j];
mat3[i][j] = cof;
}
}
}
printf("\n");
out(mat3);
}

void trans(int mat1[3][3])


{
int mat2[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mat2[i][j] = mat1[j][i];
}
}
printf("\n");
printf("\nTranspose of the given matrix is:\n");
out(mat2);
}
int trian(int mat1[3][3])
{
int flag = 0;

printf("\nThe given matrix is ");

for (int i = 1; i < 3; i++)


{
for (int j = 0; j < i; j++)
{
if (mat1[i][j] != 0)
{
flag = 0;
}
else
{
flag = 1;
}
}
}
if (flag == 1)
{
printf("an Upper Triangular Matrix.");
return 0;
}
else
{
flag = 2;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
if (mat1[i][j] != 0)
{
flag = 0;
}
}
}
}
if (flag == 2)
{
printf("a Lower Triangular Matrix.");
}
else if (flag == 0)
{
printf("neither an Upper nor a Lower Triangular Matrix.");
}
}
int main()
{
int mat1[3][3], mat2[3][3], mat3[3][3];

printf("Menu Driven Program:\n");


printf(" 1. Addition of two matrices.\n");
printf(" 2. Subtraction of two matrices.\n");
printf(" 3. Multiplication of two matrices.\n");
printf(" 4. Transpose of a given matrix.\n");
printf(" 5. Check if given matrix is upper or lower triangular.\n");
printf("\nEnter the number for the operation to be performed: ");
scanf("%d", &op);

if (op != 4 && op != 5)
{
printf("Enter the first matrix:\n");
for (int i = 0; i < 3; i++)
{
printf("\nElements of row %d:\n", i + 1);
for (int j = 0; j < 3; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the second matrix:\n");
for (int i = 0; i < 3; i++)
{
printf("\nElements of row %d:\n", i + 1);
for (int j = 0; j < 3; j++)
{
scanf("%d", &mat2[i][j]);
}
}
printf("\n\nThe given matrices are:\n\n");
out(mat1);
out(mat2);
}
else
{
printf("Enter the matrix:\n");
for (int i = 0; i < 3; i++)
{
printf("\nElements of row %d:\n", i + 1);
for (int j = 0; j < 3; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("\n\nThe given matrix is:\n");
out(mat1);
}

switch (op)
{
case 1:
addin(mat1, mat2, mat3);
printf("\n");
break;

case 2:
subin(mat1, mat2, mat3);
printf("\n");
break;

case 3:
multin(mat1, mat2, mat3);
printf("\n");
break;
case 4:
trans(mat1);
printf("\n");
break;

case 5:
trian(mat1);
printf("\n");
break;
}

return 0;
}
OUTPUT:
EXPERIMENT 6

AIM: Write a program to copy one file to other, use command line
arguments.

SOURCE CODE:

#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc != 3) printf("Invalid input\n");
else
{
FILE *copy = fopen(argv[1], "r"), *paste = fopen(argv[2], "w");

if (copy == NULL || paste == NULL) printf("Cannot open file\n");


else
{
char c = fgetc(copy);
while (c != EOF)
{
fputc(c, paste);
c = fgetc(copy);
}
printf("successfully done\n");
}
fclose(copy);
fclose(paste);
}
}

OUTPUT:
EXPERIMENT 7

AIM- An array of record contains information of managers and workers


of a company. print all the data of managers and workers in separate
files

SOURCE CODE:

#include<stdio.h>
int main()
{
int n;
printf("no. of records of manager you want to store: \n");
scanf("%d", &n);
int age[100], salary[100];
char name[100][20];
FILE *ftr = fopen("manager.txt", "a");
fprintf(ftr, "NAME AGE SALARY\n");
for (int i = 0; i < n; i++)
{
printf("Enter name, age, salary of manager respectively\n");
scanf("%s%d%d", &name[i], &age[i], &salary[i]);
fprintf(ftr, "%s%d %d\n", name[i], age[i], salary[i]);
}
fclose(ftr);
printf("no. of records of employee you want to store: \n");

scanf("%d", &n);
ftr = fopen("employee.txt", "a");
fprintf(ftr, "NAME AGE SALARY\n");
for (int i = 0; i < n; i++)
{
printf("Enter name, age, salary of employee respectively\n");
scanf("%s%d%d", &name[i], &age[i], &salary[i]);
fprintf(ftr, "%s %d %d\n", name[i], age[i], salary[i]);
}
fclose(ftr);
printf("manager data is :\n");
ftr = fopen("manager.txt", "r");
char ch = fgetc(ftr);
while (ch!= EOF)
printf("%c", ch), ch = fgetc(ftr);
fclose(ftr);
printf("employee data is :\n");
ftr = fopen("employee.txt", "r");
ch = fgetc(ftr);
while (ch!= EOF) printf("%c", ch), ch = fgetc(ftr);
fclose(ftr);}
OUTPUT:
EXPERIMENT 8

AIM: Write a program to perform the following operators an strings


without using string functions:
To find the length of string
To concatenate two strings
To find reverse of a string
To copy one string to another string

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
char str[50], str2[50];
int j, i = 0, count = 0, ch;

int length()
{
printf("Enter a string : ");
getchar();
gets(str);

while (str[i] != '\0')


{
count++;
i++;
}

printf("length : %d", count );


return 0;
}

void catenate()
{
printf("Enter the first String :");
scanf("%s", &str);
printf("Enter the second String :");
scanf("%s", &str2);
while (str[i] != '\0')
{
i++;
}
while (str2[j] != '\0')
{
str[i] = str2[j];
i++;
j++;
}
printf("%s", str);
}
void reverse()
{
printf("Enter the String :");
scanf("%s", &str);
while (str[i] != '\0')
{
i++;
count++;
}
for (j = 0; j < count; j++)
{
str2[j] = str[i - 1];
i--;
}
printf("%s", str2);
}
void copy()
{
printf("Enter the String :");
scanf("%s", &str);
while (str[i] != '\0')
{
str2[i] = str[i];
i++;
}
printf("\nUser entered String is: %s", str);
printf("\nCopied String is : %s", str);
}
void main()
{
printf("Select the operation For
STRINGS:\n1.Length\n2.concatenation\n3.Reverse\n4.Copy\n\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
length();

break;
case 2:
catenate();

break;
case 3:
reverse();

break;
case 4:
copy();

break;

default:
printf("Enter valid choice");
}
}
OUTPUT:
EXPERIMENT 9

AIM: Write a program to store records of a student in student file. The


data must be stored using Binary File. Read the record stored in
“Student.txt” file in Binary Code. Edit the record stored in binary file.
Append a record in the student file.

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[100]; int id;
char gender;
};
int main()
{
FILE *file; char k;
printf("enter r for reading from file and w for writing to file\n");
scanf("%c", &k);
if (k == 'r' || k == 'R')
{
file = fopen("student.bin", "rb");
if (file == NULL) printf("file created\n"); char c = fgetc(file);
while (c != EOF)
{
printf("%c", c); c = fgetc(file);
}
}
else if (k == 'W' || k == 'w')
{
file = fopen("student.bin", "ab");
if (file == NULL) printf("file created\n"); struct student s;
printf("Enter name of student, id and gender respectively\n");
scanf("%s %d %c", &s.name, &s.id, &s.gender); fprintf(file, "%s %d
%c\n", s.name, s.id,
s.gender);
printf("successfully written\n");
}
else printf("wrong input\n"); fclose(file);
}
OUTPUT:
EXPERIMENT 10

AIM: Write a program to count the number of lowercase, Uppercase


and Special characters present in the contents of a file.

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
int main()

{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);

fptr = fopen(filename, "r"); if (fptr == NULL)


{
printf("Cannot open file \n");
exit(0);
}
int u = 0, l = 0, s = 0, n = 0; c = fgetc(fptr);
while (c != EOF)
{
if (c >= 'A' && c <= 'Z') ++u;
else if (c >= 'a' && c <= 'z')
++l;
else if (c >= '0' && c <= '9')
++n;
else
++s;
c = fgetc(fptr);
}
fclose(fptr);
printf("\nupper character is : %d\nlower character is :%d\nnumber
is : %d\nspecial character is : %d\n", u, l, n, s);
}

OUTPUT:

You might also like