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

C lab Assignment

Uploaded by

biplob.py
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C lab Assignment

Uploaded by

biplob.py
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

AIGENANS 1

It seems that you need help writing multiple C programs based on the given requirements.
Below are the detailed solutions for each of the tasks.

1. C Program to demonstrate SWITCH-CASE.


Write C-Programs to show the operations of SWITCH-CASE for the following.

a) Calculator using SWITCH-CASE.

Design a Calculator to show the major mathematical operations on two variables (1-
Addition, 2- Subtraction, 3-Multiplication, 4-Division, 5-Modulo Operations, 6- Square and
the default with a message “Wrong Choice”).

#include <stdio.h>

int main() {
int choice;
float num1, num2;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);

printf("Select an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulo\n");
printf("6. Square of numbers\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Result: %.2f\n", num1 + num2);
break;
case 2:
printf("Result: %.2f\n", num1 - num2);
break;
case 3:
printf("Result: %.2f\n", num1 * num2);
break;
case 4:
if(num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
case 5:
printf("Modulo operation result: %.0f\n", (int)num1 %
(int)num2);
break;
case 6:
printf("Square of %.2f is %.2f\n", num1, num1*num1);
printf("Square of %.2f is %.2f\n", num2, num2*num2);
break;
default:
printf("Wrong Choice!\n");
break;
}
return 0;
}

b) Restaurant Menu with SWITCH-CASE


Design a Menu of a Restaurant showing the BREAKFAST and LUNCH Items. Based on the
choice of the customer, the bills need to be generated. The program should have provision
for multiple items choosing. The final bill should display all the items with a bill.

#include <stdio.h>

int main() {
int choice, item, quantity;
float totalBill = 0;

printf("Welcome to our Restaurant\n");


printf("Select the Meal type:\n");
printf("1. Breakfast\n2. Lunch\nEnter choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Breakfast Menu:\n1. Pancakes - $5\n2. Omelette - $4\n3.
Coffee - $2\n");
printf("Select item: ");
scanf("%d", &item);
printf("Enter quantity: ");
scanf("%d", &quantity);
if(item == 1)
totalBill += 5 * quantity;
else if(item == 2)
totalBill += 4 * quantity;
else if(item == 3)
totalBill += 2 * quantity;
else
printf("Invalid item choice!\n");
break;
case 2:
printf("Lunch Menu:\n1. Burger - $10\n2. Pizza - $12\n3. Salad -
$7\n");
printf("Select item: ");
scanf("%d", &item);
printf("Enter quantity: ");
scanf("%d", &quantity);
if(item == 1)
totalBill += 10 * quantity;
else if(item == 2)
totalBill += 12 * quantity;
else if(item == 3)
totalBill += 7 * quantity;
else
printf("Invalid item choice!\n");
break;
default:
printf("Invalid Meal Choice!\n");
break;
}

printf("Total Bill: $%.2f\n", totalBill);


return 0;
}

2. C Programs Using Functions (Call-by-Value and Call-by-


Reference)
Write C-Programs to show the operations of FUNCTIONS using Call-by-Value and Call-by-
Reference for the following situations.

a) Calculate Sum (Call-by-Value)

Accept two number from the user in main function. Pass these numbers to a function
called the CALCULATE_SUM for finding the sum of these numbers.
#include <stdio.h>

int calculate_sum(int a, int b) {


return a + b;
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

int sum = calculate_sum(num1, num2);


printf("Sum: %d\n", sum);
return 0;
}

b) Find Average (Call-by-Value)


Accept five numbers from the user in the main function and send them to a function
FIND_AVG to get the average number.

#include <stdio.h>

float find_avg(int nums[], int size) {


int sum = 0;
for(int i = 0; i < size; i++) {
sum += nums[i];
}
return (float)sum / size;
}

int main() {
int nums[5];
printf("Enter 5 numbers: ");
for(int i = 0; i < 5; i++) {
scanf("%d", &nums[i]);
}

float avg = find_avg(nums, 5);


printf("Average: %.2f\n", avg);
return 0;
}

c) Sum of Natural Numbers (Call-by-Value)


Generate a natural number between 1 and 100 inside the main function and send it to a
function GET_SUM to find the sum of all the natural numbers up to the given number.
(E.g. use sum=n (n+1)/2).

#include <stdio.h>

int get_sum(int n) {
return (n * (n + 1)) / 2;
}

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

int sum = get_sum(n);


printf("Sum of natural numbers up to %d: %d\n", n, sum);
return 0;
}

3. Array Operations Using SWITCH-CASE


Write C-programs for the following by using the SWITCH-CASE control structure for
performing the given operations.
a) Initialize an array of 10-integer numbers. Accept inputs from the user and store them
in the array (CASE-1).
b) Display the Array elements with their array positions (CASE-2)
c) Find the sum and average of the array elements (CASE-3)
d) Fins the largest and smallest elements in the array (CASE-4)
e) Find the number at a certain position of the array (CASE-5)
f) Find the numbers in the array which are divisible by another user-defined number
(CASE6)
g) Find the sum of the array elements at position X and Y defined by the user (CASE-7).
h) DEFAULT: Check if Array is empty. If so, display a message.

#include <stdio.h>

