Skip to content

Instantly share code, notes, and snippets.

@vtvh
Created August 15, 2023 11:12
Show Gist options
  • Save vtvh/c6316fa7b7e19b3260b96f04fdaad8cb to your computer and use it in GitHub Desktop.
Save vtvh/c6316fa7b7e19b3260b96f04fdaad8cb to your computer and use it in GitHub Desktop.
// De Thi 1
// 15.08.2023
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
struct City
{
char areaCode[30];
char name[30];
int population;
};
struct City c[MAX];
int numberOfCitiesInput;
void inputCity(struct City[]);
void outputCity(struct City[]);
void doQuestionR1()
{
// Check whether a number is PRIME or not
puts("Checking the prime:");
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n == 0 || n == 1)
{
puts("This is NOT a prime");
return;
}
if (n == 2)
{
puts("This is a prime");
return;
}
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
puts("This is NOT a prime");
break;
}
else
{
puts("This is a prime");
break;
}
}
puts("-END OF R1-");
return;
}
void doQuestionR2()
{
printf("Enter the number of cities: ");
scanf("%d", &numberOfCitiesInput);
inputCity(c);
outputCity(c);
return;
}
void inputCity(struct City c[])
{
for (int i = 0; i < numberOfCitiesInput; i++)
{
printf("Input for city %d: \n", i + 1);
printf("\t - Code: ");
scanf(" %[^\n]", c[i].areaCode);
printf("\t - Name: ");
scanf(" %[^\n]", c[i].name);
printf("\t - Population: ");
scanf("%d", &c[i].population);
printf("\n");
}
}
void outputCity(struct City c[])
{
int maxIndex = 0;
int maxValue = 0;
puts("------------------------------");
puts("Displaying all cities:");
puts("------------------------------");
for (int i = 0; i < numberOfCitiesInput; i++)
{
printf("City %d:", i + 1);
printf("\t Code: %s \n", c[i].areaCode);
printf("\t Name: %s \n", c[i].name);
printf("\t Population: %d \n\n", c[i].population);
// So sanh Max
if (c[i].population > maxValue)
{
maxIndex = i;
maxValue = c[i].population;
}
}
puts("------------------------------");
puts("City's most populous:");
puts("------------------------------");
printf("\t Code: %s \n", c[maxIndex].areaCode);
printf("\t Name: %s \n", c[maxIndex].name);
printf("\t Population: %d \n\n", c[maxIndex].population);
puts("-END OF R2-");
}
void doQuestionR3()
{
puts("Exit program....");
exit(0);
}
void menu()
{
puts("*********************************");
puts("* Select Action: *");
puts("* \t 1. R1 \t\t\t*");
puts("* \t 2. R2 \t\t\t*");
puts("* \t 3. R3 \t\t\t*");
puts("*********************************");
}
int main()
{
int choice;
do
{
menu();
printf("Option: ");
scanf("%d", &choice);
// clear screen
printf("\033[H\033[2J");
// Choice into programs:
switch (choice)
{
case 1:
doQuestionR1();
break;
case 2:
doQuestionR2();
break;
case 3:
doQuestionR3();
break;
default:
// clear screen
printf("\033[H\033[2J");
puts("\n\t INVALID! Please choose from 1 2 or 3.");
continue;
}
} while (1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment