FYBCS C Exercise_3

Set A: Apply all the three program development steps for the following examples. 

 1. Accept a single digit from the user and display it in words. For example, if digit entered is 9,display Nine. 

 2. Write a program, which accepts two integers and an operator as a character (+ - * /),performs the corresponding operation and displays the result.

 3. Accept two numbers in variables x and y from the user and perform the following operations 

 Set B: Apply all the three program development steps for the following examples. 

 1. Accept radius from the user and write a program having menu with the following options and corresponding actions

2. Write a program having a menu with the following options and corresponding actions


Set C: Write a program to solve the following problems 

1. Accept the three positive integers for date from the user (day, month and year) and checkwhether the date is valid or invalid. Run your program for the following dates and fill the table.(Hint: For valid date 1<=month<=12,1<= day <=no-of-days where no-of-days is 30 in case ofmonths 4, 6,9 and 11. 31 in case of months 1,3,5,7,8,10 and 12. In case of month 2 no-of-days is28 or 29 depending on year is leap or not) 

 2. Write a program having menu that has three options - add, subtract or multiply two fractions.The two fractions and the options are taken as input and the result is displayed as output. Eachfraction is read as two integers, numerator and denominator.  

Post a Comment

6 Comments

  1. #include
    #include
    void scan(int req[],int n,int head,int size,int dir)
    {
    int seek=0,i,j,temp,bound=size,start;
    for(i=0;ireq[j])
    {
    temp=req[i];
    req[i]=req[j];
    req[j]=temp;
    }
    }
    }
    for(start=0;start=head)
    {
    break;
    }
    printf("seek sequence:%d",head);
    if(dir==1)
    {
    for(i=start;i%d",head);
    }
    seek+= abs(bound - head);
    head = bound;
    printf(" ->%d",head);
    for(i=start-1;i>=0;i--)
    {
    seek+=abs(req[i]-head);
    head=req[i];
    printf("->%d",head);
    }
    }
    else
    {
    for(i=start-1;i>=0;i--)
    {
    seek+=abs(req[i]-head);
    head=req[i];
    printf("->%d",head);
    }
    seek+= abs( head -0);
    head = 0;
    printf(" ->%d",head);
    for(i=start;i%d",head);
    }
    }
    printf("\n Total seek time:%d\n",seek);
    }
    int main()
    {
    int n,head,size,dir,i;
    printf("Enter number of requets:");
    scanf("%d",&n);
    int req[n];
    printf("Enter request queue:");
    for(i=0;i<n;i++)
    scanf("%d",&req[i]);
    printf("Enter disk size:");
    scanf("%d",&size);
    printf("Enter initial head position:");
    scanf("%d",&head);
    printf("Enter direction (1 for right,0 for left):");
    scanf("%d",&dir);
    scan(req,n,head,size,dir);
    return 0;
    }

    ReplyDelete
  2. #include
    using namespace std;

    void traverseArray(int arr[], int size) {
    cout << "Array elements: ";
    for (int i = 0; i < size; i++)
    cout << arr[i] << " ";
    cout << endl;
    }

    void insertElement(int arr[], int &size, int capacity, int element, int position) {
    if (size == capacity || position < 0 || position > size) {
    cout << "Insertion failed!" << endl;
    return;
    }
    for (int i = size; i > position; i--)
    arr[i] = arr[i - 1];
    arr[position] = element;
    size++;
    cout << "Element inserted successfully!" << endl;
    }

    void deleteElement(int arr[], int &size, int position) {
    if (position < 0 || position >= size) {
    cout << "Invalid position!" << endl;
    return;
    }
    for (int i = position; i < size - 1; i++)
    arr[i] = arr[i + 1];
    size--;
    cout << "Element deleted successfully!" << endl;
    }

    void searchElement(int arr[], int size, int element) {
    for (int i = 0; i < size; i++) {
    if (arr[i] == element) {
    cout << "Element " << element << " found at position " << i << "." << endl;
    return;
    }
    }
    cout << "Element not found." << endl;
    }

    int main() {
    const int capacity = 100;
    int arr[capacity], size = 0, choice, element, position;

    do {
    cout << "\nMenu:\n1.Traverse 2.Insert 3.Delete 4.Search 5.Exit\nEnter choice: ";
    cin >> choice;

    switch (choice) {
    case 1: traverseArray(arr, size); break;
    case 2:
    cout << "Element to insert: "; cin >> element;
    cout << "Position (0-based): "; cin >> position;
    insertElement(arr, size, capacity, element, position);
    break;
    case 3:
    cout << "Position to delete: "; cin >> position;
    deleteElement(arr, size, position);
    break;
    case 4:
    cout << "Element to search: "; cin >> element;
    searchElement(arr, size, element);
    break;
    case 5: cout << "Exiting...\n"; break;
    default: cout << "Invalid choice!\n";
    }
    } while (choice != 5);

    return 0;
    }

    ReplyDelete
  3. #include
    using namespace std;

    #define MAX 100

    class Stack {
    int arr[MAX];
    int top;

    public:
    Stack() { top = -1; }

    bool isEmpty() { return top == -1; }
    bool isFull() { return top == MAX - 1; }

    void push(int x) {
    if (isFull()) {
    cout << "Stack Overflow! Cannot push " << x << endl;
    return;
    }
    arr[++top] = x;
    cout << x << " pushed to stack." << endl;
    }

    void pop() {
    if (isEmpty()) {
    cout << "Stack Underflow! Nothing to pop." << endl;
    return;
    }
    cout << arr[top--] << " popped from stack." << endl;
    }

    void peek() {
    if (isEmpty()) {
    cout << "Stack is empty." << endl;
    return;
    }
    cout << "Top element is: " << arr[top] << endl;
    }

    void display() {
    if (isEmpty()) {
    cout << "Stack is empty." << endl;
    return;
    }
    cout << "Stack elements from top to bottom: ";
    for (int i = top; i >= 0; i--)
    cout << arr[i] << " ";
    cout << endl;
    }
    };

    int main() {
    Stack s;
    int choice, value;

    do {
    cout << "\nStack Operations Menu:\n";
    cout << "1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\nEnter choice: ";
    cin >> choice;

    switch (choice) {
    case 1:
    cout << "Enter value to push: ";
    cin >> value;
    s.push(value);
    break;
    case 2:
    s.pop();
    break;
    case 3:
    s.peek();
    break;
    case 4:
    s.display();
    break;
    case 5:
    cout << "Exiting program." << endl;
    break;
    default:
    cout << "Invalid choice! Try again." << endl;
    }
    } while (choice != 5);

    return 0;
    }

    ReplyDelete
  4. #include
    #include
    #include
    using namespace std;

    int precedence(char op) {
    if (op == '+' || op == '-') return 1;
    if (op == '*' || op == '/') return 2;
    if (op == '^') return 3;
    return 0;
    }

    string infixToPostfix(const string &infix) {
    stack s;
    string postfix;
    for (char c : infix) {
    if (isdigit(c)) {
    postfix += c;
    } else if (c == '(') {
    s.push(c);
    } else if (c == ')') {
    while (!s.empty() && s.top() != '(') {
    postfix += s.top();
    s.pop();
    }
    s.pop();
    } else {
    while (!s.empty() && precedence(s.top()) >= precedence(c)) {
    postfix += s.top();
    s.pop();
    }
    s.push(c);
    }
    }
    while (!s.empty()) {
    postfix += s.top();
    s.pop();
    }
    return postfix;
    }

    int evaluatePostfix(const string &postfix) {
    stack s;
    for (char c : postfix) {
    if (isdigit(c)) {
    s.push(c - '0');
    } else {
    int b = s.top(); s.pop();
    int a = s.top(); s.pop();
    switch (c) {
    case '+': s.push(a + b); break;
    case '-': s.push(a - b); break;
    case '*': s.push(a * b); break;
    case '/': s.push(a / b); break;
    case '^': {
    int res = 1;
    for (int i = 0; i < b; i++) res *= a;
    s.push(res);
    break;
    }
    }
    }
    }
    return s.top();
    }

    int main() {
    string infix;
    cout << "Enter infix expression (single digit operands): ";
    cin >> infix;

    string postfix = infixToPostfix(infix);
    cout << "Postfix: " << postfix << "\n";
    cout << "Result: " << evaluatePostfix(postfix) << "\n";

    return 0;
    }

    ReplyDelete
  5. #include
    using namespace std;

    class Queue {
    int front, rear, capacity;
    int* arr;

    public:
    Queue(int size) {
    capacity = size;
    arr = new int[capacity];
    front = -1;
    rear = -1;
    }

    ~Queue() { delete[] arr; }

    bool isFull() { return rear == capacity - 1; }
    bool isEmpty() { return front == -1 || front > rear; }

    void enqueue(int val) {
    if (isFull()) {
    cout << "Queue overflow. Cannot insert " << val << endl;
    return;
    }
    if (isEmpty()) {
    front = 0;
    }
    arr[++rear] = val;
    cout << val << " enqueued to queue." << endl;
    }

    void dequeue() {
    if (isEmpty()) {
    cout << "Queue underflow. No element to dequeue." << endl;
    return;
    }
    cout << arr[front++] << " dequeued from queue." << endl;
    if (front > rear) {
    front = rear = -1;
    }
    }

    void display() {
    if (isEmpty()) {
    cout << "Queue is empty." << endl;
    return;
    }
    cout << "Queue elements: ";
    for (int i = front; i <= rear; i++) {
    cout << arr[i] << " ";
    }
    cout << endl;
    }
    };

    int main() {
    int size;
    cout << "Enter queue capacity: ";
    cin >> size;

    Queue q(size);
    int choice, val;

    do {
    cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nEnter choice: ";
    cin >> choice;

    switch (choice) {
    case 1:
    cout << "Enter value to enqueue: ";
    cin >> val;
    q.enqueue(val);
    break;
    case 2:
    q.dequeue();
    break;
    case 3:
    q.display();
    break;
    case 4:
    cout << "Exiting..." << endl;
    break;
    default:
    cout << "Invalid choice." << endl;
    }
    } while (choice != 4);

    return 0;
    }

    ReplyDelete
  6. #include
    #include
    using namespace std;

    class CircularQueue {
    int front, rear, capacity;
    int* arr;

    public:
    CircularQueue(int size) : front(-1), rear(-1), capacity(size) {
    arr = new int[capacity];
    }
    ~CircularQueue() { delete[] arr; }

    bool isFull() { return front == (rear + 1) % capacity; }
    bool isEmpty() { return front == -1; }

    void enqueue(int val) {
    if (isFull()) {
    cout << "Overflow: Cannot insert " << val << endl;
    return;
    }
    if (isEmpty()) front = rear = 0;
    else rear = (rear + 1) % capacity;
    arr[rear] = val;
    cout << val << " enqueued." << endl;
    }

    void dequeue() {
    if (isEmpty()) {
    cout << "Underflow: No element to dequeue." << endl;
    return;
    }
    cout << arr[front] << " dequeued." << endl;
    if (front == rear) front = rear = -1;
    else front = (front + 1) % capacity;
    }

    void display() {
    if (isEmpty()) {
    cout << "Queue is empty." << endl;
    return;
    }
    cout << "Queue elements: ";
    int i = front;
    while (true) {
    cout << arr[i] << " ";
    if (i == rear) break;
    i = (i + 1) % capacity;
    }
    cout << endl;
    }
    };

    int main() {
    int size, choice, val;
    cout << "Enter queue capacity: ";
    cin >> size;

    CircularQueue cq(size);

    do {
    cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nChoice: ";
    cin >> choice;

    switch (choice) {
    case 1:
    cout << "Value: ";
    cin >> val;
    cq.enqueue(val);
    break;
    case 2:
    cq.dequeue();
    break;
    case 3:
    cq.display();
    break;
    case 4:
    cout << "Exit.\n";
    break;
    default:
    cout << "Invalid choice.\n";
    }
    } while (choice != 4);

    return 0;
    }

    ReplyDelete