0% found this document useful (0 votes)
67 views21 pages

Experiment 1: Aim: Code

The document describes 3 experiments related to file handling in C programming: 1. The first experiment involves reading data from a file and storing it in an array. 2. The second experiment swaps the contents of two arrays by reading and writing to files. 3. The third experiment copies the contents from one file to another.

Uploaded by

Aman
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)
67 views21 pages

Experiment 1: Aim: Code

The document describes 3 experiments related to file handling in C programming: 1. The first experiment involves reading data from a file and storing it in an array. 2. The second experiment swaps the contents of two arrays by reading and writing to files. 3. The third experiment copies the contents from one file to another.

Uploaded by

Aman
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/ 21

EXPERIMENT 1

Aim:

Code:

include<stdio.h>

int main()

intn,p,i=0,digit,large=0;//variables

printf("enter any integer number:\n");

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

p=n;//storing original value entered by user to variable p

while(n>0)

digit=n%10;//finding digit

if(digit>large)//checking condition for large


large=digit;

n=n/10;//dividing number by 10

printf("Largest digit in number %d=%d",p,large);//displaying the


largest number

return 0;

Output:

enter any integer number:

7865

Largest digit in number 7865=8


EXPERIMENT

Aim:

Code:

#include<stdio.h>

int main()

intn,p,i=0,digit,small=9;//variables

printf("enter any integer number:\n");

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

p=n;//storing original value entered by user to variable p

while(n>0)

digit=n%10;//finding digit

if(digit==0)

{
small=0;//assigning 0 to small

if(digit<=small)

small=digit;//assigning digit to small

n=n/10;//dividing number by 10

printf("Smallest digit in number %d=%d",p,small);//displaying the


largest number

return 0;

Output:

enter any integer number:

76545412
Smallest digit in number 76545412=1

#include <stdio.h>

int count_3s(int n)
{

int count = 0;

while (n > 0)

if (n % 10 == 3)

count++;

n = n / 10;

return count;

intcount_in_range(int n)

int count = 0 ;

for (inti = 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
Enter the end value : 100
Total occurrences of 3 from 0 to 100 is 20

#include<stdio.h>
int main(){
/* 2D array declaration*/
intdisp[2][3];
/*Counter variables for the loop*/
inti, j;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("Enter value for disp[%d][%d]:",i, j);
scanf("%d",&disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d ",disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return0;
}

Enter value fordisp[0][0]:1


Enter value fordisp[0][1]:2
Enter value fordisp[0][2]:3
Enter value fordisp[1][0]:4
Enter value fordisp[1][1]:5
Enter value fordisp[1][2]:6
TwoDimensional array elements:
123
456

Experiment: 8.1

Aim:
Code:
#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

intmain()

intarr[MAX_SIZE];

int N,i;

int*ptr=arr;// Pointer to arr[0]

printf("Enter size of array: ");

scanf("%d",&N);

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

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

// (ptr + i) is equivalent to &arr[i]

scanf("%d",(ptr+i));

printf("Array elements: ");

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

// *(ptr + i) is equivalent to arr[i]

printf("%d, ",*(ptr+i));

return0;

}
Output:

Output
Enter size of array: 10
Enter elements in array:
1
2
3
4
5
6
7
8
9
10
Array elements: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

Experiment: 8.2

Aim:
Code:

/**
* C program to swap two arrays using pointers
*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

/* Function declarations */
voidinputArray(int*arr,int size);
voidprintArray(int*arr,int size);
voidswapArray(int*sourceArr,int*destArr,int size);

intmain()
{
intsourceArr[MAX_SIZE];
intdestArr[MAX_SIZE];

int size;

// Input array size


printf("Enter size of array: ");
scanf("%d",&size);

// Input elements of destination array


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

// Input element of destination array


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

/*
* Print elements of both arrays before swapping
*/
printf("\n\nSource array before swapping: ");
printArray(sourceArr, size);

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


printArray(destArr, size);

/* Swap elements of both arrays. */


swapArray(sourceArr,destArr, size);

/*
* Print elements of both arrays after swapping
*/
printf("\n\nSource array after swapping: ");
printArray(sourceArr, size);

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


printArray(destArr, size);

return0;
}

/**
* Function used to read input from user in an array.
*
* @arr Pointer to array to store input
* @size Size of the array
*/
voidinputArray(int*arr,int size)
{
// Pointer to last element of array.
int*arrEnd=(arr+(size -1));

// Input elements in array using pointer


while(arr<=arrEnd)
scanf("%d",arr++);
}

/**
* Function used to print elements of array.
*
* @arr Pointer to array, which is to print.
* @size Size of the array
*/
voidprintArray(int*arr,int size)
{
// Pointer to last element of array.
int*arrEnd=(arr+(size -1));

// Print elements of array one by one


while(arr<=arrEnd)
printf("%d, ",*(arr++));
}
/**
* Function to swap elements of two arrays.
*
* @sourceArr Pointer to source array to swap.
* @destArr Pointer to destination array to swap.
* @size Size of array.
*/
voidswapArray(int*sourceArr,int*destArr,int size)
{
// Pointer to last element of source array
int*sourceArrEnd=(sourceArr+(size -1));

// Pointer to last element of destination array


int*destArrEnd=(destArr+(size -1));

/*
* Swap individual element of both arrays
*/
while(sourceArr<=sourceArrEnd&&destArr<=destArrEnd)
{
*sourceArr^=*destArr;
*destArr^=*sourceArr;
*sourceArr^=*destArr;

// Increment source array to point to next element


sourceArr++;

// Increment destination array to point to next element


destArr++;
}
}

}
}

Output
Enter size of array: 10
Enter 10 elements in source array: 1 2 3 4 5 6 7 8 9 10
Enter 10 elements in destination array: 10 20 30 40 50 60 70 80 90 100

Source array before swapping: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,


Destination array before swapping: 10, 20, 30, 40, 50, 60, 70, 80, 90,
100,
Source array after swapping: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Destination array after swapping: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Experiment: 8.3

Aim:

Code:
#include<stdio.h>
#include <string.h>

intmain()

    chars[1000];

    inti,alphabets=0,digits=0,specialcharacters=0;

    printf("Enter  the string : ");

    gets(s);

    

    for(i=0;s[i];i++)  

    {

        if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))

          alphabets++;

        elseif(s[i]>=48&&s[i]<=57)

         digits++;

        else

         specialcharacters++;
 

    

    printf("Alphabets = %d\n",alphabets);

    printf("Digits = %d\n",digits);

    printf("Special characters = %d",specialcharacters);

    
 

    return0;

}
Output:

