Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

/*

NAME: ETURALDE, LOUEL M.

SECTION: BSCpE 11002

INSTRUCTOR: MS. GALANG, ROSALIE.

*/

#include <stdio.h>

#include <ctype.h>

#include <conio.h>

#include <math.h>

int getUserInput(char message[], char datatype[5]); // get the user input and check if it's valid.

int yesOrNoOption(char message[]);

void removeBuffer();// Clear the buffer so scanf can ask the user again.

void display(); // display the main menu

int menu(); // Display and GetUser input.

int mainOption(int input);// The main Option

void introductionC();// The lesson in C.

void subOptionStructure(); // The submenu for structures program.

void sequentialProgram(); // Sequential Program

void calculateSumOfTwoNumbers(); // Sequential example 1

void areaOfACircle(); // Sequential example 2

void displayEvenNumbersLimit(); // Sequential example 3

void decisionProgram(); // Decision Program

void gradeClassification();// Decision Program Example 1

void calculator(); // Decision Program Example 2

void voteEligibility(); // Decision Program Example 3


void loopProgram(); //LOOP PROGRAM

void forLoop(); // for loop example 1

void whileLoop(); // While loop Example 2

void doWhileLoop(); // do while Loop Example 3

void arrayProgram(); // Array program

void sumOfFivenumber(); // Array Example 1

void findTheLargestNumberInArray();// Array Example 2

void reverseArray(); // Array Example 3

void rescursionProgram(); // Recursion

void recursiveTree(int height, int space); // Recursion Example 1

void recursiveTreeRunner(); // This func will run Recursion example 1

void drawCircle(int radius, int centerX, int centerY); // Recursion Example 2

void drawCircleRunner(); // This func will run Recursion example 2

void drawSpiral(int length); // Recursion Example 3

void drawSpiralRunner(); // This func will run Recursion example 3

void displayAuthorLife(); // About the author

// MAIN

