0% found this document useful (0 votes)
2 views

ADITYAAGARWALcode

The document contains multiple C programming exercises authored by Aditya Agarwal, focusing on array manipulation, matrix operations, string handling, and basic input/output. Each section includes a specific task such as finding the position of an element in an array, inserting and deleting elements, sorting, and performing operations on matrices. The document showcases a variety of programming concepts and techniques relevant to introductory computer science education.

Uploaded by

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

ADITYAAGARWALcode

The document contains multiple C programming exercises authored by Aditya Agarwal, focusing on array manipulation, matrix operations, string handling, and basic input/output. Each section includes a specific task such as finding the position of an element in an array, inserting and deleting elements, sorting, and performing operations on matrices. The document showcases a variety of programming concepts and techniques relevant to introductory computer science education.

Uploaded by

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

//WAP to find position of a given number in an array.

(-1 for not found)


//ADITYA AGARWAL CSE 2
#include<stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[50],i,key,n;
printf("Enter the no. of elements you want to enter : \n");
scanf("%d",&n);

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


for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element to search\n");
scanf("%d",&key);
for (i=0;i<n;i++)
{
if (a[i]==key)
{
printf("Element found at location %d",i);
break;
}
}

if (i==n)
{
printf("-1");
}
printf("\n\n\n\n");

//WAP to insert a new number in an array at any position.


//ADITYA AGARWAL CSE 2
// C Program to Insert an element
// at a specific position in an Array
#include <stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int arr[100] ;
int i, x, pos, n;
printf("\nEnter the size of the array : ");
scanf("%d",&n);
printf("\nEnter the elements in tha array : ");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);}
// initial array of size 10
//for (i = 0; i < n; i++)
// arr[i] = i + 1;
// print the original array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
// element to be inserted
printf("\nEnter the number you want to enter : ");
scanf("%d",&x);
// position at which element
// is to be inserted
printf("\nEnter the position of the array : ");
scanf("%d",&pos);
// increase the size by 1
n++;
// shift elements forward
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n\n\n\n");
return 0;
}

//WAP to delete any number from an array.


//ADITYA AGARWAL CSE 2
#include<stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
// declaration of the int type variable
int arr[50];
int pos, i, num;// declare int type variable
printf("\nEnter the number of elements in an array: \n ");
scanf("%d", &num);
printf("\nEnter %d elements in array: \n", num);

// use for loop to insert elements one by one in array


for(i=0;i<num;i++)
{printf("arr[%d]=",i);
scanf("%d",&arr[i]);
}
// enter the position of the element to be deleted
printf("Define the position of the array element where you want to delete: \n ");
scanf("%d",&pos);

// check whether the deletion is possible or not


if(pos>=num+1)
{
printf("\nDeletion is not possible in the array.");
}
else
{
// use for loop to delete the element and update the index
for(i=pos-1;i<num-1;i++)
{
arr[i] = arr[i+1];// assign arr[i+1] to arr[i]
}

printf("\nThe resultant array is: \n");

// display the final array


for(i=0;i<num-1;i++)
{
printf("arr[%d]=",i);
printf("%d\n",arr[i]);
}
}
printf("\n\n\n");
return 0;
}

//WAP to sort an array in ascending order.


