0% found this document useful (0 votes)
7 views14 pages

0464 Lab3

The document contains a series of C programming tasks that cover various concepts such as calculating the area of a rectangle, determining the type of triangle based on its sides, sorting an array of integers, checking for prime numbers, and managing employee records. Each task includes code snippets that demonstrate the implementation of these concepts, including user input and output. Additionally, the document includes memory management and file handling for employee data.

Uploaded by

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

0464 Lab3

The document contains a series of C programming tasks that cover various concepts such as calculating the area of a rectangle, determining the type of triangle based on its sides, sorting an array of integers, checking for prime numbers, and managing employee records. Each task includes code snippets that demonstrate the implementation of these concepts, including user input and output. Additionally, the document includes memory management and file handling for employee data.

Uploaded by

jitonek842
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

ANAS AHMAD L1F23BSCS0464

Operating system

LAB 3

Task 1:
#include<stdio.h>
int main()
{
float length , width;
printf("What is the length of rectangle? ");
scanf("%f" ,&length);
printf("What is widthof rectangle? ");
scanf("%f" , &width);

float Area;
Area = length*width;
printf("Area of rectangle is : %f" , Area);
return 0;
}

TASK2:

#include<stdio.h>
int main()
{
float a,b,c;
printf("Enter the length of three sides of triangle");
printf("side 1: ");
scanf("%f" , &a);
printf("side 2: ");
scanf("%f" , &b);
printf("side 3: ");
scanf("%f" , &c);
if((a+b >c) && (a+c>b) && (b+c > a))
{
if(a == b && b==c){
printf("it is a equilateral triangle \n");
}
else if(a==b || b==c || a==c)
{
printf("it is Isosceles Triangle \n");
}
else {
printf("it is Scalene Triangle \n ");
}
}
else {
printf("it does not form a valid triangle \n");
}
return 0;
}

TASK3:
#include<stdio.h>
int main()
{
int arr[20]
int n=0 , i,j,temp;
printf("enter up to 20 integer enter -99 to stop: \n");
for(i =0;i<n;i++)
{
scanf("%d" , arr[i]);
if(arr[i] == -99)
{
break;
}
++n;
}
if (n==0)
{
printf("no number is entered. \n");
return 0;
}

printf("Original sequence \n");


for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}

printf("sorted sequence \n");


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;
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}

TASK4:
#include<stdio.h>
#include<stdbool.h>
void CheckPrimeNumber();

int main()
{
CheckPrimeNumber();
return 0;
}

void CheckPrimeNumber()
{
int num ,i;
bool isPrime = true;

printf("Enter a positive integer number: ");


scanf("%d" , &num);

if(num < 2)
{
printf("it is not a prime number\n");
return ;
}

for(i = 2 ; i<= num/2;i++)


{
if(num %i ==0 )
{
isPrime = false;
break;
}
}
if(isPrime)
{
printf("it is a prime number \n");
}
else{
printf("it is not a prime number\n");
}
}
TASK5:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <integer1> <integer2>\n", argv[0]);
return 1;
}

int num1 = atoi(argv[1]);


int num2 = atoi(argv[2]);
int result = num1 * num2;

printf("Result: %d * %d = %d\n", num1, num2, result);


return 0;
}

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

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

if (n < 2) {
printf("Array must contain at least two elements.\n");
return 1;
}

int *arr = (int *)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int min, second_min;

if (arr[0] < arr[1]) {


min = arr[0];
second_min = arr[1];
} else if (arr[0] > arr[1]) {
min = arr[1];
second_min = arr[0];
} else {
min = second_min = arr[0];
}

for (int i = 2; i < n; i++) {


if (arr[i] < min) {
second_min = min;
min = arr[i];
} else if (arr[i] > min && (arr[i] < second_min || min == second_min)) {
second_min = arr[i];
}
}

if (min == second_min) {
printf("There is no second minimum (all elements may be equal).\n");
} else {
printf("Second minimum number: %d\n", second_min);
}

free(arr);
return 0;
}
TASK 7:
#include <stdio.h>

struct Employee {
int id;
char name[50];
int age;
float pay;
};

