🔒 Closed C language help

Status
Not open for further replies.
Here are the program’s specifications:

  1. Show a menu. It will not proceed unless the input 1-5 is given.
  2. Add
  3. Subtract
  4. Multiply
  5. Divide
  6. Exit
  1. After selecting an option(except 5), ask for inputs
Enter 1st number:
Enter 2nd number:
  1. It will show the results based on the option you selected. The values are computed using YOUR OWN CREATED FUNCTIONS. The computed value is stored in a simple array. After showing the result, it will ask if you want to compute again. If you answer Y, it will display the 5 options again. If you answer N, it will show all the contents of the array(containing all the computed values), then the program will end.
 
Sure, I can help you with that. Here's a sample program that meets the specifications you provided:

C:
#include <stdio.h>

// Function prototypes
int showMenu();
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);

int main() {
    int choice, num1, num2, result;
    int results[10]; // Array to store computed values
    int index = 0; // Index to keep track of the number of results stored

    do {
        choice = showMenu();

        if (choice != 5) {
            printf("Enter 1st number: ");
            scanf("%d", &num1);
            printf("Enter 2nd number: ");
            scanf("%d", &num2);

            switch (choice) {
                case 1:
                    result = add(num1, num2);
                    printf("Result: %d\n", result);
                    results[index++] = result;
                    break;
                case 2:
                    result = subtract(num1, num2);
                    printf("Result: %d\n", result);
                    results[index++] = result;
                    break;
                case 3:
                    result = multiply(num1, num2);
                    printf("Result: %d\n", result);
                    results[index++] = result;
                    break;
                case 4:
                    if (num2 == 0) {
                        printf("Error: division by zero\n");
                    } else {
                        result = divide(num1, num2);
                        printf("Result: %d\n", result);
                        results[index++] = result;
                    }
                    break;
            }
        }
    } while (choice != 5);

    // Display computed results
    printf("\nComputed results:\n");
    for (int i = 0; i < index; i++) {
        printf("%d\n", results[i]);
    }

    return 0;
}

// Function to display the menu and get user input
int showMenu() {
    int choice;

    printf("\nMenu:\n");
    printf("1. Add\n");
    printf("2. Subtract\n");
    printf("3. Multiply\n");
    printf("4. Divide\n");
    printf("5. Exit\n");

    do {
        printf("Enter choice (1-5): ");
        scanf("%d", &choice);
    } while (choice < 1 || choice > 5);

    return choice;
}

// Function to add two numbers
int add(int a, int b) {
    return a + b;
}

// Function to subtract two numbers
int subtract(int a, int b) {
    return a - b;
}

// Function to multiply two numbers
int multiply(int a, int b) {
    return a * b;
}

// Function to divide two numbers
int divide(int a, int b) {
    return a / b;
}

You can compile and run this program on a C compiler of your choice. This program uses functions to perform the arithmetic operations, and stores the computed results in an array to display at the end. Let me know if you have any questions!
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 362
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
534
Guests online
1,257
Total visitors
1,791

Forum statistics

Threads
2,277,853
Posts
28,979,277
Members
1,229,112
Latest member
rosearugay
Back
Top