0% found this document useful (0 votes)
19 views57 pages

PWC Final Journal

The document contains a series of programming practicals in C, covering basic operations such as arithmetic calculations, area and volume calculations, student grade calculations, string manipulations, and various algorithms including checking even/odd, positive/negative numbers, and calculating factorials. Each practical includes a specific aim, code implementation, and expected output. The document serves as a comprehensive guide for beginners to learn fundamental programming concepts in C.

Uploaded by

vedhasm75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views57 pages

PWC Final Journal

The document contains a series of programming practicals in C, covering basic operations such as arithmetic calculations, area and volume calculations, student grade calculations, string manipulations, and various algorithms including checking even/odd, positive/negative numbers, and calculating factorials. Each practical includes a specific aim, code implementation, and expected output. The document serves as a comprehensive guide for beginners to learn fundamental programming concepts in C.

Uploaded by

vedhasm75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

PROGRAMMING WITH C:

Practical No 1:

Aim: Basic Programs (Variables and Operators).

Practical A:
Aim: Write a program to find the addition, subtraction, multiplication and division of two
numbers.
Code:
int sum(int x, int y) {
return x + y;
}

int diff(int x, int y) {


return x - y;
}

int product(int x, int y) {


return x * y;
}

int quotient(int x, int y) {


return x / y;
}

void main() {
int a, b, ans, choice = 0;

do {
printf(" Basic Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1.ADD\n2.SUBTRACT\n3.MULTIPLICATION\n4.DIVISION\n5.EXIT\n");
printf("Enter your choice as (1-5)\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = sum(a, b);
printf("The answer is %d\n", ans);
break;
case 2:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = diff(a, b);
printf("The answer is %d\n", ans);
break;
case 3:
printf("Enter two integers:\n");
scanf("%d%d", &a, &b);
ans = product(a, b);
printf("The answer is %d\n", ans);
break;
case 4:
printf("Enter Dividend:\n");
scanf("%d", &a);
printf("Enter Divisor:\n");
scanf("%d", &b);
if (b != 0) {
ans = quotient(a, b);
printf("The answer is %d\n", ans);
} else {
printf("Division by zero is not allowed.\n");
}
break;
case 5:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 5);
}

Output
Practical B:
Aim: Write a program to find area of rectangle, square and circle.
Code:
float areaofsquare(float x) {
return x * x;
}

float areaofcircle(float x) {
return 3.14159 * x * x;
}

float areaofrectangle(float x, float y) {


return x * y;
}

void main() {
int choice = 0;
float l, b, r, area;
do {
printf(" Area Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1. Area of Square\n2. Area of Circle\n");
printf("3. Area of Rectangle\n4. EXIT\n");
printf("Enter your choice (1-4):\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter length of side of square:\n");
scanf("%f", &l);
area = areaofsquare(l);
printf("The area of the square is %f\n", area);
break;
case 2:
printf("Enter radius of circle:\n");
scanf("%f", &r);
area = areaofcircle(r);
printf("The area of the circle is %f\n", area);
break;
case 3:
printf("Enter length and breadth of rectangle:\n");
scanf("%f%f", &l, &b);
area = areaofrectangle(l, b);
printf("The area of the rectangle is %f\n", area);
break;
case 4:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 4);
}

Output:

Practical C:
Aim: Write a program to find volume of cube, sphere and cylinder.
Code:
float volumeofcube(float x) {
return x * x * x;
}
float volumeofsphere(float x) {
return (4.0 / 3.0) * 3.14159 * x * x * x;
}

float volumeofcylinder(float x, float y) {


return 3.14159 * x * x * y;
}

void main() {
int choice = 0;
float l, h, r, vol;

do {
printf(" Volume Calculator \n");
printf(" \n");
printf("Choose an operation:\n");
printf("1. Volume of Cube\n2. Volume of Sphere\n");
printf("3. Volume of Cylinder\n4. EXIT\n");
printf("Enter your choice (1-4):\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter length of side of cube:\n");
scanf("%f", &l);
vol = volumeofcube(l);
printf("The volume of the cube is %f\n", vol);
break;
case 2:
printf("Enter radius of sphere:\n");
scanf("%f", &r);
vol = volumeofsphere(r);
printf("The volume of the sphere is %f\n", vol);
break;
case 3:
printf("Enter height of the cylinder:\n");
scanf("%f", &h);
printf("Enter radius of cylinder:\n");
scanf("%f", &r);
vol = volumeofcylinder(r, h);
printf("The volume of the cylinder is %f\n", vol);
break;
case 4:
printf("Thank You\n");
break;
default:
printf("Enter a valid choice\n");
}
} while (choice != 4);
}