int main() {
struct Employee emp;

printf("Enter Employee ID: ");


scanf("%d", &[Link]);

printf("Enter Employee Name: ");


scanf(" %[^\n]", [Link]); // Reads string with spaces

printf("Enter Employee Age: ");


scanf("%d", &[Link]);

printf("Enter Employee Pay: ");


scanf("%f", &[Link]);

printf("\n--- Employee Details ---\n");


printf("ID : %d\n", [Link]);
printf("Name : %s\n", [Link]);
printf("Age : %d\n", [Link]);
printf("Pay : %.2f\n", [Link]);

return 0;
}
TASK 8:

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

#define MAX 100


#define FILENAME "[Link]"
#define BASIC_PAY 20000

typedef struct {
char id[10];
char name[50];
char gender;
char position[20];
int experience;
float pay;
} Employee;

Employee employees[MAX];
int count = 0;

void loadData();
void saveData();
void addRecord();
void searchByID();
void showAll();
void showBelowBasicPay();

int main() {
int choice;
loadData();

do {
printf("\n==================== MENU ===================\n");
printf("1. Add a record\n");
printf("2. Search a record by ID\n");
printf("3. Show all records\n");
printf("4. Show employees having pay less than basic pay (20000)\n");
printf("5. Save and exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline

switch(choice) {
case 1: addRecord(); break;
case 2: searchByID(); break;
case 3: showAll(); break;
case 4: showBelowBasicPay(); break;
case 5: saveData(); printf("Data saved. Exiting...\n"); break;
default: printf("Invalid choice. Try again.\n");
}
} while(choice != 5);

return 0;
}

void loadData() {
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("File not found. Starting with empty data.\n");
return;
}

while (fscanf(fp, "%s %s %c %s %d %f",


employees[count].id,
employees[count].name,
&employees[count].gender,
employees[count].position,
&employees[count].experience,
&employees[count].pay) == 6) {
count++;
if (count >= MAX) break;
}

fclose(fp);
}

void saveData() {
FILE *fp = fopen(FILENAME, "w");
if (fp == NULL) {
printf("Error opening file for writing.\n");
return;
}

for (int i = 0; i < count; i++) {


fprintf(fp, "%s %s %c %s %d %.2f\n",
employees[i].id,
employees[i].name,
employees[i].gender,
employees[i].position,
employees[i].experience,
employees[i].pay);
}

fclose(fp);
}
void addRecord() {
if (count >= MAX) {
printf("Maximum number of employees reached.\n");
return;
}

Employee e;
printf("Enter ID: ");
scanf("%s", [Link]);
printf("Enter Name: ");
scanf("%s", [Link]);
printf("Enter Gender (m/f): ");
scanf(" %c", &[Link]);
printf("Enter Position (internee/developer): ");
scanf("%s", [Link]);
printf("Enter Experience (years): ");
scanf("%d", &[Link]);
printf("Enter Pay: ");
scanf("%f", &[Link]);

employees[count++] = e;
printf("Record added successfully.\n");
}

void searchByID() {
char id[10];
printf("Enter ID to search: ");
scanf("%s", id);

for (int i = 0; i < count; i++) {


if (strcmp(employees[i].id, id) == 0) {
printf("\nID: %s\nName: %s\nGender: %c\nPosition: %s\nExperience: %d\nPay: %.2f\n",
employees[i].id,
employees[i].name,
employees[i].gender,
employees[i].position,
employees[i].experience,
employees[i].pay);
return;
}
}

printf("Record not found.\n");


}

void showAll() {
printf("\nAll Employee Records:\n");
for (int i = 0; i < count; i++) {
printf("%s %s %c %s %d %.2f\n",
employees[i].id,
employees[i].name,
employees[i].gender,
employees[i].position,
employees[i].experience,
employees[i].pay);
}
}

void showBelowBasicPay() {
printf("\nEmployees with pay less than %.2f:\n", (float)BASIC_PAY);
int found = 0;
for (int i = 0; i < count; i++) {
if (employees[i].pay < BASIC_PAY) {
printf("%s %s %c %s %d %.2f\n",
employees[i].id,
employees[i].name,
employees[i].gender,
employees[i].position,
employees[i].experience,
employees[i].pay);
found = 1;
}
}
if (!found) {
printf("No employees found with pay less than %.2f\n", (float)BASIC_PAY);
}
}

You might also like