int main() {
int arr[10], choice, x, y, num;
int sum = 0;
float avg;

printf("Enter 10 integers:\n");
for(int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}

printf("Enter choice:\n");
printf("1. Display Array\n2. Sum & Average\n3. Largest & Smallest\n4.
Find Element by Position\n5. Divisible by number\n6. Sum at position X and
Y\n");
printf("Choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
for(int i = 0; i < 10; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
break;
case 2:
for(int i = 0; i < 10; i++) {
sum += arr[i];
}
avg = sum / 10.0;
printf("Sum: %d, Average: %.2f\n", sum, avg);
break;
case 3:
int largest = arr[0], smallest = arr[0];
for(int i = 1; i < 10; i++) {
if(arr[i] > largest) largest = arr[i];
if(arr[i] < smallest) smallest = arr[i];
}
printf("Largest: %d, Smallest: %d\n", largest, smallest);
break;
case 4:
printf("Enter position (0-9): ");
scanf("%d", &x);
if(x >= 0 && x < 10) {
printf("Element at arr[%d]: %d\n", x, arr[x]);
} else {
printf("Invalid position!\n");
}
break;
case 5:
printf("Enter divisor: ");
scanf("%d", &num);
printf("Elements divisible by %d: ", num);
for(int i = 0; i < 10; i++) {
if(arr[i] % num == 0) {
printf("%d ", arr[i]);
}
}
printf("\n");
break;
case 6:
printf("Enter X and Y (0-9): ");
scanf("%d %d", &x, &y);
if(x >= 0 && x < 10 && y >= 0 && y < 10) {
printf("Sum of arr[%d] and arr[%d]: %d\n", x, y, arr[x] +
arr[y]);
} else {
printf("Invalid positions!\n");
}
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}

4. Matrix Operations
Write C-programs for the matrix operations (i.e. 2-D Arrays) –

a) Matrix Addition
ADDITION of corresponding elements of the matrices

#include <stdio.h>

int main() {
int a[2][2], b[2][2], sum[2][2];
printf("Enter elements of first 2x2 matrix:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of second 2x2 matrix:\n");


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}

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


for(int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}

printf("Sum of matrices:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}

b) Matrix Subtraction
SUBTRACTION of corresponding elements of 2nd matrix from the 1st matrix.

#include <stdio.h>

int main() {
int a[2][2], b[2][2], difference[2][2];

// Input first matrix


printf("Enter elements of first 2x2 matrix:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}

// Input second matrix


printf("Enter elements of second 2x2 matrix:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}
// Subtracting corresponding elements
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
difference[i][j] = a[i][j] - b[i][j];
}
}

// Display result
printf("Difference of matrices:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", difference[i][j]);
}
printf("\n");
}

return 0;
}

c) Matrix Multiplication (Assuming Compatibility)


MULTIPLICATION of the two given matrices if the matrix operation for multiplications
holds fit (i.e. rows and column count matches) in the matrices.

For matrix multiplication, the number of columns in the first matrix must equal the
number of rows in the second matrix. For this example, we'll multiply two 2x2 matrices.

#include <stdio.h>

int main() {
int a[2][2], b[2][2], product[2][2];

// Input first matrix


printf("Enter elements of first 2x2 matrix:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}

// Input second matrix


printf("Enter elements of second 2x2 matrix:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}

// Multiplying the matrices


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
product[i][j] = 0;
for(int k = 0; k < 2; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}

// Display result
printf("Product of matrices:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}

return 0;
}

5. STRUCT Operations in C
a) Bank Account Structure (Continued)
Create Bank Accounts for Two persons with the NAME, ACC_NO, and MIN_BALANCE.
Take the STRUCT instances as Person_1 and Person_2.

#include <stdio.h>

struct BankAccount {
char name[50];
int acc_no;
float min_balance;
};

int main() {
// Create two bank accounts
struct BankAccount person1 = {"John Doe", 101, 1000.0};
struct BankAccount person2 = {"Alice Smith", 102, 1500.0};
// Display account details
printf("Account 1: %s, Account No: %d, Min Balance: %.2f\n",
person1.name, person1.acc_no, person1.min_balance);
printf("Account 2: %s, Account No: %d, Min Balance: %.2f\n",
person2.name, person2.acc_no, person2.min_balance);

return 0;
}

b) Vehicle Details Structure


Describe 4-wheeler Vehicle Details based on the candidate Type in the STRUCT. Take the
variables VEH_NAME, VEH_TYPE, VEH_DETAILS. Define only three instances VEH1, 2 &
3.

#include <stdio.h>

struct Vehicle {
char veh_name[50];
char veh_type[20];
char veh_details[100];
};

int main() {
// Create vehicle details for three vehicles
struct Vehicle veh1 = {"Toyota", "SUV", "Color: Red, Engine: V8, Year:
2022"};
struct Vehicle veh2 = {"Honda", "Sedan", "Color: Blue, Engine: V6, Year:
2021"};
struct Vehicle veh3 = {"BMW", "Coupe", "Color: Black, Engine: V6, Year:
2023"};

// Display vehicle details


printf("Vehicle 1: %s, Type: %s, Details: %s\n", veh1.veh_name,
veh1.veh_type, veh1.veh_details);
printf("Vehicle 2: %s, Type: %s, Details: %s\n", veh2.veh_name,
veh2.veh_type, veh2.veh_details);
printf("Vehicle 3: %s, Type: %s, Details: %s\n", veh3.veh_name,
veh3.veh_type, veh3.veh_details);

return 0;
}

Summary of the Provided Programs

You might also like