Introduction To Programming ETCS-108: Practical File)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 55

INTRODUCTION TO PROGRAMMING

ETCS-108

(Practical File)

Submitted to: Submitted by:


Ms. Ankita Gupta Aman (E72)
Assistant Professor Enrolment No.:
01314803120

Maharaja Agrasen Institute of Technology, PSP Area, Sector – 22,


Rohini, New Delhi – 110085
INDEX

S No. Experiment Date Remarks

1. 1.1)WAP to print 'Hello World'

2. 2.1)WAP to sum numbers.


2.2)WAP to take input and print user’s first alphabet of
his name and age.

3. 3.1)WAP to swap two numbers


3.2)WAP to check number is divisible by 5, 9, 15.
3.3)WAP to take 2 numbers and perform operation based on
user’s choice as 1 = +, 2 = ‐, 3 = *.
3.4)WAP to calculate sum of a GP series.

4. 4.1)WAP to print the sum of digits of a number entered


by user.
4.2)WAP to find whether entered alphabet is a vowel
or a consonant.
4.3)WAP to find the roots of a quadratic equation
entered by user.
4.4)WAP to find whether the entered number is prime
or not

5. 5.1)WAP to print patterns‐


1
5.2)WAP to print patterns‐
2
5.3)WAP to print patterns‐
3
6. 6.1)WAP to print largest and smallest digit in the no.
entered by user.
6.2)WAP to count and print occurring of 3 in each and
every number from 0 to n.
6.3)WAP to convert uppercase character to lowercase
and vice versa.
6.4)WAP to find and display position of an array element
and its address entered by the user.
6.5)Write a program in C to match two arrays and
find similarity scope.
6.6)WAP to enter 2‐D array of size m*n from the
user and display the content row‐wise and column
wise.
7. 7.1)WAP to reverse a number using a function.
7.2)WAP to copy a string to another string without
using any string function.
7.3)WAP to input and displays the information of
students using structures.
7.4)Write a menu driven program for matrices to do
the following operations depending on whether the
operation requires one or two matrices:
a) Addition of two matrices
b) Subtraction of two matrices
d) Trace of a matrix
e) Transpose of a matrix
f) Product of two matrices.

8. 8.1)WAP to input and print array elements using pointers.


8.2)WAP to swap two arrays using pointers.
8.3)WAP to count the no of Lowercase, Uppercase
numbers and special characters presents in the
contents of file.
8.4)WAP 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.
8.5)WAP to copy one file to other, use command line
arguments.
Experiment – 1

Aim: Write a program to print Hello World.

Code –
#include <stdio.h>

using namespace std;

int main()
{
printf("Hello World");

return 0;
}

Output-
Experiment – 2.1

Aim: Write a program in C to print the sum of four numbers with taking
input from the user.

Code-

#include <stdio.h>

using namespace std;

int main(){
int num1,num2,num3,num4,ans;
printf("Enter 4 integers: ");
scanf("%d %d %d %d",&num1,&num2,&num3,&num4);

ans = num1 + num2 + num3 + num4;


printf("%d + %d + %d + %d =
%d\n",num1,num2,num3,num4,ans);
return 0;
}

OutPut-
Experiment – 2.2

Aim: Write a program in C to take input from the user and print user's first
alphabet of his name and age of the user.

Code-

#include <stdio.h>

using namespace std;

int main(){
int age;
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin);
printf("\nEnter age: ");
scanf("%d",&age);
printf("\nFirst alphabet of name: %c",name[0]);
printf("\nAge: %d\n",age);
return 0;
}

Output-
Experiment – 3.1

Aim: Write a program in C to swap two numbers

Code-

#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int temp, a, b;
printf("Enter 2 numbers A B :");
scanf("%d%d", &a, &b);
temp = a;
a = b;
b = temp;
printf("number swapped! \nA = %d \nB = %d", a, b);
getch();
return 0;
}

