Untitled
unknown
plain_text
9 months ago
4.5 kB
6
Indexable
#include <stdio.h>
#include <stdlib.h>
// Forward declarations
void fibo(void);
void military_time(void);
int main() {
char choice;
int runProgram = 1;
do {
// Clear screen (optional, depends on your system).
// On Windows you can use "cls", on Linux/macOS use "clear"
// system("cls");
// or system("clear");
printf("=====================================\n");
printf(" M A I N M E N U\n");
printf("=====================================\n");
printf(" A. FIBONACCI SERIES\n");
printf(" B. MILITARY TIME\n");
printf(" C. EXIT\n");
printf("=====================================\n");
printf("CHOOSE LETTER TO EXECUTE: ");
scanf(" %c", &choice);
switch(choice) {
case 'A':
case 'a':
fibo();
break;
case 'B':
case 'b':
military_time();
break;
case 'C':
case 'c':
printf("\nExiting the program...\n");
runProgram = 0;
break;
default:
printf("\nInvalid choice. Please choose A, B, or C.\n");
break;
}
// If user did not choose to exit, we can ask if they want to go again
if(runProgram) {
char again;
printf("\nDo you want to go back to the main menu? (Y/N): ");
scanf(" %c", &again);
if(again == 'N' || again == 'n') {
runProgram = 0;
}
}
} while(runProgram);
return 0;
}
// -------------------------------------------------
// FIBONACCI / RABBITS FUNCTION
// -------------------------------------------------
// This function calculates how many rabbits (pairs)
// there are after 12 months, stores them in an array
// and displays them, then prints the final total.
void fibo(void) {
// We’ll store 12 months of data in an array:
int RABBIT[12];
// First two months in classical Fibonacci: 1, 1
RABBIT[0] = 1;
RABBIT[1] = 1;
// Generate Fibonacci for months 3 to 12
for(int i = 2; i < 12; i++) {
RABBIT[i] = RABBIT[i-1] + RABBIT[i-2];
}
printf("\nFIBONACCI SERIES FOR 12 MONTHS (Rabbits per month):\n");
for(int i = 0; i < 12; i++) {
printf("Month %2d: %d\n", i+1, RABBIT[i]);
}
// The total number of rabbits at the 12th month
printf("\nAfter 12 months, total rabbits = %d\n", RABBIT[11]);
// If you wanted to sum them all, you'd do:
// int sum = 0;
// for(int i = 0; i < 12; i++) sum += RABBIT[i];
// printf("Sum of all months = %d\n", sum);
printf("\n");
}
// -------------------------------------------------
// MILITARY TIME FUNCTION
// -------------------------------------------------
// Converts a time given in 24-hour notation (0000 to 2400)
// into 12-hour notation or reports INVALID if out of range.
void military_time(void) {
int input;
int hour, minute;
printf("\nEnter time in military format (0 to 2400): ");
scanf("%d", &input);
// Quick check for valid range
if(input < 0 || input > 2400) {
printf("Output : INVALID\n\n");
return;
}
// Separate hours and minutes
hour = input / 100; // e.g. 1300 -> hour = 13
minute = input % 100; // e.g. 1300 -> minute = 0
// Check if minutes are valid (0–59). If not, invalid.
if(minute < 0 || minute > 59) {
printf("Output : INVALID TIME\n\n");
return;
}
// Special case for 2400 -> treat as 00:00
if(hour == 24 && minute == 0) {
// 24:00 is effectively 12:00 AM
printf("Output : 12:00 AM\n\n");
return;
}
// If hour is 24 with non-zero minutes, it's invalid.
if(hour == 24 && minute != 0) {
printf("Output : INVALID TIME\n\n");
return;
}
// Now convert to 12-hour format
// We’ll display something like "1:00 PM"
if(hour == 0) {
// Midnight hour
printf("Output : 12:%02d AM\n\n", minute);
} else if(hour < 12) {
// From 1:00 AM to 11:59 AM
printf("Output : %d:%02d AM\n\n", hour, minute);
} else if(hour == 12) {
// 12:xx PM
printf("Output : 12:%02d PM\n\n", minute);
} else {
// From 13:00 to 23:59 => subtract 12 for PM
printf("Output : %d:%02d PM\n\n", hour - 12, minute);
}
}
Editor is loading...
Leave a Comment