Output:
PROGRAMMING WITH C:

Practical No 2:

A) Write c program for student grade calculator. Take Name, roll no and marks of three
subjects (Math, science and English) from user. Display total marks, percentage and Grade.
(Percentage >=90 A Grade, >=80 B Grade, >=70 C grade, >=60 D grade Else F grade).

Code:
#include <stdio.h>

void main()

char name[30], grade;

int roll;
float percentage, m1, m2, m3, total;

printf("Enter the student's name:\n");

scanf("%s", name); // Removed '&' as it's not required for strings.

printf("Enter student's roll no.:\n");

scanf("%d", &roll);

printf("Enter marks in Math:\n");


scanf("%f", &m1);

printf("Enter marks in Science:\n");

scanf("%f", &m2);

printf("Enter marks in English:\n");

scanf("%f", &m3);
total = m1 + m2 + m3;

percentage = total / 3.0;

if (percentage >= 90.00)

grade = 'A';

else if (percentage >= 80.00)

grade = 'B';

}
else if (percentage >= 70.00)

grade = 'C';

else if (percentage >= 60.00)

grade = 'D';

else

grade = 'F';

printf("Student's Name: %s", name);

printf("\nStudent's Roll no.: %d", roll);

printf("\nMarks in Math: %.2f", m1); // Added precision for float output.


printf("\nMarks in Science: %.2f", m2);
printf("\nMarks in English: %.2f", m3);

printf("\nTotal Marks: %.2f", total);

printf("\nPercentage: %.2f", percentage);

printf("\nGrade: %c", grade);


}

Output:

B) Write C program to perform string manipulation operation like find length, concatenation,
comparison, copy and reverse. Take required string from user.

Code:
void main()

int choice=0,n;

char str1[30],str2[30];

do{

printf("\nChoose an operation:\n");
printf("1.Length 2.Concatenation 3.Comparison 4.Copy 5.Reverse 6.Exit\n");

printf("Enter your choice as (1-6)\n");

scanf("%d",&choice);
switch (choice)

case 1:

printf("Enter String:\n");

scanf("%s",&str1);

n=strlen(str1);

printf("%d\n",n);

break;

case 2:
printf("Enter first string :\n");

scanf("%s",&str1);
printf("Enter second string :\n");

scanf("%s",&str2);
printf(strcat(str1,str2));

break;

case 3:

printf("Enter first string :\n");

scanf("%s",&str1);
printf("Enter second string :\n");

scanf("%s",&str2);
n=strcmp(str1,str2);

if (n==0)

printf("String are the same.\n");

else

printf("String are different.\n");


}
break;

case 4:
printf("Enter String:\n");
scanf("%s",&str1);
strcpy(str2,str1);

printf("Copied string :%s\n",str2);

break;

case 5:
printf("Enter String:\n");

scanf("%s",&str1);

printf(strrev(str1),"\n");

break;
case 6:
printf("Thank You\n");

break;
default:

printf("Enter valid choice\n");

};

while (choice!=6);

Output:
C) Write a C program to check if a given string is palindrome or not.

Code:
void main()

char str1[50],rstr1[50];

int n;
n=1;

printf("\nEnter a string :");

scanf("%s",&str1);

strcpy(rstr1,str1);

strrev(rstr1);

n=strcmp(str1,rstr1);
if (n==0)

printf("Original String :%s\n",str1);


printf("Reversed String %s\n",rstr1);

printf("Given string is a palindrome");

else

printf("Original String :%s\n",str1);


printf("Reversed String :%s\n",rstr1);

printf("Given string is not a palindrome");

}
}

Output:

D) Write a C program to count the frequency of each character in given string.

