Worksheet 1
Worksheet 1
C Programming
1. Printing Environment Variables
Code:
#include <stdio.h>
#include <stdlib.h>
Question: What is the purpose of the div() function in C, and how does it work? Provide an
example.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
div_t result = div(10, 3);
printf("Quotient: %d, Remainder: %d\n", result.quot, result.rem);
return 0;
}
Output:
Quotient: 3, Remainder: 1
Question: How do you use scanf() to read the date in the form 'dd-mm-yy'?
Code:
int day, month, year;
scanf("%d-%d-%d", &day, &month, &year);
printf("Day: %d, Month: %d, Year: %d\n", day, month, year);
Input:
26-02-25
Output:
C++ Programming
Question: Write a program that implements a date class containing day, month, and year.
Code:
#include <iostream>
using namespace std;
class Date {
public:
int day, month, year;
Date(int d, int m, int y) : day(d), month(m), year(y) {}
void display() { cout << day << "-" << month << "-" << year << endl; }
};
int main() {
Date today(26, 2, 2025);
today.display();
return 0;
}
Output:
26-2-2025
Data Structures
1. Tree Traversal Algorithms
Question: Traverse the given tree using Inorder, Preorder, and Postorder traversals.
Code:
struct Node {
int data;
Node* left;
Node* right;
};
SQL Queries
1. Recovered Development Cost
Question: Display the details of packages for which the development cost has been recovered.
Query:
Query:
Question: Find the longest increasing subsequence (LIS) in a given array of integers.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
cout << LIS(arr) << endl;
return 0;
}
Input:
9
15 27 14 38 26 55 46 65 85
Output:
Question: Print the freedom fighter pattern for a given number of photos.
Code:
#include <iostream>
using namespace std;
void printPattern(int n) {
for (int i = 0; i < 2 * n - 1; i++) {
int spaces = abs(n - 1 - i) * 2;
for (int j = 0; j < spaces; j++) cout << "-";
for (char ch = 'e' - abs(n - 1 - i); ch <= 'e'; ch++) cout << ch << "-";
for (char ch = 'e' - 1; ch >= 'e' - abs(n - 1 - i); ch--) cout << ch << (ch ==
'e' - abs(n - 1 - i) ? "" : "-");
cout << endl;
}
}
int main() {
int n;
cin >> n;
printPattern(n);
return 0;
}
Input:
Output:
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------