0% found this document useful (0 votes)
9 views5 pages

First Program

A simple c program that is a course ending program

Uploaded by

202270006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

First Program

A simple c program that is a course ending program

Uploaded by

202270006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

// color

#define RED "\x1b[31m"

#define GREEN "\x1b[32m"

#define YELLOW "\x1b[33m"

#define BLUE "\x1b[34m"

#define MAGENTA "\x1b[35m"

#define CYAN "\x1b[36m"

#define RESET "\x1b[0m"

#define BG_CYAN "\x1b[46m"

int main() {

int choice, num, age, year, currentYear = 2025;

float a, b, c, d, result;

char operation;

// Introduction of me

printf(BG_CYAN BLUE "\nName: Amjad Ma'asar\n" RESET);

printf(BG_CYAN BLUE "Faculty: Engineering\n" RESET);

printf(BG_CYAN BLUE "Department: Mechatronics\n" RESET);

printf(BG_CYAN BLUE "Academic No: 202270006\n\n" RESET);

while (1) {

printf(YELLOW "\nMenu:\n" RESET);

printf(CYAN "1. Check if Even or Odd\n" RESET);


printf(GREEN "2. Simple Calculator\n" RESET);

printf(MAGENTA "3. Calculate Function (A, B, C, D)\n" RESET);

printf(RED "4. Calculate Age\n" RESET);

printf(BLUE "5. Do Loop Demo\n" RESET);

printf(YELLOW "6. Exit\n" RESET);

printf(YELLOW "Enter your choice: " RESET);

if (scanf("%d", &choice) != 1) {

printf(RED "Invalid input. Please enter a number.\n" RESET);

while (getchar() != '\n');

continue;

switch (choice) {

case 1:

printf(GREEN "Enter a number: " RESET);

if (scanf("%d", &num) != 1) {

printf(RED "Invalid input.\n" RESET);

while (getchar() != '\n');

break;

if (num % 2 == 0) {

printf(BLUE "%d is even.\n" RESET, num);

} else {

printf(MAGENTA "%d is odd.\n" RESET, num);

break;

case 2:
printf(YELLOW "Enter expression (e.g., 2 + 3): " RESET);

if (scanf(" %f %c %f", &a, &operation, &b) != 3) {

printf(RED "Invalid input.\n" RESET);

while (getchar() != '\n');

break;//?

switch (operation) {

case '+': result = a + b; break;

case '-': result = a - b; break;

case '*': result = a * b; break;

case '/':

if (b == 0) {

printf(RED "Division by zero!\n" RESET);

break;

result = a / b;

break;

default: printf(RED "Invalid operator.\n" RESET); break;

printf(CYAN "Result: %f\n" RESET, result);

break;

case 3:

printf(MAGENTA "Enter values for A, B, C, and D: " RESET);

if (scanf("%f %f %f %f", &a, &b, &c, &d) != 4) {

printf(RED "Invalid input.\n" RESET);

while (getchar() != '\n');

break;

}
result = (a + b) * (c - d);

printf(BLUE "Result of (A + B) * (C - D): %f\n" RESET, result);

break;

case 4:

printf(RED "Enter your birth year: " RESET);

if (scanf("%d", &year) != 1) {

printf(RED "Invalid input.\n" RESET);

while (getchar() != '\n');

break;

} //strugeled here alot

age = currentYear - year;

if (age >= 0) {

printf(GREEN "You are approximately %d years old.\n" RESET, age);

} else {

printf(RED "Invalid birth year. Please enter a valid input\n" RESET); // Fixed to here

break;

case 5: { // the loop explain

printf(BLUE "Do Loop :\n" RESET);

int counter = 0;

do {

printf(YELLOW "Counter: %d\n" RESET, counter);

counter++;

if (counter == 3) {

printf(RED "Continuing to the next iteration.\n" RESET);

continue;

}
if (counter == 5) {

printf(MAGENTA "Breaking out of the loop.\n" RESET);

break;

} while (counter < 7);

break;

} // was thinking of making the user give the value of the loop then i thuoght what if the user kept
on pressing 999999 so no .

case 6:

printf(BLUE "Exiting program.\n" RESET);

return 0;

default:

printf(RED "Invalid choice. Please try again.\n" RESET);

//my info is at the begining of the code .the main struggles would revolve around understanding the
subtle detials of C language, especially regarding scanf, switch statements, input handling, and loop
control. It would take practice and experimentation and consulting resources like textbooks, online
tutorials, and asking questions to overcomw these challenges

return 0;

You might also like