QC101: Calculate A Bike's Average Consumption: Sure, I'll Provide The Solutions For Each of The Tasks Mentioned
QC101: Calculate A Bike's Average Consumption: Sure, I'll Provide The Solutions For Each of The Tasks Mentioned
c Copy code
#include <stdio.h> int main() { int distance; float fuel; printf("Enter total distance
traveled (in km): "); scanf("%d", &distance); printf("Enter spent fuel (in liters): ");
scanf("%f", &fuel); float averageConsumption = distance / fuel; printf("Average
consumption (km/lt): %.2f\n", averageConsumption); return 0; }
c Copy code
#include <stdio.h> int main() { int units; float bill; printf("Enter the number of
units consumed: "); scanf("%d", &units); if (units <= 199) { bill = units * 1.20; } else
if (units >= 200 && units <= 399) { bill = units * 1.50; } else if (units >= 400 &&
units <= 599) { bill = units * 1.80; } else { bill = units * 2.00; } printf("Electricity
Bill: %.2f Rs\n", bill); return 0; }
c Copy code
#include <stdio.h> void add(int num1, int num2) { printf("Result: %d\n", num1 + num2);
} void sub(int num1, int num2) { printf("Result: %d\n", num1 - num2); } void mul(int
num1, int num2) { printf("Result: %d\n", num1 * num2); } void div(int num1, int num2) {
if (num2 != 0) { printf("Result: %.2f\n", (float)num1 / num2); } else { printf("Division
by zero is not allowed.\n"); } } int main() { int num1, num2; char op; printf("Enter two
numbers: "); scanf("%d %d", &num1, &num2); printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op); switch (op) { case '+': add(num1, num2); break; case '-': sub(num1,
num2); break; case '*': mul(num1, num2); break; case '/': div(num1, num2); break;
default: printf("Invalid operator.\n"); break; } return 0; }
c Copy code
#include <stdio.h> void printPattern() { for (int i = 0; i < 7; i++) { for (int j = 0;
j < 7; j++) { if (i == 0 || i == 6 || j == 0 || j == 6) { printf("*"); } else { printf("
"); } } printf("\n"); } } int main() { printPattern(); return 0; }
QC301: Permutations of an array of strings in lexicographical order
c Copy code
#include <stdio.h> #include <string.h> #include <stdlib.h> int cmpfunc(const void *a,
const void *b) { return strcmp(*(const char **)a, *(const char **)b); } void swap(char
**a, char **b) { char *temp = *a; *a = *b; *b = temp; } void permute(char *arr[], int l,
int r) { if (l == r) { for (int i = 0; i <= r; i++) { printf("%s ", arr[i]); }
printf("\n"); } else { for (int i = l; i <= r; i++) { swap((arr + l), (arr + i));
permute(arr, l + 1, r); swap((arr + l), (arr + i)); // backtrack } } } int main() { char
*arr[] = {"wx", "xy", "yz"}; int n = sizeof(arr) / sizeof(arr[0]); qsort(arr, n,
sizeof(char *), cmpfunc); printf("Permutations:\n"); permute(arr, 0, n - 1); return 0;
}
QC302: Read and print reverse, rank, and transpose of the matrix
c Copy code
c Copy code
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; };
struct Node* findMergeNode(struct Node *headA, struct Node *headB) { struct Node
*currentA = headA; struct Node *currentB = headB; while (currentA != currentB) {
currentA = (currentA == NULL) ? headB : currentA->next; currentB = (currentB == NULL) ?
headA : currentB->next; } return currentA; } struct Node* newNode(int data) { struct
Node *node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next =
NULL; return node; } int main() { struct Node *newNode; struct Node *head1 =
newNode(10); head1->next = newNode(20); head1->next->next = newNode(30); head1->next-
>next->next = newNode(40); head1->next->next->next->next = newNode(50); struct Node
*head2 = newNode(15); head2->next = head1->next->next; struct Node *mergeNode =
findMergeNode(head1, head2); if (mergeNode != NULL) { printf("The merge point is %d\n",
mergeNode->data); } else { printf("No merge point found\n"); } return 0; }
QC402: Insert a new node at the head and tail of a doubly linked list
c Copy code
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next;
struct Node *prev; }; struct Node* createNode(int data) { struct Node *newNode = (struct
Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = newNode->prev =
NULL; return newNode; } void insertAtHead(struct Node **head, int data) { struct Node
*newNode = createNode(data); if (*head == NULL) { *head = newNode; return; } newNode-
>next = *head; (*head)->prev = newNode; *head = newNode; } void insertAtTail(struct Node
**head, int data) { struct Node *newNode = createNode(data); if (*head == NULL) { *head
= newNode; return; } struct Node *temp = *head; while (temp->next != NULL) { temp =
temp->next; } temp->next = newNode; newNode->prev = temp; } void printList(struct Node
*node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; }
printf("NULL\n"); } int main() { struct Node *head = NULL; insertAtHead(&head, 10);
insertAtHead(&head, 20); insertAtTail(&head, 30); insertAtTail(&head, 40);
printf("Doubly Linked List:\n"); printList(head); return 0; }
c Copy code
c Copy code
#include <stdio.h> #include <stdlib.h> #define MAX_SHELVES 1000 #define MAX_BOOKS 1000
int books[MAX_SHELVES][MAX_BOOKS]; int bookCount[MAX_SHELVES] = {0}; void insertBook(int
shelf, int pages) { books[shelf][bookCount[shelf]++] = pages; } void printBookPages(int
shelf, int book) { if (book < bookCount[shelf]) { printf("%d\n", books[shelf][book]); }
else { printf("Invalid book number\n"); } } void printBookCount(int shelf) {
printf("%d\n", bookCount[shelf]); } int main() { int queries, type, x, y; printf("Enter
number of queries: "); scanf("%d", &queries); while (queries--) { printf("Enter query
type: "); scanf("%d", &type); if (type == 1) { printf("Enter shelf number and pages: ");
scanf("%d %d", &x, &y); insertBook(x, y); } else if (type == 2) { printf("Enter shelf
number and book number: "); scanf("%d %d", &x, &y); printBookPages(x, y); } else if
(type == 3) { printf("Enter shelf number: "); scanf("%d", &x); printBookCount(x); } else
{ printf("Invalid query type\n"); } } return 0; }