C lab Assignment
C lab Assignment
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.
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;
}
#include <stdio.h>
int main() {
int choice, item, quantity;
float totalBill = 0;
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;
}
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 main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
#include <stdio.h>
int main() {
int nums[5];
printf("Enter 5 numbers: ");
for(int i = 0; i < 5; i++) {
scanf("%d", &nums[i]);
}
#include <stdio.h>
int get_sum(int n) {
return (n * (n + 1)) / 2;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
#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("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];
// 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;
}
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];
// 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;
}
#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"};
return 0;
}