Code:
void main()
{

char str1[50];

int count[100],i,j,len;

printf("Enter a string :");

scanf("%s",str1);

len=strlen(str1);
for (i=0;i<=(len-1);i++)
{

if (str1[i]==' ')

continue;

count[i]=0;

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

if(str1[j]==str1[i])

count[j]+=1;
break;

printf("Frequency :\n");

for(i=0;i<=(len-1);i++)

{
if (count[i]==0 || str1[i]==' ')

continue;
}

else

printf("%c : %d\n",str1[i],count[i]);
}

Output:
PROGRAMMING WITH C:

Practical No 3:

Practical A:
Aim: Write a program to check whether the number is Even or Odd.

Code:
#include <stdio.h>

void main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is Even.\n", num);

} else {

printf("%d is Odd.\n", num);

Output:
Practical B:
Aim: Write a program to check whether the number is Positive, Negative or Zero.

Code:
#include <stdio.h>
void main() {

int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num > 0) {

printf("%d is Positive.\n", num);


} else if (num < 0) {

printf("%d is Negative.\n", num);

} else {

printf("The number is Zero.\n");

Output:
Practical C:
Aim: Write a program to find the sum of squares of digits of a number.

Code:

#include <stdio.h>

void main() {

int num, digit, sum = 0;


printf("Enter a number: ");

scanf("%d", &num);

while (num != 0) {

digit = num % 10;

sum = sum + digit * digit;

num = num / 10;

}
printf("Sum of squares of digits: %d\n", sum);

Output:
Practical D:
Aim: Write a program to reverse the digits of an integer.

Code:
#include <stdio.h>
void main() {

int num, reversed = 0, digit;

printf("Enter an integer: ");


scanf("%d", &num);

while (num != 0) {

digit = num % 10;

reversed = reversed * 10 + digit;

num = num / 10;

}
printf("Reversed number: %d\n", reversed);

Output:
PROGRAMMING WITH C:

Practical No 4:

Practical A:
Aim: Write a C program to calculate the Body Mass Index (BMI) of a person based on their
weight and height, and determine their BMI category according to the following criteria:

BMI < 18.5 Underweight; 18.5 ≤ BMI < 24.9 Normal weight; 25 ≤ BMI < 29.9 Overweight
BMI ≥ 30 Obese using function.

Code:
#include <stdio.h>

float calculateBMI(float weight, float height) {


return weight / (height * height);

void determineBMICategory(float bmi) {


if (bmi < 18.5) {

printf("BMI: %.2f - Underweight\n", bmi);

} else if (bmi >= 18.5 && bmi < 24.9) {

printf("BMI: %.2f - Normal weight\n", bmi);

} else if (bmi >= 25 && bmi < 29.9) {

printf("BMI: %.2f - Overweight\n", bmi);


} else {
printf("BMI: %.2f - Obese\n", bmi);

void main() {

float weight, height, bmi;


printf("Enter height in meters: ");

scanf("%f", &height);

printf("Enter weight in kg: ");

scanf("%f", &weight);
bmi = calculateBMI(weight, height);

determineBMICategory(bmi);

}
Output:
PROGRAMMING WITH C:

Practical No 5:

Practical A:
Aim: Write a program to find the factorial of a number using recursive function.

Code:
#include <stdio.h>

long factorial(int n) {

if (n == 0 || n == 1)

return 1;

else
return n * factorial(n - 1);
}

void main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d is %ld\n", num, factorial(num));

Output:
Practical B:
Aim: Write a program to find the sum of natural numbers using recursive function.

Code:
#include <stdio.h>

int sumOfNaturalNumbers(int n) {

if (n == 0)

return 0;

else

return n + sumOfNaturalNumbers(n - 1);


}

void main() {

int num;

printf("Enter a positive integer: ");


scanf("%d", &num);

if (num < 0) {

printf("Please enter a positive integer.\n");


} else {
printf("Sum of first %d natural numbers is %d\n", num, sumOfNaturalNumbers(num));
}

Output:
PROGRAMMING WITH C:

Practical No 6:

Practical A:
Aim: Find Largest Value Store in Array.

Code:
#include <stdio.h>

int main()
{

int n, i, max;

printf("Name: Karthik Vinod, Roll No. FCS2425041\n");


printf("Enter Total Number of Elements in Array: ");
scanf("%d", &n);

int arr[n];

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

printf("Enter Element %d: ", i + 1);

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

}
max = arr[0];

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

if (max < arr[i])

max = arr[i];

}
}

printf("Largest Number is: %d", max);

return 0;
}

Output:

Practical B:
Aim: Write a program using pointer to compute the sum of all elements stored in array.

Code:
#include <stdio.h>
int main()

int n, i, sum = 0;

int *ptr;

printf("Name: Karthik Vinod, Roll No. FCS2425041\n");

printf("Enter size of array: ");


scanf("%d", &n);
int arr[n];

ptr = arr;

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


{

printf("Enter Element %d: ", i + 1);

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

sum += *ptr;

ptr++;
}
printf("Sum is: %d", sum);

return 0;
}

Output:

Practical C:

Aim: Arrange The Element of An Array in Ascending and Descending Order.

Code:
#include <stdio.h>

void sortAscending(int arr[], int n)

{
int i, j, temp;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])


{

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}
}
}

void sortDescending(int arr[], int n)

{
int i, j, temp;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (arr[j] < arr[j + 1])

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;
}

}
void printArray(int arr[], int n)

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