//ADITYA AGARWAL CSE 2
#include <stdio.h>
void main (){
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int num[20];
int i, j, a, n;
printf("enter number of elements in an array");
scanf("%d", &n);
printf("Enter the element ");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:");
for (i = 0; i < n; ++i){
printf("%d ", num[i]);
}
printf("\n\n\n");

//WAP to enter data in 2D array and print on screen.


//ADITYA AGARWAL CSE 2
#include <stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[10][10], i, j, r, c ;
printf(" Enter the Numbers of Row : ");
scanf("%d",&r);
printf(" Enter the Number of Coloumn : ");
scanf("%d",&c);

printf("\n Enter the Element of Matrix : \n");


for ( i = 0 ; i < r ; i++ )
{

for ( j = 0 ; j < c ; j++ )


{

printf("\n Enter the Element [%d] [%d] : ",i, j);


scanf("%d",&a[i][j]);

printf("\n Element in the Matrix are : \n");


for ( i = 0 ; i < r ; i++ )
{

for ( j = 0 ; j < c ; j++ )


{

printf("\t %d ", a[i][j]);

}
printf(" \n ");

}
printf("\n\n\n");
return 0 ;

//WAP to add two matrices in 2-D array


//ADITYA AGARWAL CSE 2
#include <stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[3][3],b[3][3],c[3][3],i,j;
printf("\nenter first matrix : \n");
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("enter second matrix : \n");
for (i=0;i<=2;i++)
{

for (j=0;j<=2;j++)
{

scanf("%d",&b[i][j]);
}
}

printf("\nThe first matrix is : \n");


for (i=0;i<=2;i++)
{

for (j=0;j<=2;j++)
{

printf("%d\t",a[i][j]);
}
printf("\n");
}

printf("\nThe second matrix is : \n");


for (i=0;i<=2;i++)
{

for (j=0;j<=2;j++)
{

printf("%d\t",b[i][j]);
}
printf("\n");
}

printf("\nThird matrix is : \n");


for (i=0;i<=2;i++)
{

for (j=0;j<=2;j++)
{

c[i][j] = a[i][j] + b[i][j];


printf("%d\t",c[i][j]);
}
printf("\n");
}

printf("\n\n\n");
}

//WAP to multiply two matrices in 2-D array.


//ADITYA AGARWAL CSE 2
#include<stdio.h>
#define N 50
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[N][N],b[N][N],c[N][N],i,j,k,sum,m,n,p,q;
printf("Enter rows and column for first matrix : \n");
scanf("%d %d",&m,&n);
printf("\nEnter first matrix : \n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\nEnter rows and column for second matrix : \n");


scanf("%d %d",&p,&q);
printf("\nEnter second matrix :\n");
for (i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("\nFirst matrix is :\n");


for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

printf("\nSecond matrix is :\n");


for (i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

if (n!=p)
{
printf("can not multiply");
}
else
{
for (i=0;i<m;i++)
{
for (j=0;j<q;j++)
{
sum=0;
for (k=0;k<m;k++)
{
sum = sum + (a[i][k] * b[k][j]);
}
c[i][j] = sum;
}
}

printf("multiplication is :\n");
for (i=0;i<m;i++)
{
for (j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
return 0;
}

//WAP to find transpose of a Matrix.


//ADITYA AGARWAL CSE 2

#include<stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[2][3],i,j;
printf("enter elements of Marrix : \n");
for (i=0;i<2;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\nMatrix is :\n");
for (i=0;i<2;i++)
{
for (j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

printf("\nTranspose of matrix :\n");


for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
printf("\n\n\n");
return 0;
}

//WAP to print sum of all diagonal elements of a matrix.


//ADITYA AGARWAL CSE 2

#include<stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int num[20][20],i,j,sum=0,r,c;
printf("\n\nEnter Number of Row And Column in a Matrix : ");
scanf("%d%d",&r,&c);
printf("\nEnter Elements of Matrix ....\n");
for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
printf("Enter elements in pocket [%d] [%d] \t ",i,j);
scanf("%d",&num[i][j]);
}
}

printf("Matrix is ........\n");
for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
printf("%d\t",num[i][j]);
}
printf("\n");
}

for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
if (i==j)
{
sum = sum + num[i][j];

}
}
}
printf("\n\n");
printf("Sum of right diagonal matrix is : %d",sum);

printf("\n\n");
return 0;
}

//WAP to enter any string and display on screen


//ADITYA AGARWAL CSE 2
#include<stdio.h>

int main()

{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char arr[100];

printf("\nEnter a string : ");

scanf("%s",&arr);

printf("\nThe given string : %s",arr);


printf("\n\n\n");
return 0;

//WAP to find length of string.(with string.h


//ADITYA AGARWAL CSE 2
#include <stdio.h>
#include <string.h>

int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char Str[1000];
int i;

printf("Enter the String: ");


scanf("%s", Str);

for (i = 0; Str[i] != '\0'; ++i);

printf("Length of Str is %d", i);


printf("\n\n\n");
return 0;
}

//WAP to copy one string to another string (with string.h)


//ADITYA AGARWAL CSE 2

#include<stdio.h>
#include<string.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char str1[70], str2[70];
printf("Enter the string: ");
gets(str1);
printf("\nString 1 = %s", str1);
strcpy(str2, str1);
printf("\nString 2 = %s", str2);
printf("\n\n\n");
return 0;
}

