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

cs lab exp kani

hi

Uploaded by

nandu.sbng
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 views

cs lab exp kani

hi

Uploaded by

nandu.sbng
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/ 53

Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

6.Solving problems using Strings


6(a) Write a C program to print each character in a string in a
line
Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
printf("Characters in the string:\n");
for (int i = 0; str[i] != '\0'; i++)
{
printf("%c\n", str[i]);
}
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Case 1:

Enter a string: Hello


Characters in the string:
H
e
l
l
o

Case 2:
Enter a string: World123
Characters in the string:
W
o
r
l
d
1
2
3

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

6(b) Write a C program to count the frequency of vowels in a


string.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels[5] = {0};
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
char ch = tolower(str[i]);
switch (ch)
{
case 'a': vowels[0]++; break;
case 'e': vowels[1]++; break;
case 'i': vowels[2]++; break;
case 'o': vowels[3]++; break;
case 'u': vowels[4]++; break;
}
}
printf("\nFrequency of vowels:\n");
printf("a: %d\n", vowels[0]);
printf("e: %d\n", vowels[1]);
printf("i: %d\n", vowels[2]);
printf("o: %d\n", vowels[3]);
printf("u: %d\n", vowels[4]);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Case 1:

Enter a string: Hello World


Frequency of vowels:
a: 0
e: 1
i: 0
o: 2
u: 0

Case 2:

Enter a string: Programming is fun


Frequency of vowels:
a: 1
e: 0
i: 2
o: 1
u: 1

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

6(c) A numerologist suggests renaming a person by repeating