printf("%d ", arr[i]);

printf("\n\n");
}

int main() {

int n, i;
printf("Name: Karthik Vinod, Roll No. FCS2425041\n");

printf("Enter size of array: ");

scanf("%d", &n);

int arr[n];
for(i = 0; i<n; i++)
{

printf("Enter Element %d: ", i + 1);

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

printf("Original array: ");

printArray(arr, n);

sortAscending(arr, n);
printf("Sorted in Ascending Order: ");

printArray(arr, n);

sortDescending(arr, n);

printf("Sorted in Descending Order: ");

printArray(arr, n);

return 0;
}

Output:

Practical D:
Aim: Display result of 5 students using array.

Code:

#include <stdio.h>

void main()

{
int n;

printf("Name: Karthik Vinod, Roll No. FCS2425041\n");

printf("Enter Total Number of Students: ");


scanf("%d", &n);

int i, roll[n], phy[n], chem[n], bio[n], cs[n], maths[n];

float percentage[5], calculate;

char name[n][30], grade[n];

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

printf("\nEnter Name of Student %d: ", i+1);

scanf("%s", name[i]);

printf("Enter Roll No of Student %d: ", i+1);

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

printf("Enter Physics: ");

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

printf("Enter Chemistry: ");

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

printf("Enter Biology: ");

scanf("%d", &bio[i]);
printf("Enter CS: ");

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

printf("Enter Maths: ");


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

calculate = 0;

calculate = phy[i] + chem[i] + bio[i] + cs[i] + maths[i];

percentage[i] = (float)calculate/500*100;
if (percentage[i] >= 90)

grade[i] = 'A';
}

else if (percentage[i] >= 80)

grade[i] = 'B';

else if (percentage[i] >= 70)

grade[i] = 'C';

else if (percentage[i] >= 60)

grade[i] = 'D';

else {
grade[i] = 'F';

}
}
for (i = 0; i<n; i++)

printf("\nName: %s \t Roll No: %d\n", name[i], roll[i]);

printf("Physcis: %d\t Chemistry: %d\t Biology: %d\t CS: %d\t Maths: %d\n", phy[i],
chem[i], bio[i], cs[i], maths[i]);

printf("Percentage: %.2f%%\n", percentage[i]);


printf("Grade: %c\n", grade[i]);

}
}

Output:
PROGRAMMING WITH C:

Practical No 7:

Practical A:
Aim: Swap two number using pointer.

Code:
#include <stdio.h>

#include <stdlib.h>

void swap(int* a, int* b)

int temp=*a;
*a=*b;

*b=temp;

int main()

int num1, num2;

printf("Enter First Number: ");

scanf("%d",&num1);

printf("Enter Second Number: ");

scanf("%d",&num2);

swap(&num1, &num2);

printf("Swapped Value \n Num1=%d \n Num2=%d \n",num1,num2);


}

Output:
Practical B:

Aim: Copy string and find length using pointer.

Code:
// Copy string and find length using pointer

#include <stdio.h>

#include <stdlib.h>

