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

PBL Assignment 1-5

Uploaded by

niksgaming1112
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)
28 views

PBL Assignment 1-5

Uploaded by

niksgaming1112
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/ 37

DR. D. Y.

PATIL SCHOOL OF SCIENCE & TECHNOLOGY


DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3rd cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)
(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)

Date: 10/01/2024

Assignment No: 1
Problem Statement: Design a program to simulate a basic ATM machine function allowing users to check
balance, deposit, and withdraw funds.

Source Code:

#include <stdio.h>

unsigned long amount=100000, deposit, withdraw;

int choice, pin, k;

char transaction ='y';

void main()

while (pin != 2004)

printf("ENTER YOUR SECRET PIN NUMBER:");

scanf("%d", &pin);

if (pin != 2004)

printf("PLEASE ENTER VALID PASSWORD\n");

do

printf("********Welcome to ATM Service**************\n");


printf("1. Saving Account\n");

printf("2. Current Account\n");

printf("3. Check Balance\n");

printf("4. Withdraw Cash\n");

printf("5. Deposit Cash\n");

printf("6. Quit\n");

printf("******************?**************************?*\n\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("\n Choose from saving Account : %lu ", amount);

break;

case 2:

printf("\n Choose from Current Account : %lu ", amount);

break;

case 3:

printf("\n YOUR BALANCE IN Rs : %lu ", amount);

break;

case 4:

printf("\n ENTER THE AMOUNT TO WITHDRAW: ");

scanf("%lu", &withdraw);

if (withdraw % 100 != 0)

printf("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");

else if (withdraw >(amount - 500))


{

printf("\n INSUFFICENT BALANCE");

else

amount = amount - withdraw;

printf("\n\n PLEASE COLLECT CASH");

printf("\n YOUR CURRENT BALANCE IS%lu", amount);

break;

case 5:

printf("\n ENTER THE AMOUNT TO DEPOSIT");

scanf("%lu", &deposit);

amount = amount + deposit;

printf("YOUR BALANCE IS %lu", amount);

break;

case 6:

printf("\n THANK U USING ATM");

break;

default:

printf("\n INVALID CHOICE");

printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");

fflush(stdin);

scanf("%c", &transaction);

if (transaction == 'n'|| transaction == 'N')

k = 1;

} while (!k);

printf("\n\n THANKS FOR USING OUT ATM SERVICE");


}

Sample Output:
DR. D. Y. PATIL SCHOOL OF SCIENCE & TECHNOLOGY
DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3rd cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)
(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)

Date: 18/01/2024

Assignment No: 2
Problem Statement: Implement a basic to-do list program with add, delete, and display functionalities.

Source Code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_TASKS 100

typedef struct {

char task[100];

} Task;

Task todoList[MAX_TASKS];

int numTasks = 0;