Output-
Experiment – 3.2

Aim: Write a C program to check whether given no. is divisible by


5,9&15.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int temp, a, b;
printf("Enter a numbers :");
scanf("%d", &b);
if (b % 5 == 0 || b % 9 == 0 || b % 15 == 0)
printf("Number is divisible by 5, 9 & 15");

else
printf("Number is not divisible by 5, 9 & 15");

getch();
return 0;
}

Output-
Experiment – 3.3

Aim: Write a program in C to make a calculator using switch statement

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
char n;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &n);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (n)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
getch();
}
Output-
Experiment – 3.4

Aim: Write a program to calculate sum of G.P Series.

Code-
#include <stdio.h>
#include <conio.h>
#include <math.h>

using namespace std;


int main()
{
float a, r, i;
int n;
float sum = 0;
printf("Enter the first number of the G.P. series: ");
scanf("%f", &a);
printf("Enter the total numbers in the G.P. series: ");
scanf("%d", &n);
printf("Enter the common ratio of G.P. series: ");
scanf("%f", &r);
sum = (a * (1 - pow(r, n + 1))) / (1 - r);
printf("\nSum of the G.P.: %f", sum);
getch();
return 0;
}

Output-
Enter the first number of the G.P. series: 1
Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000
Experiment – 4.1

Aim: Write a program in C to print sum of digits of a number entered


by user

Code-
#include <stdio.h>
#include <conio.h>

using namespace std;


int main()
{
int a, sum = 0;
printf("Enter a number: ");
scanf("%d", &a);
while (a)
{
sum = sum + a % 10;
a /= 10;
}
printf("\nSum is= %d", sum); getch();
return 0;
}

Output-
Experiment – 4.2

Aim: Write a program in C to find whether entered alphabet is vowel or


constant

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
char c;
printf("Enter an alphabet: ");
scanf("%c", &c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c
== 'E' || c == 'I' || c == 'O' || c == 'U')
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
getch();
return 0;
}

Output-

Enter an alphabet: G
G is a consonant.
Experiment – 4.3

Aim: Write a program in C to find out roots of the quadratic equation


entered by the user

Code-

#include <stdio.h>
#include <conio.h>
#include <math.h>

using namespace std;

int main()
{
int a, b, c, d;
double root1, root2;

printf("Enter a, b and c where a*x*x + b*x + c = 0\n"); scanf("%d%d


%d", &a, &b, &c);

d = b * b - 4 * a * c;

if (d < 0)
{ // complex roots, i is for iota (√-1, square root of -1)
printf("First root = %.2lf + i%.2lf\n", -b / (double)(2 * a), sqrt(-
d) / (2 * a));
printf("Second root = %.2lf - i%.2lf\n", -b / (double)(2 * a), sqrt(-
d) / (2 * a));
}
else
{ // real roots
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);

printf("First root = %.2lf\n", root1);


printf("Second root = %.2lf\n", root2);
}
getch();
return 0;
}
Output-

Enter coefficients a, b and c: 2.3


4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
Experiment – 4.4

Aim: Write a program in C to find whether the entered no. is prime or


not.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int n, m;
printf("Enter the number to check prime:");
scanf("%d", &n);
m = n / 2;
for (int i = 2; i <= m; i++)
{
if (n % i == 0)
{
printf("Number is not prime");
getch();
return 0;
}
}
printf("Number is prime");
return 0;
}

Output-

Enter a positive integer: 29


29 is a prime number
Experiment – 5.1

Aim: Write a program in C to Print the pattern.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int a;
printf("Enter a number :");
scanf("%d", &a);
printf("\n");
for (int i = 0; i < a; i++)
{
for (int j = 0; j <= i; j++)
{
printf("*");
}
printf("\n");
}
getch();
return 0;
}

Output-
Experiment – 5.2

