Untitled

 avatar
unknown
plain_text
17 days ago
3.6 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

// Case Study 1: FIBO function
// Simple iterative Fibonacci to get FIBO(n) where FIBO(0)=0, FIBO(1)=1
int FIBO(int n) {
    int a = 0, b = 1, c, i;
    if(n == 0) return 0;
    if(n == 1) return 1;
    for(i = 2; i <= n; i++){
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}

// Prints Fibonacci table for 12 months of rabbits
void runFibonacci() {
    int i;
    int rabbits[12];
    
    printf("\n===== FIBONACCI RABBIT TABLE =====\n");
    printf(" Month | Number of Rabbits\n");
    printf("-------+-------------------\n");
    
    // Fill array with FIBO(1) to FIBO(12) (which yields 1..144)
    for(i = 1; i <= 12; i++){
        rabbits[i - 1] = FIBO(i);
        printf("   %2d   | %3d\n", i, rabbits[i - 1]);
    }
    
    // The final total at 12 months is FIBO(12)
    printf("\nTotal rabbits at the end of 12 months: %d\n\n", rabbits[11]);
}

// Case Study 2: MILITARY_TIME function
// Converts an integer (0..2400) in 24-hour notation to 12-hour notation
void MILITARY_TIME(int timeVal) {
    int hour = timeVal / 100;   // e.g. 1340 / 100 = 13
    int minute = timeVal % 100; // e.g. 1340 % 100 = 40
    
    // Validate range
    if(timeVal < 0 || timeVal > 2400 || minute > 59) {
        printf("INVALID TIME\n");
        return;
    }
    
    // If hour == 24, minute must be 0 to be valid "midnight"
    if(hour == 24) {
        if(minute == 0) {
            printf("Time is 12:00 AM\n"); // 2400 means midnight
        } else {
            printf("INVALID TIME\n");
        }
        return;
    }
    
    // Use const char* to store string literals
    const char *ampm;
    int displayHour = hour;
    
    if(hour == 0) {
        displayHour = 12;
        ampm = "AM";
    } else if(hour == 12) {
        displayHour = 12;
        ampm = "PM";
    } else if(hour > 12) {
        displayHour = hour - 12;
        ampm = "PM";
    } else {
        ampm = "AM";
    }
    
    // Print result (use 2-digit minute with %02d)
    printf("Time is %d:%02d %s\n", displayHour, minute, ampm);
}

void runMilitaryTime() {
    int inputVal;
    printf("\nEnter time in 24-hour military format (0 to 2400): ");
    scanf("%d", &inputVal);
    
    // Call the function
    MILITARY_TIME(inputVal);
    printf("\n");
}

int main() {
    char choice;
    int again = 1;
    
    do {
        printf("========================================\n");
        printf("                MAIN MENU\n");
        printf("========================================\n");
        printf("  A. FIBONACCI SERIES (Rabbit Problem)\n");
        printf("  B. MILITARY TIME\n");
        printf("  C. EXIT\n");
        printf("========================================\n");
        printf("Choose letter to execute: ");
        
        // Read one character choice
        scanf(" %c", &choice);
        choice = toupper(choice);
        
        switch(choice) {
            case 'A':
                runFibonacci();
                break;
            case 'B':
                runMilitaryTime();
                break;
            case 'C':
                printf("\nExiting program...\n");
                again = 0;
                break;
            default:
                printf("\nInvalid choice. Please try again.\n\n");
        }
        
        if(choice != 'C') {
            char c;
            printf("Would you like to try again? (Y/N): ");
            scanf(" %c", &c);
            if(toupper(c) != 'Y') {
                again = 0;
            }
        }
    } while(again);
    
    return 0;
}
Editor is loading...
Leave a Comment