void copyString(char* src, char* dest)

while(*src!='\0')

*dest=*src;
src++;
dest++;

*dest='\0';

int findLength(char* str)

int length=0;
while(*str!='\0')
{

length++;

str++;

}
return length;

int main()

char firstString[]="Computer Science";

char secondString[20];
copyString(&firstString,&secondString);

printf("Copied String: %s\n",secondString);

printf("Length of First String: %d \n",findLength(firstString));

return 0;
}

Output:

Practical C:
Aim: Perform addition and subtraction of two pointer variable.

Code:
//Perform addition and subtraction of two pointer variable

#include <stdio.h>
#include <stdlib.h>

int intAddition(int* x,int* y)

return(*x+*y);
}

int intSubtraction(int* x,int* y)

return(*x-*y);

int main()

int num1,num2;

printf("Enter First Number: ");

scanf("%d",&num1);

printf("Enter Second Number: ");

scanf("%d",&num2);
printf("Sum: %d\n",intAddition(&num1,&num2));
printf("Difference: %d\n",intSubtraction(&num1,&num2));

return 0;

Output:
PROGRAMMING WITH C:

Practical No 8:

Practical A:

Aim: Program on Student Grade system.

Code:
#include <stdio.h>

#include <stdlib.h>

#include <stdio.h>

struct Student {

char name[50];
int age;

float grade;

};

int main()

struct Student s1 = {"Geek", 20, 85.5};

printf("%s\n", s1.name);

printf("%d\n", s1.age);

printf("%.2f\n", s1.grade);

printf("Size: %d bytes", sizeof(s1));

return 0;

}
Output:

Practical B:
Aim: Program on structure.

Code:
#include <stdio.h>

#include <stdlib.h>

struct Bank

int acc_no;
char name[30];
float balance;

};

struct Bank deposit(struct Bank x, float num)