Aim: Write a program in C to Print the pattern.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int a;
printf("Enter a number :");
scanf("%d", &a);
printf("\n");
for (int i = 0; i < a; i++)
{
for (int j = 0; j <= i; j++)
{
printf("%d\t", j + 1);
}
printf("\n");
}
getch();
return 0;
}

Output-
Experiment – 5.3

Aim: Write a program in C to Print the pattern.

Code-
#include <stdio.h>
#include <conio.h>
int main()
{
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1)
{
printf("* ");
++k;
}
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 6.1

Aim: Write a program in C to print largest & smallest digit in the no.
entered by the user.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int n, p, digit, large = 0, small = 10;

printf(“enter any integer number: “);


scanf(“%d”, &n); //taking input

p = n;

while (n > 0)
{
digit = n % 10;

if (digit > large)


large = digit;
if (digit < small)
small = digit;
n = n / 10;
}

printf(“\nLargest digit in number %d = %d”, p, large); printf(“\


nSmallest digit in number %d = %d”, p, small); getch();
return 0;
}
Output-
Experiment – 6.2

Aim: Write a program in C to count & print occurring of 3 in each and


every number from 0 to n(n being entered by the user).

Code-
#include <stdio.h>
using namespace std;
int count_3s(int n)

{
int count = 0;
while (n > 0)
{
if (n % 10 == 3)
{
count++;
}
n = n / 10;
}
return count;
}

int count_in_range(int n)
{
int count = 0;
for (int i = 2; i <= n; i++)
{
count += count_3s(i);
}
return count;
}

int main()
{
int n;
printf("\nEnter the end value: ");
scanf("%d", &n);
printf("\nTotal occurrences of 3 from 0 to % d is % d\n", n,
count_in_range(n));
return 0;
}
Output-
Experiment – 6.3

Aim: Write a program in C to convert uppercase character to lowercase


character and vice-versa.

Code-

#include<stdio.h>
 
int main()
{
    
char str[20];
    int i;
 
    printf("\n\nEnter The String: ");
    scanf("%s",str);
 
    int len=0;
    // Calculating length of input string
    
    while(str[len]!='\0')
    {
        len++;
    }
    for (i=0;i<len;i++)
    {
        if (str[i]>=65 && str[i]<=90)
        {
         str[i] = str[i] + 32;
        }    
 
        else if (str[i] >= 97 && str[i] <= 122)
        {
         str[i] = str[i] - 32;
        }    
    }
    
    printf("\nConverted String(Lower/Upper) Is: %s\n",str);

    return 0;
}
Output-
Experiment – 6.4

Aim: Write a program in C to find and display position of an array


element and its address entered by the user.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int arr[10] = {1, 7, 2, 6, 0, 8, 5, 3, 9, 4};
int element = 0, position = 0, index = 0;
printf("Available elements are:");

for (index = 0; index < 10; ++index)


{
printf("%d\n", arr[index]);
}

printf("Enter the element whose position is to be found: ");


scanf("%d", &element);

for (index = 0; index < 10; index++)


{
if (arr[index] == element)
{
position = index + 1;
break;
}
}

printf("The position of %d in array is: %d\n", element, position);


printf("The address of %d in array is: %p", element, &arr[index]);
getch();
return 0;
}
Output-
Experiment – 6.5

Aim: Write a program in C to match two arrays and find similarity


scope.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main(void)
{
int array1[10];
int array2[10];
int i;

for (i = 0; i < 5; i++)


{
printf("Enter numbers for array 1: ");
scanf("%d", &array1);
}
printf("\n");
for (i = 0; i < 5; i++)
{
printf("Enter numbers for array 2: ");
scanf("%d", &array2);
}

for (i = 0; i < 5; i++)


{
if (array1[i] != array2[i])
{
printf("Not equal \n");
getch();
return 1;
}
}
printf("They are equal. \n");
getch();
}
Output-
Experiment – 6.6