the vowel in the name subsequent to its existing position. Ex:
Sivakumar :Siivaakuumaar. Write a C program to automate
his suggestion.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int is_vowel(char ch)
{
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
int main()
{
char name[100], modifiedName[200];
int i, j = 0;
printf("Enter a name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
for (i = 0; name[i] != '\0'; i++)
{
modifiedName[j++] = name[i];
if (is_vowel(name[i])) {
modifiedName[j++] = name[i];
}
}
modifiedName[j] = '\0';
printf("Modified name: %s\n", modifiedName);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Case 1:

Enter a name: Sivakumar


Modified name: Siivaakuumaar

Case 2:

Enter a name: Alexander


Modified name: Aaleexaandeer

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

6(d) Write a C program to read the first name, middle name,


and last name of a person and form a full name as the
concatenation of them.(Using and without using library
functions).

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

Using Library Functions

#include <stdio.h>
#include <string.h>
int main()
{
char firstName[50], middleName[50], lastName[50],
fullName[150];
scanf("%s", firstName);
printf("Enter the middle name: ");
scanf("%s", middleName);
printf("Enter the last name: ");
scanf("%s", lastName);
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, middleName);
strcat(fullName, " ");
strcat(fullName, lastName);
printf("Full name (using library functions): %s\n", fullName);
return 0;
}

Without Using Library Functions

#include <stdio.h>
int main()
{
char firstName[50], middleName[50], lastName[50],
fullName[150];
int i, j;
printf("Enter the first name: ");
scanf("%s", firstName);
printf("Enter the middle name: ");
scanf("%s", middleName);

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

printf("Enter the last name: ");


scanf("%s", lastName);
for (i = 0; firstName[i] != '\0'; i++)
{
fullName[i] = firstName[i];
}
fullName[i++] = ' ';
for (j = 0; middleName[j] != '\0'; j++)
{
fullName[i++] = middleName[j];
}
fullName[i++] = ' ';
for (j = 0; lastName[j] != '\0'; j++)
{
fullName[i++] = lastName[j];
}
fullName[i] = '\0';
printf("Full name (without using library functions): %s\n",
fullName);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the first name: John


Enter the middle name: Fitzgerald
Enter the last name: Kennedy

Output:

Full name (using library functions): John Fitzgerald Kennedy


Full name (without using library functions): John Fitzgerald
Kennedy

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

Exercise 7 Programs to illustrate user defined functions

7(a) Write a C program to implement a pocket calculator


using user defined functions

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
int main() {
int choice;
float num1, num2, result;
while (1)
{
printf("\n--- Pocket Calculator ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice)
if (choice == 5) {
printf("Exiting the program. Goodbye!\n");
break;
}
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (choice)
{
case 1:
result = add(num1, num2);
printf("Result: %.2f\n", result);
break;
case 2:
result = subtract(num1, num2);
printf("Result: %.2f\n", result);
break;
case 3:
result = multiply(num1, num2);

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

printf("Result: %.2f\n", result);


break;
case 4:
if (num2 == 0)
{
printf("Error: Division by zero is not allowed.\n");
}
else
{
result = divide(num1, num2);
printf("Result: %.2f\n", result);
}
break;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
float add(float a, float b)
{
return a + b;
}

float subtract(float a, float b)


{
return a - b;
}

float multiply(float a, float b)


{
return a * b;
}

float divide(float a, float b)


{
return a / b;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

--- Pocket Calculator ---


1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter your choice: 1


Enter two numbers: 10 5

Output:
Result: 15.00

Input:
Enter your choice: 4
Enter two numbers: 10 0

Output:
Error: Division by zero is not allowed.

Input:
Enter your choice: 5

Output:
Exiting the program. Goodbye!

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

7(b) Write a C program to calculate factorial of a number


using recursive function.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
long long factorial(int n);
int main()
{
int number;
long long result;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0)
{
printf("Error: Factorial of a negative number doesn't exist.\n");
}
else
{
result = factorial(number);
printf("Factorial of %d is %lld\n", number, result);
}
return 0;
}
long long factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter a positive integer: 5

Output:

Factorial of 5 is 120

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

7(c) Write a program to pass two numbers to the function


print_even(n1,n2) and print all the even numbers between n1
and n2.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
void print_even(int n1, int n2);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
print_even(num1, num2);
return 0;
}
void print_even(int n1, int n2)
{
int start = n1;
if (start % 2 != 0) {
start++;
}
printf("Even numbers between %d and %d are:\n", n1, n2);
for (int i = start; i <= n2; i += 2) {
printf("%d ", i);
}
printf("\n");
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the first number: 4


Enter the second number: 15

Output:

Even numbers between 4 and 15 are:


4 6 8 10 12 14

Input:

Enter the first number: 9


Enter the second number: 18

Output:

Even numbers between 9 and 18 are:


10 12 14 16 18

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

7 (d) Write a program to pass two numbers to the function


print_prime(n1,n2) and print all the prime numbers between
n1 and n2.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
#include <stdbool.h>
bool is_prime(int num);
void print_prime(int n1, int n2);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
print_prime(num1, num2);
return 0;
}
bool is_prime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
void print_prime(int n1, int n2)
{
printf("Prime numbers between %d and %d are:\n", n1, n2); for
(int i = n1; i <= n2; i++)
{
if (is_prime(i))

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

{
printf("%d ", i);
}
}

printf("\n");
}

OUTPUT:

Input:

Enter the first number: 10


Enter the second number: 20

Output:

Prime numbers between 10 and 20 are:


11 13 17 19

Input:

Enter the first number: 1


Enter the second number: 10

Output:

Prime numbers between 1 and 10 are:

2357

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

8.Programs to illustrate Structures

8(a) Declare a book with <title, float price, int


number_of_pages>. Read the details of a book and print the
same.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
struct Book
{
char title[100];
float price;
int number_of_pages;
};
int main()
{
struct Book book;
printf("Enter the title of the book: ");
fgets(book.title, sizeof(book.title), stdin);
printf("Enter the price of the book: ");
scanf("%f", &book.price);
printf("Enter the number of pages: ");
scanf("%d", &book.number_of_pages);
printf("\n--- Book Details ---\n");
printf("Title: %s", book.title);
printf("Price: %.2f\n", book.price);
printf("Number of Pages: %d\n", book.number_of_pages);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the title of the book: C Programming Basics


Enter the price of the book: 499.99
Enter the number of pages: 250

Output:

--- Book Details ---


Title: C Programming Basics
Price: 499.99
Number of Pages: 250

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

8(b) Read 2 book details and print the details of the costlier
book.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
struct Book
{
char title[100];
float price;
int number_of_pages;
};
int main()
{
struct Book book1, book2;
printf("Enter the details of the first book:\n");
printf("Enter the title: ");
fgets(book1.title, sizeof(book1.title), stdin);
printf("Enter the price: ");
scanf("%f", &book1.price);
printf("Enter the number of pages: ");
scanf("%d", &book1.number_of_pages);
getchar();
printf("\nEnter the details of the second book:\n");
printf("Enter the title: ");
fgets(book2.title, sizeof(book2.title), stdin); // Read title with
spaces
printf("Enter the price: ");
scanf("%f", &book2.price);
printf("Enter the number of pages: ");
scanf("%d", &book2.number_of_pages);
printf("\n--- Costlier Book Details ---\n");
if (book1.price > book2.price) {
printf("Title: %s", book1.title);
printf("Price: %.2f\n", book1.price);
printf("Number of Pages: %d\n", book1.number_of_pages);
}
else if (book2.price > book1.price)
{
printf("Title: %s", book2.title);

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

printf("Price: %.2f\n", book2.price);


printf("Number of Pages: %d\n", book2.number_of_pages);
} else {
printf("Both books have the same price.\n");
}
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the details of the first book:


Enter the title: C Programming Basics
Enter the price: 499.99
Enter the number of pages: 250

Enter the details of the second book:


Enter the title: Data Structures and Algorithms
Enter the price: 599.99
Enter the number of pages: 350

Output:

--- Costlier Book Details ---


Title: Data Structures and Algorithms
Price: 599.99
Number of Pages: 350
Edge Case

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

Input:

Enter the details of the first book:


Enter the title: Java Programming
Enter the price: 450.00
Enter the number of pages: 300

Enter the details of the second book:


Enter the title: Python Programming
Enter the price: 450.00
Enter the number of pages: 350

Output:

Both books have the same price.

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

8(c) Create a structure STUDENT with


<Roll_no,Name,Gender,marks[5], grades[5],GPA>. Calculate the
grade of each subject (assuming one-to one mapping of marks and
grades) and GPA as the average of marks scored by a student.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
int main() {
int n, i, mark, total = 0;
printf("Enter the number of questions: ");
scanf("%d", &n);
printf("Enter the marks for each question (0-16):\n");
for (i = 1; i <= n; i++) {
while (1) {
printf("Question %d: ", i);
if (scanf("%d", &mark) != 1) {
printf("Invalid input! Please enter a valid number.\n"); // Clear
the input buffer
while (getchar() != '\n');
continue;
}
if (mark < 0 || mark > 16) {
printf("Marks must be in the range 0-16. Please re-enter.\n");
continue;
}
if (total + mark > 100) {
printf("Adding this mark exceeds the total limit of 100. Please re-
enter.\n");
continue;
}
total += mark;
break;
}
}
printf("The total marks awarded: %d\n", total);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Enter the number of questions: 5


Enter the marks for each question (0-16):
Question 1: 15
Question 2: 17
Marks must be in the range 0-16. Please re-enter.
Question 2: 10
Question 3: 20
Marks must be in the range 0-16. Please re-enter.
Question 3: 16
Question 4: 40
Adding this mark exceeds the total limit of 100. Please re-enter.
Question 4: 12
Question 5: -3
Marks must be in the range 0-16. Please re-enter.
Question 5: 10
The total marks awarded: 63

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

9.Programs to illustrate Pointers

Exercise (9a) Demonstrate address of (*) and indirection (*)


operators.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
int main()
{
int num = 10;
int *ptr;
ptr = &num;
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Value stored in ptr (Address of num): %p\n", (void*)ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
return 0;
}

OUTPUT:

Value of num: 10
Address of num: 0x7ffee1d5e3dc
Value stored in ptr (Address of num): 0x7ffee1d5e3dc
Value pointed to by ptr: 10

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

9(b) Write a C program, to swap two number using pointers.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
printf("Enter the first number: ");
scanf("%d", &x);
printf("Enter the second number: ");
scanf("%d", &y);
printf("\nBefore swapping:\n");
printf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("\nAfter swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the first number: 5


Enter the second number: 10

Output:

Before swapping:
x = 5, y = 10

After swapping:
x = 10, y = 5

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

9(c) Implement a Pocket calculator with arithmetic operators (include


increment/decrement also).

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
int main()
{
int num1, num2;
char operator;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter an operator (+, -, *, /, ++, --): ");
scanf(" %c", &operator); // Space before %c to avoid newline
character from previous input
if (operator == '++' || operator == '--')
{
if (operator == '++')
{
printf("Value before increment: %d\n", num1);
printf("Value after increment: %d\n", ++num1);
}
else if (operator == '--')
{
printf("Value before decrement: %d\n", num1);
printf("Value after decrement: %d\n", --num1);
}
}
else
{
printf("Enter the second number: ");
scanf("%d", &num2);
switch (operator)
{
case '+':
printf("Result: %d + %d = %d\n", num1, num2, num1 + num2);
break;

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

case '-':
printf("Result: %d - %d = %d\n", num1, num2, num1 - num2);
break;
case '*':
printf("Result: %d * %d = %d\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0)
{
printf("Result: %d / %d = %.2f\n", num1, num2, (float)num1 / num2);
}
else
{
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator!\n");
}
}
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the first number: 10


Enter an operator (+, -, *, /, ++, --): +
Enter the second number: 5

Output:

Result: 10 + 5 = 15

Input:

Enter the first number: 5


Enter an operator (+, -, *, /, ++, --): /
Enter the second number: 0

Output:

Error! Division by zero is not allowed.

Input:

Enter the first number: 5


Enter an operator (+, -, *, /, ++, --): ++

Output:

Value before increment: 5


Value after increment: 6

Input:

Enter the first number: 8


Enter an operator (+, -, *, /, ++, --): --

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

Output:

Value before decrement: 8


Value after decrement: 7

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

10. File Handling in C


10(a) File Handling in C

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
int main()
{
FILE *file;
char ch;
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
printf("File contents:\n");
while ((ch = fgetc(file)) != EOF)
{
putchar(ch);
}
fclose(file);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

INPUT

Hello, World!
This is a test file.

OUTPUT

File contents:
Hello, World!
This is a test file.

RESULT:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

10(b) Write a C program to read contents from user through console


and write in a file.

Aim:

Algorithm:

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

CODE:

#include <stdio.h>
int main()
{
FILE *file;
char content[1000];
printf("Enter the content to write to the file (press Enter to end
input):\n");
fgets(content, sizeof(content), stdin);
file = fopen("output.txt", "w");
if (file == NULL)
{
printf("Could not open the file.\n");
return 1;
}
fprintf(file, "%s", content);
printf("Content written to the file 'output.txt'.\n");
fclose(file);
return 0;
}

Register No: 2127240501118 Page No:


Exercise No: IT22111 – Programming for Problem Solving Laboratory Date:

OUTPUT:

Input:

Enter the content to write to the file (press Enter to end input):
Hello, this is a test content!

Output:

Content written to the file 'output.txt'.


After the program execution, the content "Hello, this is a test
content!" will be written into a file called output.txt.

File Contents (output.txt)


If you open the output.txt file after running the program, it will
contain:
Hello, this is a test content!

RESULT:

Register No: 2127240501118 Page No:

You might also like