int main(){

int menuInput;

while(menuInput != 4){// This will loop forever until user input number 4.

clrscr();

menuInput = menu();

mainOption(menuInput);

// MENU SETTINGS
int mainOption(int input){

switch(input){

case 1: introductionC();break;

case 2: subOptionStructure();break;

case 3: displayAuthorLife();break;

case 4: printf("Quit");break; // If input is equal to 4 then the entire program will be


terminated.

default: printf("Invalid choices");

int menu(){

/*

COMBINING THE display and getUserInput FUNCTION.

return: int user_input;

*/

display();

return getUserInput("Choose from the menu[1-4]: ", "%d");

void display(){

/*

THIS FUNCTION WILL DISPLAY THE MENU.

I use void in the function because Im not gonna return anything. :)

*/

printf("\n\n******************** MENU ********************\n\n");

printf("\n"
"1. C LANGUAGE INTRODUCTION\n"

"2. PROGRAMMING STRUCTURE\n"

"\tA. Sequential Programs\n"

"\tB. Decision Programs\n"

"\tC. Loop Programs\n"

"\tD. Array\n"

"\tE. Recursor\n"

"3. About the author\n"

"4. Quit\n\n"

);

// ******************** C LANGUAGE INTRODUCTION ********************

void introductionC(){

/*

THESE ARE THE LESSON FOR OPTION 1, INTRODUCTION IN C.

*/

char lessons[][1000] = { // LIST OF LESSON

"What is C?\n C is a general-purpose, procedural programming language developed by


Dennis\n Ritchie at Bell Labs in the early 1970s. It was designed to be a simple and\n efficient language
that could be used for system programming, such as developing\n operating systems. Over time,C
became widely adopted and influenced the development\n of many other programming languages.",

"Origins at Bell Labs:\n\t* C was created by Dennis Ritchie at Bell Labs (AT&T) in the
early 1970.\n\t*It evolved from an earlier programming language called B, which was developed by Ken
Thompson.B, in turn, had its \n\troots in BCPL (Basic Combined Programming Language).",

"Development and Standardization:\n\t*Ritchie and Brian Kernighan further developed


C, adding features such as data types and structures.\n\t*The language was used in the development of
the Unix operating system, which was also created at Bell Labs.\n\t*Kernighan and Ritchie published.\n\
t*The C Programming Language (often referred to as K&R C) in 1978. This book became the authoritative
reference for\n\t C."

};

int length = sizeof(lessons) / sizeof(lessons[0]); // Calculate the total items in an array.

int user_choice;

for(int i = 0; i<length;i++){// Looping all the lesson in an array lessons.

clrscr();

printf("\n\n******** LESSON %d ********", i+1);

printf("\n\n%s\n", lessons[i]);// display lesson.

// Ask the user if he/she want to continue or not.

user_choice = yesOrNoOption("\nMore lesson(Y/N)?: ");

if(i+1>=length){ // Check if there's any more lesson.'

printf("Oops no more lesson :(");

if(user_choice == 1){

break;

removeBuffer(); // Clear the input buffer. or else recursion.

// ******************** ABOUT THE AUTHOR ********************

void displayAuthorLife(){

char lessons[][1000] = { // LIST OF LESSON

"Hello my name is Louel Eturalde the author of this program.\nI was born on November
27, 2003 at Bagong silang Caloocan City.",
"My hobbies are playing mobile games, eating, and also I love music.\nI have 7 siblings,
My favourite color is marine color and my favourite sports is basketball.\nI like sweets food. ",

"Im currently studying at Bestlink college of the philippines, and they have the best
teachers I love they all."

};

int length = sizeof(lessons) / sizeof(lessons[0]); // Calculate the total items in an array.

int user_choice;

for(int i = 0; i<length;i++){// Looping all the lesson in an array lessons.

clrscr();

printf("\n\n******** LESSON %d ********", i+1);

printf("\n\n%s\n", lessons[i]);// display lesson.

// Ask the user if he/she want to continue or not.

user_choice = yesOrNoOption("\nMore lesson(Y/N)?: ");

if(i+1>=length){ // Check if there's any more lesson.'

printf("Oops no more lesson :(");

if(user_choice == 1){

break;

removeBuffer(); // Clear the input buffer. or else recursion.

// ******************* STRUCTURE PROGRAM *******************


void subOptionStructure(){

char subUserInput;

int alive = 1;

while(alive != 0){

clrscr();

printf("\n=============> PROGRAMMING STRUCTURE <=============\n"

"\tA. Sequential Programs\n"

"\tB. Decision Programs\n"

"\tC. Loop Programs\n"

"\tD. Array\n"

"\tE. Recursion\n"

"\tF. *** QUIT ***\n");

subUserInput = getUserInput("Choose a program[A-E]: ", " %c");

switch(toupper(subUserInput)){

case 'A': sequentialProgram();break;

case 'B': decisionProgram();break;

case 'C': loopProgram();break;

case 'D': arrayProgram();break;

case 'E': rescursionProgram();break;

case 'F': alive = 0;break;

default: printf("\nInvalid input please enter [A-E]\n");

// >>>>>>>>>>>>>>>>>> RECURSION STRUCTURE <<<<<<<<<<<<<<<<<<<<

void rescursionProgram(){

printf("\n\n<>-------------------------<><><><> RECURSION PROGRAM <><><><>-------------------------


<>\n");
printf(""

"1. Recursive Trees\n"

"2. Draw Circle\n"

"3. Draw spiral\n");

int menuOption = getUserInput("Choose sequential program example from the menu[1-4]: ",
"%d");

switch(menuOption){

case 1: recursiveTreeRunner();break;

case 2: drawCircleRunner();break;

case 3: drawSpiralRunner();break;

// THREE EXAMPLE OF RECURSION PROGRAM

// =======> DRAW SPIRAL <=======

void drawSpiralRunner(){

int initialLength;

int alive = 1;

while(alive != 0){

initialLength = getUserInput("Enter initial Length: ", "%d");

// Print a unique recursive pattern of a spiral

drawSpiral(initialLength);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}
}

void drawSpiral(int length) {

if (length <= 0) {

return;

// Draw the spiral using ASCII art

for (int i = 0; i < length; ++i) {

printf("*");

printf("\n");

// Recursive call for the next iteration of the spiral

drawSpiral(length - 1);

// =======> DRAW CIRCLE <=======

void drawCircleRunner(){

int initialRadius;

int centerX;

int alive = 1;

int centerY;

while(alive != 0){

// Print a unique recursive pattern of concentric circles

initialRadius = getUserInput("Enter initial radius: ", "%d");


centerX = getUserInput("Enter center x: ", "%d");

centerY = getUserInput("Enter center y: ", "%d");

drawCircle(initialRadius, centerX, centerY);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

void drawCircle(int radius, int centerX, int centerY) {

// Base case: stop recursion when radius becomes zero

if (radius <= 0) {

return;

// Draw the circle using ASCII art

for (int i = 0; i < 360; ++i) {

int x = centerX + radius * cos(i);

int y = centerY + radius * sin(i);

// Print the circle point

printf(".");

// Add new line for better visualization

if (i % 36 == 0) {

printf("\n");

}
printf("\n");

// Recursive call for concentric circles

drawCircle(radius - 2, centerX, centerY);

// =======> RECURSIVE TREE <=======

void recursiveTreeRunner(){

int height;

int spaces;

int alive = 1;

while(alive != 0){

height = getUserInput("Enter the height: ", "%d");

spaces = getUserInput("Enter the spaces: ", "%d");

recursiveTree(height, spaces);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

void recursiveTree(int height, int spaces) {

if (height <= 0) {

return;
}

// Print spaces

for (int i = 0; i < spaces; ++i) {

printf(" ");

// Print asterisks

for (int i = 0; i < height * 2 - 1; ++i) {

printf("*");

printf("\n");

// Recursive calls for upper and lower halves

recursiveTree(height - 1, spaces + 1);

recursiveTree(height - 1, spaces + 1);

// >>>>>>>>>>>>>>>>>> ARRAY STRUCTURE <<<<<<<<<<<<<<<<<<<<

void arrayProgram(){

printf("\n\n<>-------------------------<><><><> ARRAY PROGRAM <><><><>-------------------------<>\


n");

printf(""

"1. Sum of 5 numbers\n"

"2. Largest Number In an array\n"

"3. Reverse Array\n");

int menuOption = getUserInput("Choose sequential program example from the menu[1-4]: ",
"%d");
switch(menuOption){

case 1: sumOfFivenumber();break;

case 2: findTheLargestNumberInArray();break;

case 3: reverseArray();break;

// THREE EXAMPLE OF ARRAY PROGRAM

// =======> SUM OF THE FIVE LETTER <=======

void sumOfFivenumber(){

int alive = 1;

int limit = 5;

int numbers[10];

int sum = 0;

while(alive != 0){

for (int i = 0; i < limit; i++) {

numbers[i] = getUserInput("Enter a number: ", "%d");

for (int n = 0; n < limit; n++) {

sum += numbers[n];

printf("Sum of array elements: %d\n", sum);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;
}

// =======> FIND THE LARGE NUMBER OF AN ARRAY <=======

void findTheLargestNumberInArray(){

int alive = 1;

int limit = 5;

int numbers[5];

int max;

while(alive != 0){

for (int i = 0; i < limit; i++) {

numbers[i] = getUserInput("Enter a number: ", "%d");

for (int n = 0; n < limit; n++) {

if (numbers[n] > max) {

max = numbers[n];

printf("Largest element in the array: %d\n", max);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// =======> REVERSED ARRAY <=======


void reverseArray(){

int alive = 1;

int limit = 5;

int numbers[5];

int temp;

while (alive != 0){

for (int i = 0; i < limit; i++) {

numbers[i] = getUserInput("Enter a number: ", "%d");

int i = 0, j = limit - 1;

while (i < j) {

// Loop body

// Swap elements at positions i and j

temp = numbers[i];

numbers[i] = numbers[j];

numbers[j] = temp;

++i;

--j;

printf("Reversed array: ");

for (int i = 0; i < limit; i++) {

printf("%d ", numbers[i]);

} if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}
// >>>>>>>>>>>>>>>>>> LOOP STRUCTURE <<<<<<<<<<<<<<<<<<<<

void loopProgram(){

printf("\n\n<>-------------------------<><><><> LOOP PROGRAM <><><><>-------------------------<>\n");

printf(""

"1. For Loop\n"

"2. Multiple numbers by 10\n"

"3. Rows and Columns\n");

int menuOption = getUserInput("Choose sequential program example from the menu[1-4]: ",
"%d");

switch(menuOption){

case 1: forLoop();break;

case 2: whileLoop();break;

case 3: doWhileLoop();break;

// THREE EXAMPLE OF LOOP PROGRAM

// =======> FOR LOOP <=======

void forLoop(){

int alive = 1;

while(alive != 0){

int user_input = getUserInput("Enter a number: ", "%d");

for (int i = 1; i <= user_input; i++) {

printf("%d ", i);

}
if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// =======> WHILE LOOP <=======

void whileLoop(){

int alive = 1;

int userInput;

while(alive !=0){

userInput = getUserInput("Enter a number: ", "%d");

for(int i=0;i < userInput; i++){

printf("%d * 10 = %d\n", i, i*10);

}if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// =======> DO WHILE LOOP <=======

void doWhileLoop(){

int alive = 1;

int row;

int col;

char symbol;

while(alive !=0){

row = getUserInput("Enter row: ", "%d");

col = getUserInput("Enter column: ", "%d");

symbol = getUserInput("Enter Symbol: ", " %c");


for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

printf("%c ", symbol);

printf("\n");

}if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// >>>>>>>>>>>>>>>>>> DECISION STRUCTURE <<<<<<<<<<<<<<<<<<<<

void decisionProgram(){

printf("\n\n<>-------------------------<><><><> DECISION PROGRAM <><><><>-------------------------


<>\n");

printf(""

"1. Grade Classification\n"

"2. Calculator\n"

"3. Voting Eligibility. \n");

int menuOption = getUserInput("Choose sequential program example from the menu[1-4]: ",
"%d");

switch(menuOption){

case 1: gradeClassification();break;

case 2: calculator();break;

case 3: voteEligibility();break;
}

// THREE EXAMPLE OF DECISION PROGRAM

// =======> GRADE CLASSIFICATION <=======

void gradeClassification(){

int marks;

int alive = 1;

while(alive != 0){

// Input marks from the user

marks = getUserInput("Enter your marks: ", "%d");

// Decision-making based on marks

if (marks >= 90) {

printf("Grade: A\n");

} else if (marks >= 80) {

printf("Grade: B\n");

} else if (marks >= 70) {

printf("Grade: C\n");

} else if (marks >= 60) {

printf("Grade: D\n");

} else {

printf("Grade: F\n");

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}
// =======> CALCULATOR <=======

void calculator(){

char operators;

int num1, num2;

double result;

int alive = 1;

while(alive != 0){

// Input values and operator from the user

num1 = getUserInput("Enter first number: ", "%d");

num2 = getUserInput("Enter second number: ", "%d");

operators = getUserInput("Enter operator(*, /, +, -): ", " %c");

// Decision-making based on the operator

switch (operators) {

case '+': result = num1 + num2;break;

case '-': result = num1 - num2;break;

case '*': result = num1 * num2;break;

case '/': result = num1 / num2;break;

default: printf("Invalid operator\n");

// Output the result

printf("Result: %.2lf\n", result);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}
}

// =======> VOTE ELIGIBILITY<=======

void voteEligibility(){

int age;

int alive = 1;

while(alive != 0){

// Input age from the user

age = getUserInput("Enter your age: ", "%d");

// Decision-making based on age for voting eligibility

if (age >= 18) {

printf("You are eligible to vote.\n");

} else {

printf("You are not eligible to vote yet.\n");

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// >>>>>>>>>>>>>>>>>> SEQUENTIAL STRUCTURE <<<<<<<<<<<<<<<<<<<<

void sequentialProgram(){

printf("\n\n<>-------------------------<><><><> SEQUENTIAL PROGRAM <><><><>-------------------------


<>\n");

printf(""

"1. Calculate the Sum of Two Numbers.\n"

"2. Find the Area of a Circle.\n"


"3. Display Even Numbers up to a Limit.\n");

int menuOption = getUserInput("Choose sequential program example from the menu[1-4]: ",
"%d");

switch(menuOption){

case 1: calculateSumOfTwoNumbers();break;

case 2: areaOfACircle();break;

case 3: displayEvenNumbersLimit();break;

// THREE EXAMPLE OF SEQUENTIAL

// =======> SUM OF TWO NUMBERS <=======

void calculateSumOfTwoNumbers(){

int num1, num2, sum;

int alive = 1;

do{

// Input: Get numbers from the user

num1 = getUserInput("Enter the first number: ", "%d");

num2 = getUserInput("Enter the second number: ", "%d");

// Process: Calculate the sum

sum = num1 + num2;

// Output: Display the result


printf("The sum is: %d\n", sum);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}while(alive != 0);

// =======> AREA OF A CIRCLE <=======

void areaOfACircle(){

// Sequential Program: Find the Area of a Circle

float radius, area;

int alive = 1;

while(alive != 0){

// Input: Get the radius from the user

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

// Process: Calculate the area

area = 3.14 * radius * radius;

//Display the result

printf("The area of the circle is: %.2f\n", area);

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

}
}

// ==============> Display Even Numbers up to a Limit < ================

void displayEvenNumbersLimit(){

// Declare variables

int limit;

int alive = 1;

// Input: Get the limit from the user

while(alive != 0){

printf("Enter the limit: ");

scanf("%d", &limit);

// Process and Output: Display even numbers

printf("Even numbers up to %d: ", limit);

for (int i = 0; i <= limit; i += 2) {

printf("%d ", i);

printf("\n");

if(yesOrNoOption("Do you want to continue? ") == 1){

alive = 0;

// TOOLS

int getUserInput(char message[], char datatype[5]){

/*
THIS FUNCTION WILL CHECK IF USER ENTER THE CORRECT DATATYPE!!

int scan_check: returns 1(as true) or 0(as false), if user entered the correct datatype it
will return 1 other wise 0;

int alive: This will terminate the code if scan_check returns 0 or false;

int user_input: returns whatever user inputs as long it's a integer.

return: int user_input;

*/

int user_input;

int alive = 1;// 1 represents true otherwise 0 as false.

while(alive){ // if the variable alive is equal to 0, then the loop will be terminated.

printf("%s", message);

int scan_check = scanf(datatype, &user_input);

if(scan_check == 1){

alive = 0;

}else{

printf("Invalid input.\n");// display notice if user enter the wrong datatype.

removeBuffer(); // Clear the input buffer. or else recursion.

}return user_input;

void removeBuffer(){

/*

This function is a concise way to clear the standard input buffer in C.

*/

int c;
while ((c = getchar()) != '\n' && c != EOF);

int yesOrNoOption(char message[]){

/*

THIS WILL ASK USER YES OR NO

*/

char user_choice;

int alive = 1;

while(alive != 0){

printf("\n%s", message);

scanf(" %c", &user_choice);

if(toupper(user_choice) == 'N'){

alive = 0;// Terminate the loop

}else if(toupper(user_choice) == 'Y'){

return 0; // Means user wants to quit

}else{

printf("Invalid input, please enter Y or N lower or upper case.");

}return 1; // 1 means the user choose No.

You might also like