Aim: Write a program in C to enter 2-D array of size m*n from the user
and display the content row-wise and column wise.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
/* 2D array declaration*/
int disp[20][20];
/*Counter variables for the loop*/
int i, j, m, n;
printf("Enter Number of rows :");
scanf("%d", &m);
printf("Enter Number of columns :");
scanf("%d", &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements printf("\
nrow-wise array elements:\n"); for (i =
0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d \t", disp[i][j]);
}
}
printf("\ncolumn-wise array elements:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d \t", disp[j][i]);
}
}
getch();
return 0;
}

Output-
Experiment – 7.1

Aim: Write a program in C to reverse a number using a function.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
getch();
return 0;
}

Output-
Experiment – 7.2

Aim: Write a program in C to copy a string to another string without using


any string function.

Code-
#include <stdio.h>
#include <conio.h>
int main()
{
char s1[100], s2[100];
int i;

printf("\nEnter string s1:");


gets(s1);

i = 0;
while (s1[i] != '\0')
{
s2[i] = s1[i]; i+
+;
}

s2[i] = '\0';
printf("\nString s2: %s ", s2); getch();
return (0);
}
Output-

Enter string s1: Hey fellow programmer.


String s2: Hey fellow programmer.
Experiment – 7.3

Aim: Write a program in C to input & displays the information of


students using structures.

Code-
#include <stdio.h>
#include <conio.h>

struct student
{
char firstName[50];
int roll;
float marks;
} s[10];

int main()
{
int i, n;
printf("Enter number of students:");
scanf("%d", &n);
printf("Enter information of students:\n");

// storing information
for (i = 0; i < n; ++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 marks: ");
scanf("%f", &s[i].marks);
}
printf("\n\nDisplaying Information:\n\n");

// displaying information
for (i = 0; i < n; ++i)
{
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
getch();
return 0;
}
Output-
Experiment – 7.4

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


operations depending on whether the operation requires one or two
matrices:
Addition of two matrices
Subtraction of two matrices
Trace of a matrix
Transpose of a matrix
Product of two matrices.

Code-
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;
void display(int[][3]);
int main(){
int c;
void func1();
void func2();
void func3();
void func4();
void func5();
system(“cls”);
printf(“\n- : Matrix Manipulation Functions (for 3 X 3 Matrix) : -“);
printf(“\n “);
printf(“\n Matrix Addition : 1”);
printf(“\n Matrix Subtraction : 2”);
printf(“\n Matrix Multiplication : 3”);
printf(“\n Find Transpose Matrix : 4”);
printf(“\n Matrix is Symmetric or not : 5”);
printf(“\n Enter Your Choice : “);
scanf(“%d”, &c);
switch I
{
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
case 4:
func4();
break;
case 5:
func5();
break;
default:
printf(“\nInvalid Choice”);
}
getch();
return 0;
}

void func1()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void addition(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
addition(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Addition (Result): - \n”);
display(z);
}
void getmatrix(int t[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf(“Enter element [%d][%d] : “, I, j);
scanf(“%d”, &t[i][j]);
}
}
}
void addition(int p[][3], int q[][3], int r[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++) r[i]
[j] = p[i][j] + q[i][j];
}
}
void func2()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void subtraction(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
subtraction(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Subtraction (Result): - \n”);
display(z);
}
void subtraction(int p[3][3], int q[3][3], int r[3][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++) r[i]
[j] = p[i][j] – q[i][j];
}
}
void func3()
{
int x[3][3], y[3][3], z[3][3];
void getmatrix(int[][3]);
void multiplication(int[][3], int[][3], int[][3]);
system(“cls”);
getmatrix(x);
getmatrix(y);
multiplication(x, y, z);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Matrix 2: - \n”);
display(y);
printf(“\n - : Matrix Multiplication (Result): - \n”);
display(z);
}
void multiplication(int p[][3], int q[3][3], int r[3][3])
{
int I, j, k;
for (I = 0; I < 3; i++)
//condition i< total row of matrix1
{
for (j = 0; j < 3; j++)
//condition i< total col of matrix1 or//condition i< total row of
matrix2
{
r[i][j] = 0;
for (k = 0; k < 3; k++) //condition i< total col of matrix2 r[i]
[j] = r[i][j] + (p[i][j] * q[j][k]);
}
}
}
void func4()
{
int x[3][3], y[3][3];
void getmatrix(int[][3]);
void transpose(int[][3], int[][3]);
system(“cls”);
getmatrix(x);
transpose(x, y);
printf(“\n - : Matrix 1: - \n”);
display(x);
printf(“\n - : Transpose Matrix : - \n”);
display(y);
}
void transpose(int p[][3], int q[][3])
{ int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
q[i][j] = p[j][i];
}
}
void func5()
{
int x[3][3], y[3][3];
void getmatrix(int[][3]);
void transpose(int[][3], int[][3]);
int symmetric(int[][3], int[][3]);
system(“cls”);
getmatrix(x);
transpose(x, y);
if (symmetric(x, y) == 1) printf(“\
nMatrix is Symmetric”);
else
printf(“\nMatrix is Not Symmetric”);
}
int symmetric(int p[][3], int q[][3])
{
int I, j;
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (q[i][j] != p[i][j])
return 0;
}
}
return 1;
}
void display(int m[][3]){
int I, j;
printf(“\n\n”);
for (I = 0; I < 3; i++)
{
for (j = 0; j < 3; j++)
printf(“%d “, m[i][j]);
printf(“\n”);
}
}
Output-
Enter element [0] l
[0] Enter element 2
[0][i] Enter 3
element [0][2]
Enter element [1] 5ñ
0] 7
Enter element [1] 8
[1] 9
Enter element [1] 0
[2] l
Enter element [2] 2
[0] 3 8 ' c:\Users\soura\Documents\Basic program'
Enter element [2]
[1]
Enter element [2]
[2]
Enter element [0]
[0]
Enter element [0]
[1]
EnterMatrix
element [0] 9
[2] l:
Enter element [1]
0]
Enter element
9Enter element
Enter element
EnterMatrix
element
2:
Enter element [2]
[2]

