0% found this document useful (0 votes)
24 views38 pages

Aman CO File

The document contains 21 code snippets demonstrating various C programming concepts like: 1) Finding sum and average of two numbers 2) Finding greatest of 10 numbers 3) Calculating simple interest 4) Printing patterns of stars 5) Checking if a number is prime 6) Sum and reversal of numbers 7) Conversions between decimal and binary 8) Implementing switch case 9) Generating Fibonacci sequence 10) Exponential functions 11) Linear and binary search 12) Sorting arrays using bubble, selection, insertion sort 13) Factorial using recursion 14) String length and counting vowels 15) Checking palindromes

Uploaded by

amansinghas2911
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)
24 views38 pages

Aman CO File

The document contains 21 code snippets demonstrating various C programming concepts like: 1) Finding sum and average of two numbers 2) Finding greatest of 10 numbers 3) Calculating simple interest 4) Printing patterns of stars 5) Checking if a number is prime 6) Sum and reversal of numbers 7) Conversions between decimal and binary 8) Implementing switch case 9) Generating Fibonacci sequence 10) Exponential functions 11) Linear and binary search 12) Sorting arrays using bubble, selection, insertion sort 13) Factorial using recursion 14) String length and counting vowels 15) Checking palindromes

Uploaded by

amansinghas2911
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/ 38

1. Program to find the sum and average of two numbers.

#include <stdio.h>
int main(){
int n1,n2;
printf("Enter The Two Numbers : ");
scanf("%d%d",&n1,&n2);
float sum = n1+ n2;
float average = sum/2;
printf("Sum Of %d and %d is %.2f\n",n1,n2,sum);
printf("Average Of %d and %d is %.2f\n",n1,n2,average);
return 0;
}

OUTPUT :

2. Program to find the greatest of 10 numbers.

#include <stdio.h>
int main(){
int n[10];
for(int i=0;i<10;i++){
printf("Enter %d Number : ",i+1);
scanf("%d",&n[i]);
}
int num = n[0];
for(int i=0;i<10;i++){
if(num<=n[i]){
num = n[i];
}
}
printf("Greatest Number Is %d\n",num);
return 0;
}

OUTPUT :

3. Program to find Simple Interest.

#include <stdio.h>
int main(){
float principal_amount,time,interest_rate;
printf("Enter Principal Amount : ");
scanf("%f",&principal_amount);
printf("Enter Interest Rate : ");
scanf("%f",&interest_rate);
printf("Enter Time Period(In Years) : ");
scanf("%f",&time);
float SI = (principal_amount*interest_rate*time)/100;
printf("Simple Interest Is %.2f\n",SI);
float total_amount = SI + principal_amount;
printf("Total Amount Is %.2f\n",total_amount);
return 0;
}
OUTPUT :

4.Program to print the following pattern (full pyramid of '*')


*
***
*****
*******
*********

