I am learning the foundation of programming bridging class for my university, I have done some small projects on my own with C to demonstrate some logics.
Programming is a skill to use computer to solve problems, before writing down the code, it is good to do an algorithm to demonstrate a logic.
/****************************************************************************** This program demonstrates the use of: 1. one dimension array. 2. iteration and selection. 3. functions. 4. To include %*c at the end of the format specifier of scanf() in while or do...while loop. *******************************************************************************/ #include <stdio.h> #include <ctype.h>//required for using tolower() #include <stdbool.h>//required for boolean operator void menu (void); void prime_prompt(void); void validate_prime_num(int); void triangle_prompt(void); void triangle_type(int,int,int); bool validate_triangle(int,int,int); void calculator_prompt(void); void draw_lines(int,char); /* Use an array to store the characters. Just for drawing lines on the menu. */ void draw_lines(int j,char pattern) { int i; char lines[j]; for(i=0;iside3 && side2+side3>side1 && side1+side3>side2) { is_it_a_triangle = 1; //1 for yes. } else { is_it_a_triangle=0; //0 for no. } return is_it_a_triangle; } /* This function is for identifying the type of triangles. Before identifying, validate_triangle function is called to validate if it is an invalid triangle. */ void triangle_type(int side1, int side2, int side3) { if(validate_triangle(side1,side2,side3)==0) { printf("Invalid triangle.\n"); } else if(side1==side2 && side1==side3 && side2==side3) { printf("This is an equilateral triangle\n"); } else if(side1==side2 || side1==side3 || side2==side3) { printf("This is an isoceles triangle\n"); } else { printf("This is a scalene triangle\n"); } } /* Simply a place to execute the triangle_type function for validation and triangle type identification. */ void triangle_prompt() { int side1,side2,side3; printf("Enter 3 sides of a triangle:");//user to define 3 sides. scanf("%d%d%d%*c",&side1,&side2,&side3); triangle_type(side1,side2,side3); } /* Prime number is a number that can only be divided by 1 or itself. The solution is to exclude 1 and the number itself to do division testing. If any number in the range of numbers between 2 and its own can be divisible i.e. there is no remainder, the number is not a prime number. */ void validate_prime_num(int num) { int i; bool flag=1; for(i=2;i<num;i++) { if(num%i==0) { flag=0; //0 for not prime number. break; } } if(flag==1) { printf("%d is a prime number.\n",num); } else { printf("%d is not a prime number.\n",num); } } //A simple prompt to call the validate_prime_num function. //Notice %*c is included in the scanf() to ignore Enter. void prime_prompt() { int num; printf("Enter a number to validate if it is prime number:\n"); scanf("%d%*c",&num); validate_prime_num(num); } //A simple menu. void menu() { char response,lines[37]; int i; do { draw_lines(37,'*'); printf ("*Enter three option, press q to quit:\n"); printf ("*Enter a for validating prime numbers.\n"); printf ("*Enter b for simple calculator.\n"); printf ("*Enter c for triangle.\n"); draw_lines(37,'*'); printf ("Your choice:"); scanf ("%c%*c", &response);//%*c is necessary in this loop to escape the Enter key. response=tolower(response); switch (response) { case 'a': prime_prompt(); break; case 'b': calculator_prompt(); break; case 'c': triangle_prompt(); break; case 'q': break; default: printf ("You have entered an invalid choice.\n"); } }while(response!='q'); } int main () { menu(); return 0; }