flat r ix Flu lt tip ie at ion ( Resu lt)

30 72
840 lo8
£8 135 0
Experiment – 8.1

Aim: Write a C program to input and print array elements using


pointer.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int arr[40];
int N, i;
int *ptr = arr;

printf("Enter size of array: ");


scanf("%d", &N);

printf("Enter elements in array:\n");


for (i = 0; i < N; i++)
{
scanf("%d", ptr); ptr+
+;
}

ptr = arr;

printf("Array elements: ");


for (i = 0; i < N; i++)
{
printf("%d, ", *ptr);
ptr++;
}
getch();
return 0;
}
Output-
Experiment – 8.2

Aim: Write a C program to swap two arrays using pointers.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

void inputArray(int *arr, int size);


void printArray(int *arr, int size);
void swapArray(int *sourceArr, int *destArr, int size);

int main()
{
int sourceArr[40];
int destArr[40];
int size;
printf("Enter size of array: ");
scanf("%d", &size);

printf("Enter %d elements in source array: ", size);


inputArray(sourceArr, size);

printf("Enter %d elements in destination array: ", size);


inputArray(destArr, size);

printf("\n\nSource array before swapping: ");


printArray(sourceArr, size);

printf("\nDestination array before swapping: ");


printArray(destArr, size);

swapArray(sourceArr, destArr, size);

printf("\n\nSource array after swapping: ");


printArray(sourceArr, size);

printf("\nDestination array after swapping: ");


printArray(destArr, size);

getch();
return 0;
}

void inputArray(int *arr, int size)


{
int *arrEnd = (arr + (size - 1));
while (arr <= arrEnd)
scanf("%d", arr++);
}

void printArray(int *arr, int size)