#include <stdio.h>
int main(){
int n;
printf("Enter The Number Of Lines Of Pattern : ");
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<2*n-1;j++){
if(j>=(n-1-i)&&j<=(n-1+i)){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
return 0;
}
OUTPUT :

5. Program to find whether the entered number is prime or


not prime.

#include <stdio.h>
#include <math.h>
int main(){
int num,count=0;
printf("Enter The Number : ");
scanf("%d",&num);
for(int i=2;i<sqrt(num);i++){
if(num%i==0){
count+=1;
}
}
if(count==0){
printf("%d Is A Prime Number",num);
}
else{
printf("%d Is Not Prime (Composite) Number",num);
}
return 0;
}

OUTPUT :
6. Program to Sum Of a number.

#include <stdio.h>
int main(){
int num;
printf("Enter A Number : ");
scanf("%d",&num);
int sum=0;
while(num!=0){
sum+= num%10;
num = num/10;
}
printf("Sum Of Number Is : %d\n",sum);
return 0;
}

OUTPUT :

7. Program to Reverse a number.

#include <stdio.h>
int main(){
int num;
printf("Enter A Number : ");
scanf("%d",&num);
int result=0;
while(num!=0){
int rem = num%10;
num = num/10;
result = result*10+rem;
}
printf("Reversed Number : %d\n",result);
return 0;
}

OUTPUT :

8.(a)Program to Convert decimal to binary.


#include <stdio.h>
#include <math.h>
int main(){
int d_num,b_num=0;
printf("Enter Decimal Number : ");
scanf("%d",&d_num);
int i=0;
while(d_num!=0){
int rem = d_num%2;
d_num = d_num/2;
b_num+=rem*pow(10,i);
i++;
}
printf("Binary Number : %d\n",b_num);
return 0;
}

OUTPUT :

8.(b)Program to convert a number from binary to decimal


#include <stdio.h>
#include <math.h>
int main(){
int d_num=0,b_num;
printf("Enter Binary Number : ");
scanf("%d",&b_num);
int i=0;
while(b_num!=0){
int rem = b_num%10;
b_num = b_num/10;
d_num+=rem*pow(2,i);
i++;
}
printf("Decimal Number : %d\n",d_num);
return 0;
}

OUTPUT :

9.Program to implement switch case statement.


#include <stdio.h>
int main() {
int rating;
printf("Enter Your Rating : ");
scanf("%d",&rating);
switch(rating){
case 1:
printf("You Have Given Rating of 1\n");
break;
case 2:
printf("You Have Given Rating of 2\n");
break;
case 3:
printf("You Have Given Rating of 3\n");
break;
case 4:
printf("You Have Given Rating of 4\n");
break;
case 5:
printf("You Have Given Rating of 5\n");
break;
default:
printf("Invalid Rating!");
}
return 0;
}

OUTPUT :

10. Program to generate the Fibonacci sequence (taking input


from user).
#include<stdio.h>
int main(){
int n;
printf("Enter the index : ");
scanf("%d",&n);
int a=0,b=1;
int sum;
for (int i = 0; i <= n; i++)
{
sum=a+b;
printf("%d ",a);
a=b;
b=sum;
}
printf("\n");
return 0;
}
OUTPUT :
11. Program to find exponential function.

#include <stdio.h>
int main(){
int n1,n2;
printf("Enter Number & It's Power : ");
scanf("%d%d",&n1,&n2);
int result = 1;
for(int i=1;i<=n2;i++){
result *= n1;
}
printf("Result Is : %d\n",result);
return 0;
}
OUTPUT :

12. Program to Search a Number From an array using linear


search.

#include <stdio.h>
int main(){
int n;
printf("Enter The Size Of array : ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
printf("Enter %d element Of Array : ",i+1);
scanf("%d",&arr[i]);
}
int num;
printf("Enter The Element To Be Searched : ");
scanf("%d",&num);
int index=0;
for(int i=0;i<n;i++){
if(arr[i]==num){
printf("%d Is Present at Index %d\n",num,i);
index+=1;
}
}
if(index==0){
printf("%d",-1);
}
return 0;
}

OUTPUT :

14. Program to Search a Number From an array using Binary


search.

#include <stdio.h>
int main(){
int n;
printf("Enter The Size Of array : ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
printf("Enter %d element Of Sorted Array : ",i+1);
scanf("%d",&arr[i]);
}
int num;
printf("Enter The Element To Be Searched : ");
scanf("%d",&num);
int low = 0;
int high = n;
int mid = (low+high)/2;
while(low<=high){
if(num==arr[mid]){
printf("%d element Is Present At %d Index",num,mid);
break;
}
else if(num>arr[mid]){
low = mid+1;
mid = (low+high)/2;
}
else if(num<arr[mid]){
high = mid-1;
mid = (low+high)/2;
}
}
return 0;
}
OUTPUT :

15. Program to Sort an array using bubble sort.

#include <stdio.h>
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void display(int arr[], int size)
{
for (int i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n;
printf("Enter Size Of Array : ");
scanf("%d",&n);
int arr[n];
printf("Enter Elements Of an Array : \n");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array: \n");
display(arr, n);
return 0;
}

OUTPUT :
16. Program to Sort an array using Selection sort.

#include <stdio.h>
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx]){
min_idx = j;
}
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
void display(int arr[], int size)
{
for (int i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n;
printf("Enter Size Of Array : ");
scanf("%d",&n);
int arr[n];
printf("Enter Elements Of an Array : \n");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
selectionSort(arr, n);
printf("Sorted array: \n");
display(arr, n);
return 0;
}

OUTPUT :

17. Program to Sort an array using Insertion sort.

#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void display(int arr[], int size)
{
for (int i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n;
printf("Enter Size Of Array : ");
scanf("%d",&n);
int arr[n];
printf("Enter Elements Of an Array : \n");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
insertionSort(arr, n);
printf("Sorted array: \n");
display(arr, n);
return 0;
}

OUTPUT :

18. Program to find factorial of a number using recursion.


#include <stdio.h>
int fac(int n){
if(n==0){
return 1;
}
else{
return fac(n-1)*n;
}
}
int main(){
int n;
printf("Enter The Number : ");
scanf("%d",&n);
printf("Factorial = %d\n",fac(n));
return 0;
}

OUTPUT :

19. Program to find length of the string without using strlen.

#include <stdio.h>
int strlenh(char st[]){
int n=0;
while(st[n]!='\0'){
n++;
}
return n;
}
int main(){
char str[100];
printf("Enter the String :");
scanf("%s",str);
printf("Lenght Of string is : %d\n",strlenh(str));
return 0;
}

OUTPUT :

20. Program to Count number of vowels in a given string.

#include <stdio.h>
int strlenh(char st[]){
int n=0;
while(st[n]!='\0'){
n++;
}
return n;
}
int vowelcount(char st[]){
int n=0;
for(int i=0;i<strlenh(st);i++){ if(st[i]=='a'||st[i]=='e'||st[i]=='i'||st[i]=='o'||
st[i]=='u'||st[i]=='A'||st[i]=='E'||st[i]=='I'||st[i]=='O'||st[i]=='U'){
n++;
}
}
return n;
}
int main(){
char str[100];
printf("Enter the String :");
scanf("%s",str);
printf("Number Of Vowels in string is%d\n",vowelcount(str));
return 0;
}

OUTPUT :

21. Program to Check if a string is a palindrome or not.

#include <stdio.h>
int strlenh(char st[]){
int n=0;
while(st[n]!='\0'){
n++;
}
return n;
}
int palindrome(char st[]){
int n= strlenh(st);
int count=0;
for(int i=0;i<(n/2);i++){
if(st[i]!=st[n-1-i]){
count++;
}
}
if(count==0){
printf("string is a palindrome");
}
else{
printf("string is not a palindrome");
}
}
int main(){
char str[100];
printf("Enter the String :");
scanf("%s",str);
palindrome(str);
return 0;
}

OUTPUT :

22. Program to String Concatenation.

#include <stdio.h>
int strlenh(char st[]){
int n=0;
while(st[n]!='\0'){
n++;
}
return n;
}
void concate(char st1[],char st2[]){
int n1 = strlenh(st1);
int n2 = strlenh(st2);
int i=0,j=0;
for(i=n1;i<n2+n1;i++){
st1[i]=st2[j];
j++;
}
st1[i]='\0';
}
int main(){
char str1[100];
printf("Enter the String :");
scanf("%s",str1);
char str2[100];
printf("Enter the String :");
scanf("%s",str2);
concate(str1,str2);
puts(str1);
return 0;
}

OUTPUT :

23. Program to String Comparison.

#include <stdio.h>
int xstrlen(char st[]){
int n=0;
while(st[n]!='\0'){
n++;
}
return n;
}
void compare(char st1[],char st2[]){
int n1 = xstrlen(st1);
int n2 = xstrlen(st2);
int count=0;
if(n1!=n2){
printf("Strings are Not Equal\n");
}
else if(n1==n2){
for(int i=0;i<n2;i++){
if(st1[i]!=st2[i]){
count++;
}
}
if(count==0){
printf("Strings Are Equal\n");
}
else{
printf("Strings Are Not Equal\n");
}
}
}
int main(){
char str1[100];
printf("Enter the String :");
scanf("%s",str1);
char str2[100];
printf("Enter the String :");
scanf("%s",str2);
compare(str1,str2);
return 0;
}

OUTPUT :
24. Program to String Reverse.

#include <stdio.h>
#include <string.h>
int main(){
char str[200000];
printf(“Enter the string : “);
scanf("%s",str);
for (int i = strlen(str) ; i >= 0; i--)
{
printf("%c",str[i]);
}
printf("\n");

return 0;
}
OUTPUT :

25. Program to convert a string from lower case to upper case.

#include <stdio.h>
int main() {
char s[100];
int i;
printf("\nEnter a string : ");
gets(s);
for (i = 0; s[i]!='\0'; i++) {
if(s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 32;
}
}
printf("\nString in Upper Case = %s", s);
return 0;
}

OUTPUT :

26. Program for the addition of two matrices.


#include <stdio.h>
int main(){
int m,n;
printf("Enter The Order Of two matrices : ");
scanf("%d%d",&m,&n);
int A[m][n];
int B[m][n];
int C[m][n];
printf("FOR MATRIX A\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&A[i][j]);
}
}
printf("FOR MATRIX B\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&B[i][j]);
}
}
printf("SUM OF TWO MATRICES IS\n");
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
C[i][j] = A[i][j]+B[i][j];
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
printf("%d ",C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT :

27. Program for the Multiplication of two matrices.

#include <stdio.h>
int main(){
int m1,n1,m2,n2;
printf("Enter The Order Of Matrix A : ");
scanf("%d%d",&m1,&n1);
printf("Enter The Order Of Matrix B : ");
scanf("%d%d",&m2,&n2);
int A[m1][n1];
int B[m2][n2];
int C[m1][n2];
printf("\t\tFOR MATRIX A\n");
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
scanf("%d",&A[i][j]);
}
}
printf("\t\tFOR MATRIX B\n");
for(int i=0;i<m2;i++){
for(int j=0;j<n2;j++){
scanf("%d",&B[i][j]);
}
}

for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
C[i][j]=0;
for(int k=0;k<n1;k++){
C[i][j] += A[i][k]*B[k][j];
}
}
}
printf("\t\tMULTIPLICATION OF A AND B\n");
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
printf("%d ",C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT :
28. Program to swap two numbers using pointers.

#include <stdio.h>
void swap(int *x,int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int x,y;
printf("Enter X and Y : ");
scanf("%d %d",&x,&y);
swap(&x,&y);
printf("\tAfter Swappping\n");
printf("X = %d\tY = %d\n",x,y);
return 0;
}

OUTPUT :

29. Program to generate employee details using structures.


#include <stdio.h>
#include <string.h>
struct employee{
char name[20];
char department[30];
int code;
float salary;
};
int main(){
struct employee e1,e2;
printf("Enter The name Of Employee e1 : ");
gets(e1.name);
printf("Enter The Department Of Employee e1 : ");
gets(e1.department);
printf("Enter The Code Of Employee e1 : ");
scanf("%d",&e1.code);
printf("Enter The Salary Of Employee e1 : ");
scanf("%f",&e1.salary);
fflush(stdin);

printf("Enter The name Of Employee e2 : ");


gets(e2.name);
printf("Enter The Department Of Employee e2 : ");
gets(e2.department);
printf("Enter The Code Of Employee e2 : ");
scanf("%d",&e2.code);
printf("Enter The Salary Of Employee e2 : ");
scanf("%f",&e2.salary);
fflush(stdin);
printf("Details Of Employee %s : \n",e1.name);
puts(e1.department);
printf("%d\n",e1.code);
printf("%.2f\n",e1.salary);
printf("Details Of Employee %s : \n",e2.name);
puts(e2.department);
printf("%d\n",e2.code);
printf("%.2f\n",e2.salary);
}
OUTPUT :

30. Program to find the area and perimeter of a


circle,rectangle,square and triangle using functions.

#include <stdio.h>
#include <math.h>
void circle(float radius)
{
float perimeter = (3.14) * 2 * (radius);
printf("perimeter of circle= %f \n", perimeter);
float area = (3.14) * (radius) * (radius);
printf("area of circle =%f \n", area);
}
void square(float side)
{
float perimeter = 4 * (side);
printf("perimeter of square= %f \n", perimeter);
float area = (side) * (side);
printf("area of sqaure= %f \n", area);
}
void rectangle(float length, float width)
{
float perimeter = (length + width) * 2;
printf("perimeter of rectangle= %f \n", perimeter);
float area = (length) * (width);
printf("area of rectangle= %f \n", area);
}
void triangle(float a,float b, float c)
{
float perimeter = (a+b+c) ;
printf("perimeter of triangle= %f \n", perimeter);
float s = ((a+b+c)*(0.5)) ;
float area = sqrt( s*(s-a)*(s-b)*(s-c) );
printf("area of triangle= %f \n",area);
}
int main()
{
float radius, side, length, width, a, b, c;
printf("\tEnter the radius of circle : ");
scanf("%f", &radius);
printf("\tEnter the side of square : ");
scanf("%f", &side);
printf("\tEnter length & width of rectangle : ");
scanf("%f%f", &length, &width);
printf("\tEnter sides of triangle : ");
scanf("%f%f%f", &a, &b, &c);
printf("\n");
circle(radius);
printf("\n");
square(side);
printf("\n");
rectangle(length, width);
printf("\n");
triangle(a, b, c);
return 0;
}
OUTPUT :

31. Program to pass and return pointer to function hence


calculate average of an array.

#include<stdio.h>
float avg(int *a,int size)
{
int i;
int sum=0;
for(i=0 ; i<size ; i++)
sum+=a[i];
return ((float)sum/size);
}
int main()
{
int n, i;
printf("Enter the number of element: ");
scanf("%d",&n);
int arr[n];
printf("Enter the array elements: ");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("The average of the array elements = %f",avg(&arr,n));
return 0;
}

OUTPUT :

32.Program to pass and return pointer to function hence


calculate the sum of all the element

#include<stdio.h>
int sum(int *a,int *size)
{
int sum=0, i;
for(i=0;i<*size;i++)
sum+=a[i];
return sum;
}
int main()
{
int size, i;
printf("Enter the number of elements: ");
scanf("%d",&size);
printf("Enter the array elements\n");
int a[size];
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("The sum of the array elements: %d",sum(a,&size));
}
OUTPUT :

33. Program to demonstrate the example of array of pointers.

#include <stdio.h>
int main()
{
int x, y, z;
int *arr[3];
arr[0] = &x;
arr[1] = &y;
arr[2] = &z;
x = 545;
y = 235;
z = 25;
printf("value of x:%d, y:%d, z:%d\n", *arr[0], *arr[1], *arr[2]);
*arr[0] += 10;
*arr[1] += 10;
*arr[2] += 10;
printf("After adding 10\nvalue of x:%d, y:%d, z:%d\n", *arr[0], *arr[1],
*arr[2]);
return 0;
}

OUTPUT :
34. Program to create a file called emp.txt and store
information about a person, in terms of his name, age and
salary.

#include<stdio.h>
int main(){
char name[15];
int age;
float salary;
FILE *f;
f=fopen("emp.txt","w+");
printf("Enter name: ");
gets(name);
printf("Enter age: ");
scanf("%d",&age);
printf("Enter salary: ");
scanf("%f",&salary);
printf(f,"%s %d %f",name,age,salary);
printf("\n \n");
fscanf(f,"%s %d %f",name,age,salary);
printf("Name=: %s\nAge: %d\nSalary: %.2f\n",name,age,salary);
return 0;
}

OUTPUT :

35. Program which copies one file content to another file.


#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 file name to open for reading
a.txt
Enter the file name to open for writing
b.txt
content copied to b.txt

36. Program to read a file and after converting all lower case
to upper case letters write it to another file
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("source.txt", "r");
if (fp1 == NULL)
{
puts("File does not exist..");
exit(1);
}
fp2 = fopen("target.txt", "w");
if (fp2 == NULL)
{
puts("File does not exist..");
fclose(fp1);
exit(1);
}
while ((ch = fgetc(fp1)) != EOF)
{
ch = toupper(ch);
fputc(ch, fp2);
}
printf("File successfully copied \n");
return 0;
}
37. Program to find the size of a given file.

#include <stdio.h>
long int findSize(char file_name[])
{
// opening the file in read mode
FILE *fp = fopen(file_name, "r");
// checking if the file exist or not
if (fp == NULL)
{
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
// calculating the size of the file
long int res = ftell(fp);
// closing the file
fclose(fp);
return res;
}
int main()
{
char file_name[] = {"a.txt"};
long int res = findSize(file_name);
if (res != -1)
printf("Size of the file is %ld bytes \n", res);
return 0;
}

35. Program which copies one file content to another file.


#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 file name to open for reading
a.txt
Enter the file name to open for writing
b.txt
content copied to b.txt

36. Program to read a file and after converting all lower case
to upper case letters write it to another file
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("source.txt", "r");
if (fp1 == NULL)
{
puts("File does not exist..");
exit(1);
}
fp2 = fopen("target.txt", "w");
if (fp2 == NULL)
{
puts("File does not exist..");
fclose(fp1);
exit(1);
}
while ((ch = fgetc(fp1)) != EOF)
{
ch = toupper(ch);
fputc(ch, fp2);
}
printf("File successfully copied \n");
return 0;
}

37. Program to find the size of a given file.


#include <stdio.h>
long int findSize(char file_name[])
{
// opening the file in read mode
FILE *fp = fopen(file_name, "r");
// checking if the file exist or not
if (fp == NULL)
{
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
// calculating the size of the file
long int res = ftell(fp);
// closing the file
fclose(fp);
return res;
}
int main()
{
char file_name[] = {"a.txt"};
long int res = findSize(file_name);
if (res != -1)
printf("Size of the file is %ld bytes \n", res);
return 0;
}

You might also like