// WAP to compare two strings (with string.h)


//ADITYA AGARWAL CSE 2

#include<stdio.h>
#include<string.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char str1[20];// declaration of char array
char str2[20];// declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same\n\n\n");
printf("\n\n\n");
return 0;
}

//WAP to reverse a given string. (with string.h


//ADITYA AGARWAL CSE 2

#include <stdio.h>
#include <string.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char str[40];// declare the size of character string
printf (" \n Enter a string to be reversed : ");
gets("%s",str);
// use strrev() function to reverse a string
printf(" \n After the reverse of a string: %s ",strrev(str));
return 0;
}

// WAP to check whether a given string is palindrome or not


//ADITYA AGARWAL CSE 2

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

int main(){
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);

length = strlen(string1);

for(i=0;i < length ;i++){


if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}

if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}

printf("\n\n\n");
return 0;
}

// WAP a create function display a simple message


//ADITYA AGARWAL CSE 2
#include<stdio.h>
void PrintMessage()
{
printf("This is user defined function");
}
void main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
printf("This is main Function! \n");
PrintMessage();
printf("\n\n");
}

//. WAP to create a function to swap two numbers using call by value.
//ADITYA AGARWAL CSE 2

void swap(int, int);

int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int x, y;

printf("Enter the value of x and y\n");


scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(x, y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);


printf("\n\n\n");
return 0;
}

void swap(int a, int b)


{
int temp;

temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}

//Q37
//ADITYA AGARWAL CSE 2
#include<stdio.h>
int biggest(int, int, int);// function prototype

int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a,b,c;

printf("Enter 3 integer numbers\n");


scanf("%d%d%d",&a, &b,&c);
//function call biggest(a, b, c)
printf("Biggest of %d, %d and %d is %d\n", a, b,c,biggest(a,b,c));

return 0;
}

// function definition
int biggest(int x, int y,int z)
{
if(x > y && x > z)
{
return x;
}
else
{
if(y > z)
return y;
else
return z;
}
}

//WAP to create function that find greatest of 10 numbers in array.


//ADITYA AGARWAL CSE 2
#include <stdio.h>
int main() {
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}
}
printf("Greatest of ten numbers is %d", greatest);
printf("\n\n\n");
return 0;
}

//WAP to find power of a number without math.h .


//ADITYA AGARWAL CSE 2

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

int Pow(int X, int Y) {

int power = 1, i;

for (i = 1; i <= Y; ++i) {


power = power * X;
}

return power;

int main() {
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");

long long int base, exponent;


printf("Enter Base: ");
scanf("%d", &base);

printf("Enter Power: ");


scanf("%d", &exponent);

printf("%d ^ %d = %d", base, exponent, Pow(base, exponent));


printf("\n\n\n");
}

// WAP to understand basic use of pointers


//ADITYA AGARWAL CSE 2
#include<stdio.h>
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("\n\n%d\n", *pc); // Ouptut: 1
printf("%d\n\n", c); // Output: 1
printf("\n\n\n");
return 0;
}

//WAP to implement call by reference for swapping of two numbers.


//ADITYA AGARWAL CSE 2
#include<stdio.h>

void swap(int*, int*);

int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int x, y;

printf("Enter the value of x and y\n");


scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);


printf("\n\n\n");
return 0;
}

void swap(int *a, int *b)


{
int temp;

temp = *b;
*b = *a;
*a = temp;
}

//WAP to Fibonacci series up to 20 using recursive numbers.


//ADITYA AGARWAL CSE 2
#include <stdio.h>

// fibonacci() funtion definition


int fibonacci(int num)
{
// first base condition check
if (num == 0)
{
return 0; // retuning 0, if condition meets
}
// second base condition check
else if (num == 1)
{
return 1; // returning 1, if condition meets
}
// else calling the fibonacci() function recursively till we get to the base
conditions
else
{
return fibonacci(num - 1) + fibonacci(num - 2); // recursively calling the
fibonacc() function and then adding them
}
}

int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int num; // variable to store how many elements to be displayed in the series
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num); // taking user input

for (int i = 0; i < num; i++)


{
printf("%d, ", fibonacci(i)); // calling fibonacci() function for each
iteration and printing the returned value
}
printf("\n\n\n");
return 0;
}