{

x.balance += num;

printf("Updated Balance: %.2f\n", x.balance);

return x;

struct Bank withdraw(struct Bank x, float num)

{
if (x.balance < num)

printf("Insufficient Funds\n");

}
else

x.balance -= num;

printf("Updated Balance: %.2f\n", x.balance);

return x;

void check_balance(struct Bank x)

printf("Balance: %.2f\n", x.balance);

int main()

struct Bank acc1;

int choice = 0;
float amt;

printf("Name: Karthik Vinod, Roll No: FCS2425041 \n\n");

printf("****** WELCOME TO PWC BANK ******\n\n");

printf("Enter your Account Number: ");


scanf("%d", &acc1.acc_no);
printf("Enter Account Holder's Name: ");

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

printf("Enter your balance: ");


scanf("%f", &acc1.balance);

while (choice != 4)

{
printf("\nWhat would you like to do?");

printf("\n1. Check Balance \n2. Deposit \n3. Withdraw \n4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

check_balance(acc1);

break;

case 2:
printf("Enter amount you want to deposit: ");

scanf("%f", &amt);

acc1 = deposit(acc1, amt);


break;

case 3:
printf("Enter amount you want to withdraw: ");

scanf("%f", &amt);

acc1 = withdraw(acc1, amt);


break;
case 4:

printf("Thank You for Banking with PWC Bank!\n");


break;

default:
printf("Invalid option selected. Please try again.\n");
}

return 0;

Output:
PROGRAMMING WITH C:

Practical No 9:

Practical A:
Aim: Write a C program on union.

Code:
#include <stdio.h>

#include <stdlib.h>

union student {

int rollno;
float percentage;

char name[30];

};

void main()

union student s1;

s1.rollno = 225;
printf("Roll no: %d\n", s1.rollno);

s1.percentage = 80.0;

printf("Percentage: %.2f\n", s1.percentage);

strcpy(s1.name, "Karthik");
printf("Name: %s\n", s1.name);

Output:
Practical B:

Aim: Write a C program on union using structure.

Code:
#include <stdio.h>

#include <string.h>

union studentData {

int id;

float percentage;
char name[30];

};
struct student {

union studentData info;

};

int main()
{

struct student s1;

s1.info.id = 101;
printf("Student ID: %d\n", s1.info.id);

s1.info.percentage = 85.5;

printf("Percentage: %.2f\n", s1.info.percentage);

strcpy(s1.info.name, "Karthik");
printf("Name: %s\n", s1.info.name);

return 0;
}

Output:

Practical C:
Aim: Write a C program on union using pointer.

Code:
#include <stdio.h>

#include <string.h>

union student {
int id;

float percentage;
char name[30];

};
int main()

union student s1;

union student *ptr;

ptr = &s1;

ptr->id = 101;
printf("Student ID: %d\n", ptr->id);

ptr->percentage = 85.5;
printf("Percentage: %.2f\n", ptr->percentage);

strcpy(ptr->name, "Karthik");
printf("Name: %s\n", ptr->name);

return 0;

Output:

Practical D:
Aim: Write a C program on union using user define function.

Code:
#include <stdio.h>

#include <stdlib.h>

#include<string.h>

union Student

{
int rollno;
float percentage;

char name[30];

};
void printStudent(union Student s1)

printf("Roll No. : %d\n",s1.rollno);


printf("Percentage :%.2f \n",s1.percentage);
printf("Name : %s",s1.name);

int main()

union Student s1;

s1.rollno= 101 ;
s1.percentage= 85.50 ;

strcpy(s1.name,"Karthik");

printStudent(s1);
return 0;

Output:
PROGRAMMING WITH C:

Practical No 10:

Practical 10A:
Aim: Create a file read and close it.

Code:
#include <stdio.h>

#include <stdlib.h>

int main()

printf("Karthik Vinod, Roll No: FCS2425041\n");


FILE* fp;

char line[100];

fp=fopen("example.txt","r");

if (fp==NULL)
{

printf("Error in opening file .\n");

fclose(fp);

return 0;
}

else

printf("File Opened Successfully.\n\n");

while(fgets(line,100,fp))

printf("%s",line);
}
printf("\nClosing File");

fclose(fp);
return 0;

Output:

Practical 10B:
Aim: Write a user friendly program to read and write the file.

Code:
#include <stdio.h>

#include <stdlib.h>

void readFile()

FILE* fp;

char line[100];

fp=fopen("ReadWrite.txt","r");

if (fp==NULL)
{
printf("Error in opening file .\n");

fclose(fp);
return;

else

printf("File Opened Successfully.\n");

while(fgets(line,100,fp))

printf("%s",line);

printf("\nClosing File");

fclose(fp);

}
void writeFile()

FILE* fp;
fp=fopen("ReadWrite.txt","w");

if (fp==NULL)

printf("Error in opening file .\n");

fclose(fp);
return;

else

printf("File Opened Successfully.\n");


}
fprintf(fp,"Hello and This is my menu driven program for reading and writing to a file.");

printf("\nClosing File");
fclose(fp);

int main()

printf("Name: Karthik Vinod, Roll No: FCS2425041");

int choice,e=1;
while(e==1)

printf("\nChoices:\n 1.Read from File \n 2.Write to File \n 3.Exit \n Enter Your


Choice:");
scanf("%d",&choice);

switch(choice)

{
case 1:

readFile();

break;

case 2:
writeFile();

break;

case 3:
printf("Program Closing. .....\n");

e=0;

break;

default:

printf("Invalid choice");
}
}
return 0;

Output:

Practical 10C:
Aim: To implement a C program that reads an integer from a file (input.txt), calculates its
factorial using recursion, and writes the result to another file (output.txt).

Code:
#include <stdio.h>

#include <stdlib.h>

int factorial(int n)

{
if (n == 0 || n == 1)

return 1;

return n * factorial(n - 1);

}
int main()
{

FILE *fp;

int num, fact;


fp = fopen("input.txt", "r");

if (fp == NULL)

printf("Error: Unable to open input file.\n");


return 1;
}

if (fscanf(fp, "%d", &num) != 1)

printf("Error: Failed to read integer from input file.\n");


fclose(fp);

return 1;
}

fclose(fp);

fact = factorial(num);

fp = fopen("output.txt", "w");
if (fp == NULL)

printf("Error: Unable to open output file.\n");

return 1;
}

fprintf(fp, "%d", fact);

fclose(fp);

printf("Factorial of %d has been written to output.txt\n", num);

return 0;
}

Output:

You might also like