void displayTasks() {

if (numTasks == 0) {

printf("No tasks in the to-do list.\n");

} else {

printf("To-Do List:\n");
for (int i = 0; i < numTasks; i++) {

printf("%d. %s\n", i + 1, todoList[i].task);

void addTask() {

if (numTasks == MAX_TASKS) {

printf("To-do list is full. Cannot add more tasks.\n");

return;

printf("Enter the task: ");

scanf(" %[^\n]s", todoList[numTasks].task);

numTasks++;

printf("Task added successfully!\n");

void deleteTask() {

int taskIndex;

if (numTasks == 0) {

printf("No tasks to delete.\n");

return;

}
displayTasks();

printf("Enter the task number to delete: ");

scanf("%d", &taskIndex);

if (taskIndex < 1 || taskIndex > numTasks) {

printf("Invalid task number.\n");

return;

// Shift tasks to fill the gap

for (int i = taskIndex - 1; i < numTasks - 1; i++) {

strcpy(todoList[i].task, todoList[i + 1].task);

numTasks--;

printf("Task deleted successfully!\n");

int main() {

int choice;

do {

printf("\n1. Display tasks\n");

printf("2. Add task\n");

printf("3. Delete task\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
displayTasks();

break;

case 2:

addTask();

break;

case 3:

deleteTask();

break;

case 4:

printf("Exiting program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 4);

return 0;

Sample Output:

1. Display tasks

2. Add task

3. Delete task

4. Exit
Enter your choice: 2

Enter the task: 50000

Task added successfully!

1. Display tasks

2. Add task

3. Delete task

4. Exit

Enter your choice: 1

To-Do List:

1. 50000

1. Display tasks

2. Add task

3. Delete task

4. Exit

Enter your choice: 4

Exiting program. Goodbye!


DR. D. Y. PATIL SCHOOL OF SCIENCE & TECHNOLOGY
DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3rd cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)
(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)

Date: 29/01/2024

Assignment No: 3
Problem Statement: Design a simple quiz application that asks multiple-choice questions.

Source Code:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#define MAX_QUESTIONS 5

typedef struct {

char question[256];

char options[4][64];

int correct_option;

} Question;

void displayQuestion(Question q)

printf("%s\n", q.question);

for (int i = 0; i < 4; i++)

printf("%d. %s\n", i + 1, q.options[i]);


}

int checkAnswer(Question q, int user_answer)

return (user_answer == q.correct_option);

int main()

srand(time(NULL));

Question original_questions[MAX_QUESTIONS] =

{ "Which bird lays the largest egg?",

{ "Owl", "Ostrich", "Kingfisher", "Woodpecker" },

2 },

{ "How many legs does a spider have?",

{ "7", "8", "6", "5" },

2 },

{ "Where does the President of the United States "

"live while in office?",

{ "The White House", "The Parliament",

"House of Commons", "Washington DC" },

1 },

{ "Which state is famous for Hollywood?",

{ "Sydney", "California", "New York", "Paris" },

2 },

{ "What is a group of lions called?",


{ "Drift", "Pride", "Flock", "Drove" },

2}

};

Question questions[MAX_QUESTIONS];

memcpy(questions, original_questions,

sizeof(original_questions));

int num_questions = MAX_QUESTIONS;

int score = 0;

printf("Hola! Here's your Quiz Game!\n");

for (int i = 0; i < MAX_QUESTIONS; i++)

int random_index = rand() % num_questions;

Question current_question = questions[random_index];

displayQuestion(current_question);

int user_answer;

printf("Enter your answer (1-4): ");

scanf("%d", &user_answer);

if (user_answer >= 1 && user_answer <= 4)

if (checkAnswer(current_question,

user_answer))

printf("Correct!\n");
score++;

else

printf("Incorrect. The correct answer is "

"%d. %s\n",

current_question.correct_option,

current_question.options

[current_question.correct_option

- 1]);

else

printf("Invalid choice. Please enter a number "

"between 1 and 4.\n");

questions[random_index]

= questions[num_questions - 1];

num_questions--;

printf("Well Done Champ !!!! Quiz completed! Your "

"score: %d/%d\n",

score, MAX_QUESTIONS);

return 0;

}
Sample Output:

Hola! Here's your Quiz Game!

What is a group of lions called?

1. Drift

2. Pride

3. Flock

4. Drove

Enter your answer (1-4): 2

Correct!

Which state is famous for Hollywood?

1. Sydney

2. California

3. New York

4. Paris

Enter your answer (1-4): 2

Correct!

Which bird lays the largest egg?

1. Owl

2. Ostrich

3. Kingfisher

4. Woodpecker

Enter your answer (1-4): 2

Correct!

How many legs does a spider have?

1. 7

2. 8

3. 6

4. 5
Enter your answer (1-4): 2

Correct!

Where does the President of the United States live while in office?

1. The White House

2. The Parliament

3. House of Commons

4. Washington DC

Enter your answer (1-4): 1

Correct!

Well Done Champ !!!! Quiz completed! Your score: 5/5


DR. D. Y. PATIL SCHOOL OF SCIENCE & TECHNOLOGY
DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3rd cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)
(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)

Date: 02/02/2024

Assignment No: 4
Problem Statement: Develop a basic currency converter between different currencies.

Source Code: z

#include <stdio.h>

int main()

float amount;

float rupee, dollar, pound, euro;

int choice;

printf("Following are the Choices:");

printf("\nEnter 1: Ruppe");

printf("\nEnter 2: Dollar");

printf("\nEnter 3: Pound");

printf("\nEnter 4: Euro");

printf("\nEnter your choice: ");


scanf("%d", &choice);

printf("Enter the amount you want to convert?\n");

scanf("%f", &amount);

switch (choice)

case 1: // Ruppe Conversion

dollar = amount / 70;

printf("%.2f Rupee = %.2f dollar", amount, dollar);

pound = amount / 88;

printf("\n%.2f Rupee = %.2f pound", amount, pound);

euro = amount / 80;

printf("\n%.2f Rupee = %.2f euro", amount, euro);

break;

case 2: // Dollar Conversion

rupee = amount * 70;

printf("\n%.2f Dollar = %.2f rupee", amount, rupee);

pound = amount *0.78;

printf("\n%.2f Dollar = %.2f pound", amount, pound);


euro = amount *0.87;

printf("\n%.2f Dollar = %.2f euro", amount, euro);

break;

case 3: // Pound Conversion

rupee = amount * 88;

printf("\n%.2f Pound = %.2f rupee", amount, rupee);

dollar = amount *1.26;

printf("\n%.2f Pound = %.2f dollar", amount, dollar);

euro = amount *1.10;

printf("\n%.2f Pound = %.2f euro", amount, euro);

break;

case 4: // Euro Conversion

rupee = amount * 80;

printf("\n%.2f Euro = %.2f rupee", amount, rupee);

dollar = amount *1.14;

printf("\n%.2f Euro = %.2f dollar", amount, dollar);

pound = amount *0.90;


printf("\n.2%f Euro = %.2f pound", amount, pound);

break;

//Default case

default:

printf("\nInvalid Input");

return 0;

Sample Output:

Following are the Choices:

Enter 1: Ruppe

Enter 2: Dollar

Enter 3: Pound

Enter 4: Euro

Enter your choice: 2

Enter the amount you want to convert?

20000

20000.00 Dollar = 1400000.00 rupee

20000.00 Dollar = 15600.00 pound

20000.00 Dollar = 17400.00 euro


DR. D. Y. PATIL SCHOOL OF SCIENCE & TECHNOLOGY
DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3 cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
rd

(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)


(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)

Date: 09/02/2024

Assignment No: 5
Problem Statement:

Implement a simple text-based game, like a number guessing game or tic-tac-toe.

Source code:
#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define COMPUTER 1

#define HUMAN 2

#define SIDE 3

#define COMPUTERMOVE 'O'

#define HUMANMOVE 'X'

struct Move {

int row, col;

};
char player = 'x', opponent = 'o';

bool isMovesLeft(char board[3][3])

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

for (int j = 0; j < 3; j++)

if (board[i][j] == '_')

return true;

return false;

int evaluate(char b[3][3])

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

if (b[row][0] == b[row][1]

&& b[row][1] == b[row][2]) {

if (b[row][0] == player)

return +10;

else if (b[row][0] == opponent)

return -10;

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


if (b[0][col] == b[1][col]

&& b[1][col] == b[2][col]) {

if (b[0][col] == player)

return +10;

else if (b[0][col] == opponent)

return -10;

if (b[0][0] == b[1][1] && b[1][1] == b[2][2]) {

if (b[0][0] == player)

return +10;

else if (b[0][0] == opponent)

return -10;

if (b[0][2] == b[1][1] && b[1][1] == b[2][0]) {

if (b[0][2] == player)

return +10;

else if (b[0][2] == opponent)

return -10;

return 0;

}
int minimax(char board[3][3], int depth, bool isMax)

int score = evaluate(board);

if (score == 10)

return score;

if (score == -10)

return score;

if (isMovesLeft(board) == false)

return 0;

if (isMax) {

int best = -1000;

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

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

if (board[i][j] == '_') {

board[i][j] = player;
int val

= minimax(board, depth + 1, !isMax);

if (val > best) {

best = val;

board[i][j] = '_';

return best;

else {

int best = 1000;

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

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

if (board[i][j] == '_') {

board[i][j] = opponent;
int val

= minimax(board, depth + 1, !isMax);

if (val < best) {

best = val;

board[i][j] = '_';

return best;

struct Move findBestMove(char board[3][3])

int bestVal = -1000;

struct Move bestMove;

bestMove.row = -1;

bestMove.col = -1;

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

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

if (board[i][j] == '_') {
board[i][j] = player;

int moveVal = minimax(board, 0, false);

board[i][j] = '_';

if (moveVal > bestVal) {

bestMove.row = i;

bestMove.col = j;

bestVal = moveVal;

return bestMove;

void showBoard(char board[][SIDE])

printf("\n\n");

printf("\t\t\t %c | %c | %c \n", board[0][0],

board[0][1], board[0][2]);

printf("\t\t\t--------------\n");

printf("\t\t\t %c | %c | %c \n", board[1][0],


board[1][1], board[1][2]);

printf("\t\t\t--------------\n");

printf("\t\t\t %c | %c | %c \n\n", board[2][0],

board[2][1], board[2][2]);

void showInstructions()

printf("\t\t\t Tic-Tac-Toe\n\n");

printf("Choose a cell numbered from 1 to 9 as below "

"and play\n\n");

printf("\t\t\t 1 | 2 | 3 \n");

printf("\t\t\t--------------\n");

printf("\t\t\t 4 | 5 | 6 \n");

printf("\t\t\t--------------\n");

printf("\t\t\t 7 | 8 | 9 \n\n");

printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");

void initialise(char board[][SIDE], int moves[])

srand(time(NULL));
for (int i = 0; i < SIDE; i++) {

for (int j = 0; j < SIDE; j++)

board[i][j] = ' ';

for (int i = 0; i < SIDE * SIDE; i++)

moves[i] = i;

for (int i = 0; i < SIDE * SIDE; i++) {

int randIndex = rand() % (SIDE * SIDE);

int temp = moves[i];

moves[i] = moves[randIndex];

moves[randIndex] = temp;

void declareWinner(int whoseTurn)

if (whoseTurn == COMPUTER)

printf("COMPUTER has won\n");

else

printf("HUMAN has won\n");

}
int rowCrossed(char board[][SIDE])

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

if (board[i][0] == board[i][1]

&& board[i][1] == board[i][2]

&& board[i][0] != ' ')

return 1;

return 0;

int columnCrossed(char board[][SIDE])

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

if (board[0][i] == board[1][i]

&& board[1][i] == board[2][i]

&& board[0][i] != ' ')

return 1;

return 0;

int diagonalCrossed(char board[][SIDE])


{

if ((board[0][0] == board[1][1]

&& board[1][1] == board[2][2]

&& board[0][0] != ' ')

|| (board[0][2] == board[1][1]

&& board[1][1] == board[2][0]

&& board[0][2] != ' '))

return 1;

return 0;

int gameOver(char board[][SIDE])

return (rowCrossed(board) || columnCrossed(board)

|| diagonalCrossed(board));

void playTicTacToe(int whoseTurn)

char board[SIDE][SIDE];

int moves[SIDE * SIDE];


initialise(board, moves);

showInstructions();

int moveIndex = 0, x, y;

while (!gameOver(board) && moveIndex != SIDE * SIDE) {

if (whoseTurn == COMPUTER) {

char tempBoard[3][3];

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

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

if (board[i][j] == 'X') {

tempBoard[i][j] = 'x';

else if (board[i][j] == 'O') {

tempBoard[i][j] = 'o';

else {

tempBoard[i][j] = '_';

struct Move thisMove = findBestMove(tempBoard);

x = thisMove.row;
y = thisMove.col;

board[x][y] = COMPUTERMOVE;

printf("COMPUTER has put a %c in cell %d %d\n",

COMPUTERMOVE, x, y);

showBoard(board);

moveIndex++;

whoseTurn = HUMAN;

else if (whoseTurn == HUMAN) {

int move;

printf("Enter your move (1-9): ");

scanf("%d", &move);

if (move < 1 || move > 9) {

printf("Invalid input! Please enter a "

"number between 1 and 9.\n");

continue;

x = (move - 1) / SIDE;

y = (move - 1) % SIDE;

if (board[x][y] == ' ') {

board[x][y] = HUMANMOVE;

showBoard(board);

moveIndex++;

if (gameOver(board)) {

declareWinner(HUMAN);

return;
}

whoseTurn = COMPUTER;

else {

printf("Cell %d is already occupied. Try "

"again.\n",

move);

if (!gameOver(board) && moveIndex == SIDE * SIDE)

printf("It's a draw\n");

else {

if (whoseTurn == COMPUTER)

whoseTurn = HUMAN;

else if (whoseTurn == HUMAN)

whoseTurn = COMPUTER;

declareWinner(whoseTurn);

}
int main()

playTicTacToe(COMPUTER);

return 0;

Sample Output:

Tic-Tac-Toe

Choose a cell numbered from 1 to 9 as below and play

1|2|3

--------------

4|5|6

--------------

7|8|9

COMPUTER has put a O in cell 0 0

O| |

--------------

| |

--------------
| |

Enter your move (1-9): 5

O| |

--------------

|X|

--------------

| |

COMPUTER has put a O in cell 0 1

O|O|

--------------

|X|

--------------

| |

Enter your move (1-9): 3

O|O|X

--------------

|X|

--------------

| |

COMPUTER has put a O in cell 1 0

O|O|X
--------------

O|X|

--------------

| |

Enter your move (1-9): 7

O|O|X

--------------

O|X|

--------------

X| |

HUMAN has wo

You might also like