Skip to content

Instantly share code, notes, and snippets.

@s3f
Created December 29, 2016 22:46
Show Gist options
  • Save s3f/04834c14487d87466bcdd620c97f660b to your computer and use it in GitHub Desktop.
Save s3f/04834c14487d87466bcdd620c97f660b to your computer and use it in GitHub Desktop.
Chapter 9 - Doing math with C created by s3f - https://repl.it/Ed4H/2
// This first sample program is to practice with using math operators
/*
#include <stdio.h>
main()
{
float a = 19.0;
float b = 5.0;
float floatAnswer;
int x = 19;
int y = 5;
int intAnswer;
floatAnswer = a / b;
printf("%.1f divided by %.1f equals %.1f\n", a, b, floatAnswer);
floatAnswer = x/y;
printf("%d divided by %d equals %.1f\n", x, y, floatAnswer);
intAnswer = a/b;
printf("%.1f divided by %.1f equals %d\n", a, b, intAnswer);
intAnswer = x % y;
printf("%d modulus %d equals %d", x, y, intAnswer);
return 0;
}
// This second practice program asks a user for a number of tires purchased and price per tire. It then calculates a total price and adding the sales tax.
#include <stdio.h>
#define SALESTAX .06
main()
{
int numTires;
float tirePrice;
float beforeTax;
float netSales;
printf("How many tires did you purchase? ");
scanf(" %d", &numTires);
printf("What was the cost per tire (enter in $XX.XX format)? ");
scanf(" $%f", &tirePrice);
// Now I compute the price
beforeTax = tirePrice * numTires;
netSales = beforeTax + (beforeTax * SALESTAX);
printf("%You spent $%.2f on your tires\n\n\n", netSales);
return 0;
}
*/
// This last practice program claculates the average of 4 grades and alos does some other basic math
#include <stdio.h>
main()
{
int grade1, grade2, grade3, grade4;
float averageGrade;
float gradeDelta;
float percentDiff;
grade1 = grade3 = 88;
grade2 = 79;
printf("What did you get on the fourth test?");
printf(" (An integer between 0 and 100)");
scanf(" %d", &grade4);
averageGrade = (grade1 + grade2 + grade3 + grade4) / 4;
printf("Your average grade is %.1f.\n", averageGrade);
gradeDelta = 95 - averageGrade;
percentDiff = 100 * ((95 - averageGrade) / 95);
printf("Your grade is %.1f points lower than the ", gradeDelta);
printf("top grade in the class(95)\n");
printf("You are %.1f percent behind", percentDiff);
printf("that grade!\n\n\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment