Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PF LAB2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

AHMAD SALEEM

REG SP24-BCS-200-2E

DATE 1-JANUARY-2025

TEACHER SIR JAWAD KHAN

PROGRAM 1:
#include<stdio.h>

int prime(int number) {

if (number <= 1) return 0;

for (int i = 2; i * i <= number; i++) {

if (number % i == 0) return 0;

return 1;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (prime(num))

printf("%d is a prime number.\n", num);

else

printf("%d is not a prime number.\n", num);


return 0;

PROGRAM 2:
#include <stdio.h>

int arraySum(int arr[], int size) {

int sum = 0;

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

sum += arr[i];

return sum;

int main() {

int size;

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);

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

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

}
printf("The sum of the array elements is: %d\n", arraySum(arr, size));

return 0;

PROGRAM 3:

#include <stdio.h>

void transposeMatrix(int rows, int cols, int source[rows][cols], int target[cols][rows]) {

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

for (int j = 0; j < cols; j++) {

target[j][i] = source[i][j];

int main() {

int rows = 4, cols = 5;

int matrix[4][5] = {

{1, 2, 3, 4, 5},

{6, 7, 8, 9, 10},

{11, 12, 13, 14, 15},

{16, 17, 18, 19, 20}

};

int transposed[5][4];
transposeMatrix(rows, cols, matrix, transposed);

printf("Original Matrix:\n");

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

for (int j = 0; j < cols; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

printf("\nTransposed Matrix:\n");

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

for (int j = 0; j < rows; j++) {

printf("%d ", transposed[i][j]);

printf("\n");

return 0;

PROGRAM 4:

#include<stdio.h>

int main() {
int matrix[5][5];

int largest;

printf("Enter elements of a 5x5 matrix:\n");

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

for (int j = 0; j < 5; j++) {

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

largest = matrix[0][0];

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

for (int j = 0; j < 5; j++) {

if (matrix[i][j] > largest) {

largest = matrix[i][j];

printf("The largest number in the matrix is: %d\n", largest);

return 0;

PROGRAM 5:
#include <stdio.h>

#define SEATS 50

void displayMenu() {

printf("\nPlease type 1 for \"First Class\"\n");

printf("Please type 2 for \"Economy\"\n");

void printBoardingPass(int seatNumber) {

if (seatNumber <= 20)

printf("Boarding pass: Seat %d (First Class)\n", seatNumber);

else

printf("Boarding pass: Seat %d (Economy)\n", seatNumber);

int main() {

int seats[SEATS] = {0};

int choice, assignedSeat;

while (1) {

displayMenu();

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1) {
assignedSeat = -1;

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

if (seats[i] == 0) {

seats[i] = 1;

assignedSeat = i + 1;

break;

if (assignedSeat == -1) {

printf("First Class is full. Would you like Economy? (1 for Yes, 0 for No): ");

int response;

scanf("%d", &response);

if (response == 1) choice = 2;

else {

printf("Next flight leaves in 3 hours.\n");

continue;

} else {

printBoardingPass(assignedSeat);

continue;

if (choice == 2) {

assignedSeat = -1;
for (int i = 20; i < SEATS; i++) {

if (seats[i] == 0) {

seats[i] = 1;

assignedSeat = i + 1;

break;

if (assignedSeat == -1) {

printf("Economy is full. Would you like First Class? (1 for Yes, 0 for No): ");

int response;

scanf("%d", &response);

if (response == 1) choice = 1;

else {

printf("Next flight leaves in 3 hours.\n");

continue;

} else {

printBoardingPass(assignedSeat);

continue;

printf("Invalid choice. Please try again.\n");

}
return 0;

PROGRAM 6:
#include <stdio.h>

void printCalendar(int month, int year) {

int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

daysInMonth[1] = 29;

int day = 1, startingDay;

int y = year, m = month;

y -= m < 3;

startingDay = (y + y / 4 - y / 100 + y / 400 + "033625514035"[m - 1] - 1) % 7;

printf("\n %s %d\n", months[month - 1], year);

printf(" Sun Mon Tue Wed Thu Fri Sat\n");


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

printf(" ");

for (int i = 1; i <= daysInMonth[month - 1]; i++) {

printf("%5d", i);

if ((i + startingDay) % 7 == 0) {

printf("\n");

printf("\n");

int main() {

int month, year;

printf("Enter month (1-12): ");

scanf("%d", &month);

printf("Enter year: ");

scanf("%d", &year);

if (month < 1 || month > 12 || year < 1) {

printf("Invalid input!\n");

return 1;

}
printCalendar(month, year);

return 0;

PROGRAM 7:
#include <stdio.h>

struct Transaction {

int division;

int product;

int quantity;

float costPerItem;

};

void updateInventory(int inventory[3][4], struct Transaction t) {

inventory[t.division][t.product] += t.quantity;

float calculateInventoryValue(int inventory[3][4], float cost[3][4]) {

float totalValue = 0;

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

for (int j = 0; j < 4; j++) {

totalValue += inventory[i][j] * cost[i][j];

}
}

return totalValue;

int main() {

int inventory[3][4] = {0};

float cost[3][4] = {

{5.0, 7.0, 6.5, 8.0},

{4.5, 6.0, 5.5, 7.0},

{6.0, 8.5, 7.5, 9.0}

};

struct Transaction t;

printf("Enter transaction details:\n");

printf("Division (0-2): ");

scanf("%d", &t.division);

printf("Product (0-3): ");

scanf("%d", &t.product);

printf("Quantity: ");

scanf("%d", &t.quantity);

printf("Cost per item: ");

scanf("%f", &t.costPerItem);

updateInventory(inventory, t);
printf("\nUpdated Inventory:\n");

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

for (int j = 0; j < 4; j++) {

printf("%d ", inventory[i][j]);

printf("\n");

float totalValue = calculateInventoryValue(inventory, cost);

printf("\nTotal inventory value: %.2f\n", totalValue);

return 0;

PROGRAM 8:
#include <stdio.h>

#include <string.h>

struct Student {

char name[50];

int roll;

float marks;

};

int main() {
struct Student students[10];

printf("Enter details of 10 students:\n");

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

printf("\nStudent %d:\n", i + 1);

printf("Name: ");

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

printf("Roll Number: ");

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

printf("Marks: ");

scanf("%f", &students[i].marks);

printf("\nDetails of Students:\n");

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

printf("\nStudent %d:\n", i + 1);

printf("Name: %s\n", students[i].name);

printf("Roll Number: %d\n", students[i].roll);

printf("Marks: %.2f\n", students[i].marks);

return 0;

THE END.

You might also like