1 Enter  the string:hello1234!@#$%

2 Alphabets=5

3 Digits=4

4 Special characters=5

Experiment: 8.4

Aim:

Code:
#include<stdio.h>
struct stud
{
      intrno;
      float per;
      char name[20],add[20];
}s;
int main()
{
      FILE *fp;
      fp=fopen("student.txt","w");
      printf("Enter record of student:\n\n");
      printf("\nEnter student number : ");
      scanf("%d",&s.rno);
      printf("\nEnter name of student: ");
      scanf("%s",s.name);
      printf("\nEnter student address : ");
      scanf("%s",s.add);
      printf("\nEnter percentage of student : ");
      scanf("%f",&s.per);
      fprintf(fp,"%d\n%s\n%s\n%f",s.rno,s.name,s.add,s.per);
      printf("\nRecord stored in file...");
      fclose(fp);
      return 0;
}

1. #include<stdio.h>

2. int main(){

3. char name[50];

4. intmarks,i,n;

5. printf("Enter number of students: ");

6. scanf("%d",&n);

7. FILE *fptr;

8. fptr=(fopen("C:\\student.txt","a"));

9. if(fptr==NULL){

10. printf("Error!");

11. exit(1);

12. }
13. for(i=0;i<n;++i){

14. printf("For student%d\nEnter name: ",i+1);

15. scanf("%s",name);

16. printf("Enter marks: ");

17. scanf("%d",&marks);

18. fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);

19. }

20. fclose(fptr);

21. return0;

22.}

Output:
Experiment: 8.5

Aim:

Code:

#include <stdio.h>

#include <stdlib.h> // For exit()

int main()

FILE *fptr1, *fptr2;

char filename[100], c;

printf("Enter the filename to open for reading \n");

scanf("%s", filename);

// Open one file for reading

fptr1 = fopen(filename, "r");

if (fptr1 == NULL)

printf("Cannot open file %s \n", filename);

exit(0);

}
printf("Enter the filename to open for writing \n");

scanf("%s", filename);

// Open another file for writing

fptr2 = fopen(filename, "w");

if (fptr2 == NULL)

printf("Cannot open file %s \n", filename);

exit(0);

// Read contents from file

c = fgetc(fptr1);

while (c != EOF)

fputc(c, fptr2);

c = fgetc(fptr1);

printf("\nContents copied to %s", filename);

fclose(fptr1);

fclose(fptr2);

return 0;

Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt

You might also like