//WAP to implement Ackerman function using recursion.


//ADITYA AGARWAL CSE 2

#include<stdio.h>
int A(int m, int n);
int main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
printf("\n\n\n");
return 0;
}

int A(int m, int n)


{
if(m==0)
return n+1;
else if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
}

//WAP for user defined data type namely Student and implement it using Structure
//ADITYA AGARWAL CSE 2

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main() {
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
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 marks: ");
scanf("%f", &s[i].marks);
}
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].marks);
printf("\n");
}
return 0;
}

//WAP for user defined data type namely Student and implement it using Union
//ADITYA AGARWAL CSE 2
#include<stdio.h>
union student{
int rollno;
char name[20];
int marks;
};
int main(){

printf("\n\n\n\nName - Aditya Agarwal");


printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
union student s1;
printf("Enter student name \n");
gets(s1.name);
printf("Student name \n");
puts(s1.name);
printf("Enter the rollno. \n");
scanf("%d",&s1.rollno);
printf("Rollno. is : %d \n",s1.rollno);
printf("Enter marks \n");
scanf("%d",&s1.marks);
printf("Marks = %d \n",s1.marks);
}

//WAP to create an array of structure.


//ADITYA AGARWAL CSE 2
#include<stdio.h>
struct student
{
char name[20];
int id;
float marks;
};
void main()
{
printf("\n\n\n\nName - Aditya Agarwal");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25006\n");
struct student s1,s2,s3;
int dummy;
printf("Enter the name, id, and marks of student 1 ");
scanf("%s %d %f",s1.name,&s1.id,&s1.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 2 ");
scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 3 ");
scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
scanf("%c",&dummy);
printf("Printing the details....\n");
printf("%s %d %f\n",s1.name,s1.id,s1.marks);
printf("%s %d %f\n",s2.name,s2.id,s2.marks);
printf("%s %d %f\n",s3.name,s3.id,s3.marks);

printf("\n\n\n\n");
}

//47
//WAP to read a simple file using file handling.
//ADITYA AGARWAL CSE 2

// C program to implement
// the above approach
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Driver code
int main()
{
printf("\n\n\n\nName - Ayush Thakur");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25033\n");
FILE* ptr;
char ch;

// Opening file in reading mode


ptr = fopen("/home/aditya/Desktop/coding.txt", "r");

if (NULL == ptr) {
printf("file can't be opened \n");
}

printf("content of this file are \n");

// Printing what is written in file


// character by character using loop.
do {
ch = fgetc(ptr);
printf("%c", ch);

// Checking if character is not EOF.


// If it is EOF stop reading.
} while (ch != EOF);

// Closing the file


fclose(ptr);
printf("\n\n\n");
return 0;
}
//48
//WAP to write data in file.
//ADITYA AGARWAL CSE 2
#include <stdio.h>
#include <stdlib.h>

int main() {
printf("\n\n\n\nName - Ayush Thakur");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25033\n");
char sentence[1000];

// creating file pointer to work with files


FILE *fptr;

// opening file in writing mode


fptr = fopen("/home/aditya/Desktop/coding.txt", "w");

// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
printf("\n\n\n");
return 0;
}

//WAP to print number of characters, integer and float data in a given file.

#include <stdio.h>
#include<stdlib.h>
int main() {
printf("\n\n\n\nName - Ayush Thakur");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25033\n");
FILE *fptr;
char filename[100], c;
int num_of_characters = 0, num_of_integers = 0, num_of_floats = 0;
fptr = fopen("/home/aditya/Desktop/coding.txt", "r");
c = fgetc(fptr);
while (c != EOF) {
num_of_characters++;
if (c >= '0' && c <= '9')
num_of_integers++;
if (c == '.')
num_of_floats++;
c = fgetc(fptr);
}
fclose(fptr);
printf("Number of characters = %d\n",num_of_characters);
printf("Number of integers = %d\n",num_of_integers);
printf("Number of floats = %d\n",num_of_floats);
printf("\n\n");
return 0;
}

//Q50
//WAP to copy contents of one file into another file.
//ADITYA AGARWAL CSE 2
#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
printf("\n\n\n\nName - Ayush Thakur");
printf("\nClass - CSE - 2");
printf("\nRoll No. - 25033\n");
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;
}

You might also like