{
int *arrEnd = (arr + (size - 1));
while (arr <= arrEnd)
printf("%d, ", *(arr++));
}

void swapArray(int *sourceArr, int *destArr, int size)


{

int *sourceArrEnd = (sourceArr + (size - 1));


int *destArrEnd = (destArr + (size - 1));
while (sourceArr <= sourceArrEnd && destArr <= destArrEnd)
{
*sourceArr ^= *destArr;
*destArr ^= *sourceArr;
*sourceArr ^= *destArr;
sourceArr++; destArr+
+;
}
}
Output-
Experiment – 8.3

Aim: Write a programme to count the no of Lowercase, Uppercase


numbers and special Characters present in the contents of File.

Code-
#include <stdio.h>
#include <conio.h>
using namespace std;

int main()
{
int upper = 0, lower = 0, num = 0, special = 0;
FILE *fp;
char c;
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Could not open file test.c");
return 1;
}
while (1)
{
c = fgetc(fp);
if (c == EOF)
break;
if (c >= 'A' && c <= 'Z')
upper++;
else if (c >= 'a' && c <= 'z')
lower++;
else if (c >= '1' && c <= '9')
num++;
else
special++;
}
fclose(fp);
printf("\nUpper case letters: %d", upper);
printf("\nLower case letters: %d", lower);
printf("\nnumbers: %d", num); printf("\
nSpecial characters: %d", special); getch();
return 0;
}
Output-
Experiment – 8.4

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.

Code-
#include <stdio.h>
#include <conio.h>

/* random record description - could be anything */


struct rec
{
int x;
char Name[10];
};

/* writes and then reads 10 arbitrary records


from the file "junk". */
int main()
{
int i, j;
FILE *f;
struct rec r;

/* create the file of 3 records */


f = fopen("junk", "w");
if (!f)
return 1;
for (i = 1; i <= 3; i++)
{
r.x = i;
scanf("%s", &r.Name);
fwrite(&r, sizeof(struct rec), 1, f);
}
fclose(f);

/* read the 3 records */


f = fopen("junk", "r");
if (!f)
return 1;
for (i = 1; i <= 3; i++)
{
fread(&r, sizeof(struct rec), 1, f);
printf("%d\t", r.x);
printf("%s\n", r.Name);
}
fclose(f); printf("\
n");

/* use fseek to read 2th record,


change it, and write it back */
f = fopen("junk", "r+");
if (!f)
return 1;
fseek(f, sizeof(struct rec) * 1, SEEK_SET);
fread(&r, sizeof(struct rec), 1, f);
scanf("%d", &r.x);
scanf("%s", &r.Name);

fseek(f, sizeof(struct rec) * 1, SEEK_SET);


fwrite(&r, sizeof(struct rec), 1, f);
fclose(f);
printf("\n");

/* read the 3 records to insure


2th record was changed */
f = fopen("junk", "r");
if (!f)
return 1;
for (i = 1; i <= 3; i++)
{
fread(&r, sizeof(struct rec), 1, f);
printf("%d\t", r.x);
printf("%s\n", r.Name);
}
fclose(f);
getch();
return 0;
}
Output -
Experiment – 8.5
Aim: Write a program to copy one file to other, use command line
arguments.

Code-
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])
{ FILE *fp1, *fp2;
char ch;
if (argc != 3)
{
printf("\n insufficient arguments.\n");
exit(0);
}
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "w");
if (fp1 == NULL || fp2 == NULL)
{
printf("\n File not created.\n");
exit(0);
}
if (NULL != fp1)
{
fseek(fp1, 0, SEEK_END);
int size = ftell(fp1);
if (0 == size)
{
printf("File is empty.\n");
exit(0);
}
}
while (!feof(fp1))
{
ch = fgetc(fp1);
fputc(ch, fp2);
}
printf("\n File successfully copied ");
fclose(fp1);
fclose(fp2);
}
Output-

You might also like