You are on page 1of 2

main.

c
#include <stdio.h>

// the ex-s instruction page wasn't found

/******************************************************************** Function Name: main The Input: an integer between 1 and 5 The Output: the function performs a unique task for each integer for more information look at the comments in the body of the function or the exercise's instructions The Function Operation: while loop asks for an integer (breaking when 5 is entered) and performs the unique task according to this integer (using switch) ********************************************************************/ int main() { const float PI = 3.14; int option_code, num, odd_sum, i, squares_sum, character_numeric_value, sign; char ch; float multipier, calc, f1, f2; do { // main loop asking for the operation code as spesified in the exercise instructions printf("Enter a number between 1 and 5: \n"); scanf("%i", &option_code); while (option_code < 1 || option_code > 5) { //handling out-of-range code printf("illegal option. Enter a number between 1 and 5:\n"); scanf("%i", &option_code); } switch (option_code) { case 1: // summing the odd numbers of a series of numbers printf("\nEnter a series of integers (to finish enter the sentinel 10) and the program will calculate its odd numbers' sum: \n"); odd_sum = 0; do { //the loop that scans integers and adds them to the general sum if they are odd scanf("%i", &num); if(num % 2 == 1 || num % 2 == -1){ odd_sum += num; } } while (num != 10); printf("\nThe sum of the odd numbers of the series you entered is %i\n\n", odd_sum); break; case 2: // calculating six numbers according to (num1*num2)/(num3*num4)/(num5*num6) //=num1*num2/(num3/num4/num5/num6) printf("\nEnter six numbers and the program will calculate their calc=(num1*num2)/(num3*num4)/(num5*num6) \n"); calc = 1; for (i = 1; i <= 2; i++) { //multiping (num1*num2) scanf("%f", &multipier); calc *= multipier; } for (i = 3; i <= 6; i++) { //dividing /(num3/num4/num5/num6) scanf("%f", &multipier); if (multipier == 0) break; calc /= multipier; } for (; i <= 5; i++) { // a loop in case a zero was entered scanf("%f", &multipier); } // printing result if (i != 7) printf("error, division in zero\n\n"); //if i is not seven it means the loop was breaked because of a zero else printf("calc is %0.3f\n\n", calc); break; case 3: //printing digits squares sum of an input integer printf("\nEnter an integer and the program will calculate its digits squares sum\n"); scanf("%c", &ch); //in order not to get the newline character entered before .For further info visit http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2008-09/msg01934.html squares_sum = -1; // in order to get into the while loop while (squares_sum == -1) { //if sum is -1 it means a un-numerical character was entered (or it's the first shot) squares_sum = 0; //resets the sum sign = 1; // default sign is positive scanf("%c", &ch); //scanning the character entered into ch if (ch == (int) '-') { //checks if first character is '-' sign = -1; // assigning the sign to negative ch = (int) '0'; //assiging the character to 0 so the sum will not change or an error will occur } while (ch != 10) { //10 in a newline char if (ch >= (int) '0' && ch <= (int) '9') { character_numeric_value = ch - (int) '0'; character_numeric_value *= character_numeric_value; squares_sum += character_numeric_value; } else { // un-numeric char was entered squares_sum = -1; // in order to loop again printf("\nneeds an integer. Enter a new number:\n"); while (ch != 10) { //ignores the rest of the input until the new line scanf("%c", &ch); } break; } scanf("%c", &ch); } } // printing result with its suitable sign printf("\nThe digits squares sum is %i\n\n", sign * squares_sum); break; case 4: // area-calculation option. Representing the area calculations types the user may perform printf("\nWhat type of area-calculation would you like to perform?\nEnter 1 for cicle-area calculation\nEnter 2 for square-area calculation\nEnter 3 for triangle-area calculation\n"); scanf("%i", &num); //asks for input according to the user choice switch (num) { case 1: //circle area calculation. Asks for radius length printf("\nEnter a radius of a circle\n"); scanf("%f", &f1); // handling a negative input if (f1 < 0) { printf("incorrect input\n\n"); break; //returning to main menu } // printing the result printf("\nThe circle area is %0.3f\n\n", f1 * f1 * PI);

break; case 2: //square area calculation. Asks for an edge length printf("\nEnter an edge length of a square:\n"); scanf("%f", &f1); // handling a negative input if (f1 < 0) { printf("incorrect input\n\n"); break; //returning to main menu } // printing the result printf("\nThe square area is %0.3f\n\n", f1 * f1); break; case 3: //triangle area calculation. Asks for an edge and its altitude lengths printf("\nEnter the lengths of an edge and its altitude of a triangle\n"); scanf("%f %f", &f1, &f2); // handling a negative input if(f1 < 0 || f2 < 0) { printf("incorrect input\n\n"); break; //returning to main menu } // printing the result printf("\nThe triangle area is %0.3f\n\n", f1 * f2 / 2); break; } break; } } while (option_code != 5); printf("\nGOOD BYE"); return 0; }

You might also like