AMITY UNIVERSITY MADHYA PRADESH GWALIOR
Practical file
of
PROBLEM SOLVING AND PROGRAMMING TECHNIQUES LAB
[MCA124]
Department of
Amity Institute of Information and Technology
AMITY UNIVERSITY MADHYA PRADESH
SUBMITTED TO: SUBMITTED BY:
Ms. Nidhi Tripathi Prince Bhadauria
Assistant Professor MCA, I Sem
ASET Enrollment No.
(A620145024015)
INDEX
[Link]. LIST OF PROGRAMS PG. No. DATE SIGNATURE
1. Write a C program to take input from the 4-5 06-09-2024
user and write it into a file.
2. Write a C program to demonstrate the use of 6-7 06-09-2024
various data types including int, float, char,
and double.
3. Write a C program to check whether the 8-9 13-09-2024
given string is palindrome or not without
using in built function.
4. Write a C program to read N integers into 10-11 20-09-2024
an array A and to find the (i) sum of odd
numbers, (ii) sum of even numbers, (iii)
average of all numbers.
5. Write a C program to find nth term of 12-13 27-09-2024
Fibonacci series
6. Write a program in C using functions to 14-15 04-10-2024
swap two numbers using global variables
concept and call by reference concept.
7. Write a C program to maintain a record of 16-17 18-10-2024
“n” students details using an array of
structures with four fields (roll no, name,
marks, and grade). Assume appropriate data
type for each field. Print the marks of the
student given the student’s name as input.
8. Write a C program to perform string 18-19 19-10-2024
manipulation operations such as
concatenation, copying, and comparison.
9. Explain how realloc() can be used to alter 20-21 25-10-2024
the size of a dynamically allocated block of
memory.
10. Write a C program to implement a self- 22-23 22-11-2024
referential structure using pointers.
11. Write a C program to demonstrate the use of 24-25 29-11-2024
an array of pointers.
12. Write a C program to demonstrate the 26-27 06-12-2024
concept of nested structures representing
employee details including department
information.
13. Write a C program to open a file, write data 28-29 13-12-2024
to it, and then close the file.
14. Write a C program to create a structure 30-31 20-12-2024
representing employee details and write
them into a file.
15. Write a C program to demonstrate error 32-33 21-12-2024
handling while reading from and writing to
a file.
2
16. Write a C++ program to demonstrate the 34-35 18-10-2024
overloading of a unary operator.
17. Write a C++ program to demonstrate the 36-37 19-10-2024
overloading of a binary operator.
18. Write a C++ program involving the type 38-39 25-10-2024
conversion from a basic data type to class
type.
19. Write a C++ program involving the type 40-41 22-11-2024
conversion from one class type to another
class.
20. Write a C++ program involving overriding 42-43 29-11-2024
of member function.
21. Write a C++ program involving a virtual 44-45 06-12-2024
function.
22. Write a C++ program to demonstrate 46-47 13-12-2024
function overloading.
23. Write a C++ program involving multiple 48-49 20-12-2024
catch statements for a try block.
24. Write a C++ program involving the 50-51 21-12-2024
handling of exceptions in constructors and
destructors.
25. Write a C++ programs using “this” Pointer. 52-53 27-12-2024
3
Output:
4
Program 1: Write a C program to take input from the user and write it into a
file.
#include <stdio.h>
int main() {
char name[100];
int age;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("\nHello %s, you are %d years old.\n", name, age);
return 0;
}
5
Output:
6
Program 2: Write a C program to demonstrate the use of various data types
including int, float, char, and double.
#include <stdio.h>
int main() {
int integerValue;
float floatValue;
char charValue;
double doubleValue;
printf("Enter an integer value: ");
scanf("%d", &integerValue);
printf("Enter a float value: ");
scanf("%f", &floatValue);
printf("Enter a character value: ");
scanf(" %c", &charValue);
printf("Enter a double value: ");
scanf("%lf", &doubleValue);
printf("\nYou entered:\n");
printf("Integer: %d\n", integerValue);
printf("Float: %.2f\n", floatValue);
printf("Character: %c\n", charValue);
printf("Double: %.2lf\n", doubleValue);
return 0;
}
7
Output 1:
Output 2:
8
Program 3: Write a C program to check whether the given string is palindrome
or not without using in built function.
#include <stdio.h>
int main() {
char str[100];
int length = 0;
int i, isPalindrome = 1;
printf("Enter a name: ");
scanf("%s", str);
while (str[length] != '\0') {
length++;
}
for (i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome) {
printf("The string \"%s\" is a palindrome.\n", str);
} else {
printf("The string \"%s\" is not a palindrome.\n", str);
}
return 0;
}
9
Output:
10
Program 4: Write a C program to read N integers into an array A and to find the(i)
sum of odd numbers, (ii) sum of even numbers, (iii) average of all numbers.
#include <stdio.h>
int main() {
int N, i;
int sumOdd = 0, sumEven = 0;
float average = 0.0;
int Number[10];
printf("Enter the number of integers (N): ");
scanf("%d", &N);
printf("Enter %d integers:\n", N);
for (i = 0; i < N; i++) {
scanf("%d", &Number[i]);
}
int totalSum = 0;
for (i = 0; i < N; i++) {
totalSum += Number[i];
if (Number[i] % 2 == 0) {
sumEven += Number[i];
} else {
sumOdd += Number[i];
}
}
average = (float)totalSum / N;
printf("\nSum of odd numbers: %d\n", sumOdd);
printf("Sum of even numbers: %d\n", sumEven);
printf("Average of all numbers: %.2f\n", average);
return 0;
}
11
Output:
12
Program 5: Write a C program to find nth term of Fibonacci series.
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
printf("Enter the term number (n) to find in the Fibonacci series: ");
scanf("%d", &n);
if (n < 0) {
printf("Please enter a non-negative integer.\n");
} else {
int result = fibonacci(n);
printf("The %dth term of the Fibonacci series is: %d\n", n, result);
}
return 0;
}
13
Output:
14
Program 6: Write a program in C using functions to swap two numbers using global
variables concept and call by reference concept.
#include <stdio.h>
int num1, num2;
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("\nBefore swapping:\n");
printf("num1 = %d\n", num1);
printf("num2 = %d\n", num2);
swap(&num1, &num2);
printf("\nAfter swapping:\n");
printf("num1 = %d\n", num1);
printf("num2 = %d\n", num2);
return 0;
}
15
Output :
16
Program 7: Write a C program to maintain a record of “n” students details using an
array of structures with four fields (roll no, name, marks, and grade). Assume
appropriate datatype for each field. Print the marks of the student given the student’s
name as input.
#include <stdio.h>
#include <string.h>
struct Student {
int rollNo;
char name[50];
float marks;
char grade;
};
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollNo);
printf("Name: ");
scanf(" %[^\n]", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
students[i].grade = (students[i].marks >= 90) ? 'A' :
(students[i].marks >= 80) ? 'B' :
(students[i].marks >= 70) ? 'C' :
(students[i].marks >= 60) ? 'D' : 'F';
}
char searchName[50];
printf("\nEnter the name of the student to search: ");
scanf(" %[^\n]", searchName);
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(students[i].name, searchName) == 0) {
printf("\nMarks of %s: %.2f\n", students[i].name, students[i].marks);
found = 1;
break;
}
}
if (!found) {
printf("\nStudent not found.\n");
}
return 0;
}
17
Output:
18
Program 8: Write a C program to perform string manipulation operations such as
concatenation, copying, and comparison.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], result[200];
printf("Enter string 1: ");
scanf("%s", str1);
printf("Enter string 2: ");
scanf("%s", str2);
strcpy(result, str1); // Copy str1 to result
strcat(result, str2); // Concatenate str2 to result
printf("\nConcatenated string: %s\n", result);
strcpy(str2, str1); // Copy str1 to str2
printf("\nCopied string: %s\n", str2);
int compareResult = strcmp(str1, str2);
if (compareResult == 0) {
printf("\nStrings are equal.\n");
} else if (compareResult < 0) {
printf("\nString 1 is lexicographically less than String 2.\n");
} else {
printf("\nString 1 is lexicographically greater than String 2.\n");
}
return 0;
}
19
Output:
20
Program 9: Explain how realloc() can be used to alter the size of a dynamically
allocated block of memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, i;
int initial_size = 5;
int new_size = 10;
ptr = (int*)malloc(initial_size * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
for (i = 0; i < initial_size; i++) {
ptr[i] = i;
}
ptr = (int*)realloc(ptr, new_size * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed.\n");
return 1;
}
for (i = initial_size; i < new_size; i++) {
ptr[i] = i;
}
for (i = 0; i < new_size; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
free(ptr);
return 0;
}
21
Output:
22
Program 10: Write a C program to implement a self-referential structure using
pointers.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
printf("Linked List: ");
displayList(head);
return 0;
}
23
Output:
24
Program 11: Write a C program to demonstrate the use of an array of pointers
#include <stdio.h>
int main() {
int data[] = {10, 20, 30, 40, 50};
int *ptr[5];
for (int i = 0; i < 5; i++) {
ptr[i] = &data[i];
}
printf("Values accessed using pointers:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr[i]);
}
printf("\n");
return 0;
}
25
Output:
26
Program 12: Write a C program to demonstrate the concept of nested structures
representing employee details including department information.
#include <stdio.h>
#include <string.h>
struct Department {
int deptId;
char deptName[50];
};
struct Employee {
int empId;
char empName[50];
struct Department dept;
float salary;
};
int main() {
struct Employee emp1;
[Link] = 101;
strcpy([Link], "John Doe");
[Link] = 10; // Department ID
strcpy([Link], "Sales"); // Department Name
[Link] = 55000.50;
printf("Employee ID: %d\n", [Link]);
printf("Employee Name: %s\n", [Link]);
printf("Department ID: %d\n", [Link]);
printf("Department Name: %s\n", [Link]);
printf("Salary: %.2f\n", [Link]);
return 0;
}
27
Output:
28
Program 13: Write a C program to open a file, write data to it, and then close the file.
#include <stdio.h>
int main() {
FILE *fptr;
char data[] = "This is the data to be written to the file.\n";
fptr = fopen("my_file.txt", "w");
if (fptr == NULL) {
printf("Error: Could not open file.\n");
return 1;
}
fprintf(fptr, "%s", data);
fclose(fptr);
printf("Data written to file successfully.\n");
return 0;
}
29
Output:
30
Program 14: Write a C program to create a structure representing employee details
and write them into a file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
FILE *fptr;
struct Employee emp;
fptr = fopen("[Link]", "w");
if (fptr == NULL) {
printf("Error: Could not open file.\n");
return 1;
}
printf("Enter employee details:\n");
printf("ID: ");
scanf("%d", &[Link]);
printf("Name: ");
scanf("%s", [Link]);
printf("Salary: ");
scanf("%f", &[Link]);
fwrite(&emp, sizeof(struct Employee), 1, fptr);
fclose(fptr);
printf("Employee data written to file successfully.\n");
return 0;
}
31
Output:
32
Program 15: Write a C program to demonstrate error handling while reading from
and writing to a file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *fptr;
char data[100];
fptr = fopen("my_file.txt", "w");
if (fptr == NULL) {
fprintf(stderr, "Error: Could not open file for writing.\n");
exit(1);
}
printf("Enter data to write: ");
fgets(data, sizeof(data), stdin);
if (fprintf(fptr, "%s", data) < 0) {
fprintf(stderr, "Error: Failed to write to file.\n");
fclose(fptr);
exit(1);
}
fclose(fptr);
fptr = fopen("my_file.txt", "r");
if (fptr == NULL) {
fprintf(stderr, "Error: Could not open file for reading.\n");
exit(1);
}
if (fgets(data, sizeof(data), fptr) == NULL) {
fprintf(stderr, "Error: Failed to read from file.\n");
fclose(fptr);
exit(1);
}
printf("Data read from file: %s", data);
fclose(fptr);
return 0;
}
33
Output:
34
Program 16: Write a C++ program to demonstrate the overloading of a unary
operator.
#include <iostream>
using namespace std;
class Number {
private:
int num;
public:
Number(int n) : num(n) {}
Number operator-() const {
return Number(-num);
}
void display() const {
cout << "Number: " << num << endl;
}
};
int main() {
Number obj1(10);
cout << "Original object:" << endl;
[Link]();
Number obj2 = -obj1;
cout << "Negated object:" << endl;
[Link]();
return 0;
}
35
Output:
36
Program 17. Write a C++ program to demonstrate the overloading of a binary
operator.
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + [Link], imag + [Link]);
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2.5, 3.1);
Complex c2(1.2, -2.7);
Complex c3 = c1 + c2; // Use the overloaded + operator
cout << "c1: ";
[Link]();
cout << "c2: ";
[Link]();
cout << "c1 + c2: ";
[Link]();
return 0;
}
37
Output:
38
Program 18: Write a C++ program involving the type conversion from a basic data
type to class type.
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
float inches;
public:
Distance(int f, float i) : feet(f), inches(i) {}
Distance(int f) : feet(f), inches(0.0) {}
void display() const {
cout << feet << " feet " << inches << " inches" << endl;
}
};
int main() {
Distance d1 = 10;
Distance d2 = Distance(5);
[Link]();
[Link]();
return 0;
}
39
Output:
40
Program 19: Write a C++ program involving the type conversion from one class type
to another class.
#include <iostream>
using namespace std;
class ClassA {
private:
int valueA;
public:
ClassA(int a) : valueA(a) {}
int getValueA() const {
return valueA;
}
};
class ClassB {
private:
int valueB;
public:
ClassB(const ClassA& objA) {
valueB = [Link]() * 2;
}
operator ClassA() const {
return ClassA(valueB / 2); // Reverse the operation for demonstration
}
void display() const {
cout << "Value in ClassB: " << valueB << endl;
}
};
int main() {
ClassA objA(10); // Create an object of ClassA
cout << "Original value in ClassA: " << [Link]() << endl;
ClassB objB = objA;
[Link](); // Display the value in ClassB
ClassA objAConverted = objB;
cout << "Converted value back to ClassA: " << [Link]() << endl;
return 0;
}
41
Output:
42
Program 20: Write a C++ program involving overriding of member function.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing a generic shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a rectangle" << endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Rectangle();
shape1->draw();
shape2->draw();
delete shape1;
delete shape2;
return 0;
}
43
Output:
44
Program 21: Write a C++ program involving a virtual function.
#include <iostream>
using namespace std;
class Amity {
public:
virtual void AUMP() {
cout << "Amity University Madhya Pradesh" << endl;
}
};
class ASET : public Amity {
public:
void AUMP() override {
cout << "ASET is a Department in AUMP." << endl;
}
};
class ABS : public Amity {
public:
void AUMP() override {
cout << "ABS is also a Department in AUMP." << endl;
}
};
int main() {
Amity* Department1 = new ASET();
Amity* Department2 = new ABS();
Department1->AUMP();
Department2->AUMP();
delete Department1;
delete Department2;
return 0;
}
45
Output:
46
Program 22: Write a C++ program to demonstrate function overloading.
#include<iostream>
using namespace std;
class ASET {
public:
void func(int roll_no) {
cout<<"The Roll Number is: "<<roll_no<<endl;
}
void func(double cgpa) {
cout<<"The CGPA is: "<<cgpa<<endl;
}
void func(char section) {
cout<<"The Section is: "<<section<<endl;
}
};
int main() {
ASET MCA;
[Link](17);
[Link](8.6);
[Link]('A');
return 0;
}
47
Output:
48
Program 23: Write a C++ program involving multiple catch statements for a try
block.
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
try {
if (num2 == 0) {
throw "Division by zero error!";
} else {
cout << "Result: " << num1 / num2 << endl;
}
}
catch (const char* msg) {
cerr << "Error: " << msg << endl;
}
catch (int error_code) {
cerr << "Error code: " << error_code << endl;
}
return 0;
}
49
Output:
50
Program 24: Write a C++ program involving the handling of exceptions in
constructors and destructors.
#include <iostream>
using namespace std;
class Test {
public:
Test() {
cout << "Constructing an object of class Test " << endl;
}
~Test() {
cout << "Destructing the object of class Test " << endl;
}
};
int main() {
try {
Test t1;
throw 10;
}
catch (int i) {
cout << "Caught " << i << endl;
}
return 0;
}
51
Output:
52
Program 25: Write a C++ programs using “this” Pointer.
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x1, int y1) {
this->x = x1; // Use 'this' to explicitly assign to the class member
this->y = y1;
}
void display() const {
cout << "Point: (" << this->x << ", " << this->y << ")" << endl;
}
Point& getObject() {
return *this;
}
void setX(int x1) {
this->x = x1;
}
void setY(int y1) {
this->y = y1;
}
};
int main() {
Point p1(10, 20);
[Link]();
Point& ref = [Link]();
[Link](30);
[Link](40);
[Link]();
return 0;
}
53