0% found this document useful (0 votes)
11 views3 pages

Algorithm

1) The document describes an algorithm to calculate the distance between two points. It defines a function that takes in the x and y coordinates of two points and returns the distance. It then calls this function in the main method. 2) The code takes in a character from the user, prints the character and its ASCII value. 3) The code presents a menu to calculate properties of geometric shapes. It allows the user to select a shape and calculation and inputs values to output the calculated area, perimeter or both. It loops back to the main menu until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Algorithm

1) The document describes an algorithm to calculate the distance between two points. It defines a function that takes in the x and y coordinates of two points and returns the distance. It then calls this function in the main method. 2) The code takes in a character from the user, prints the character and its ASCII value. 3) The code presents a menu to calculate properties of geometric shapes. It allows the user to select a shape and calculation and inputs values to output the calculated area, perimeter or both. It loops back to the main menu until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Algorithm:

Start
Step 1-> declare function to calculate distance between two point
void three_dis(float a, float x, float b, float y)
set float dis = sqrt(pow(b - a / 2) + pow(y - z / 2) * 1.0)
print dis
step 2-> In main()
Set float a = 4
Set float x = 9
Set float b = 5
Set float y = 10
Call two_dis(a, x, b, y)
Stop
Flowchart:

2) Source Code:
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf(" %c", &c);
printf("Character: %c\n", c);
printf("ASCII Value: %d\n", c);
return 0;
}
Input:
Enter a character: A
Output:
Character: A
ASCII Value: 65
3) Source Code:
#include <stdio.h>

int main() {
int choice, subChoice;
double side, length, width, base, height, area, perimeter;

do {
printf("*MAIN MENU*\n");
printf("[1] SQUARE\n");
printf("[2] RECTANGLE\n");
printf("[3] TRIANGLE\n");
printf("[4] EXIT\n");
printf("SELECT A CHOICE: ");
scanf("%d", &choice);

switch (choice) {
case 1: // Square
printf("[A] COMPUTE FOR AREA\n");
printf("[B] COMPUTE FOR PERIMETER\n");
printf("[C] COMPUTE FOR AREA AND PERIMETER\n");
printf("SELECT A CHOICE: ");
scanf(" %c", &subChoice);

printf("ENTER SIDE OF SQUARE: ");


scanf("%lf", &side);

if (subChoice == 'A' || subChoice == 'C') {


area = side * side;
printf("THE AREA OF SQUARE: %.2lf\n", area);
}
if (subChoice == 'B' || subChoice == 'C') {
perimeter = 4 * side;
printf("THE PERIMETER OF SQUARE: %.2lf\n", perimeter);
}
break;

case 2: // Rectangle
printf("[A] COMPUTE FOR AREA\n");
printf("[B] COMPUTE FOR PERIMETER\n");
printf("[C] COMPUTE FOR AREA AND PERIMETER\n");
printf("SELECT A CHOICE: ");
scanf(" %c", &subChoice);

printf("ENTER LENGTH OF RECTANGLE: ");


scanf("%lf", &length);
printf("ENTER WIDTH OF RECTANGLE: ");
scanf("%lf", &width);

if (subChoice == 'A' || subChoice == 'C') {


area = length * width;
printf("THE AREA OF RECTANGLE: %.2lf\n", area);
}
if (subChoice == 'B' || subChoice == 'C') {
perimeter = 2 * (length + width);
printf("THE PERIMETER OF RECTANGLE: %.2lf\n", perimeter);
}
break;

case 3: // Triangle
printf("[A] COMPUTE FOR AREA\n");
printf("[B] COMPUTE FOR PERIMETER\n");
printf("[C] COMPUTE FOR AREA AND PERIMETER\n");
printf("SELECT A CHOICE: ");
scanf(" %c", &subChoice);

printf("ENTER BASE OF TRIANGLE: ");


scanf("%lf", &base);
printf("ENTER HEIGHT OF TRIANGLE: ");
scanf("%lf", &height);

if (subChoice == 'A' || subChoice == 'C') {


area = 0.5 * base * height;
printf("THE AREA OF TRIANGLE: %.2lf\n", area);
}
if (subChoice == 'B' || subChoice == 'C') {
// Perimeter calculation not applicable for triangles
printf("Perimeter calculation is not applicable to triangles.\
n");
}
break;

case 4: // Exit
printf("Goodbye!\n");
break;

default:
printf("Invalid choice. Please try again.\n");
}

if (choice != 4) {
int goBackChoice;
printf("DO YOU WANT TO GO BACK TO MAIN MENU?\n");
printf("[1] YES\n");
printf("[2] NO\n");
printf("SELECT A CHOICE: ");
scanf("%d", &goBackChoice);

if (goBackChoice != 1)
choice = 4; // Exit the program if the user chooses not to go back
to the main menu
}
} while (choice != 4);
return 0;}

You might also like