I'm trying to add tax to a number and after compiling the tax is not included in the addition to the number.
I have tried int or float also this %f but it seems it is not making the math after compiling.
#include <stdio.h>
int main()
{
float setprice, price, tax;
printf("enter price in dollars : \n");
scanf("%f", &price);
printf("Enter the tax: \n");
scanf("%f", &tax);
setprice=(price*tax)+price;
printf("total = %f\n", price);
return 0;
}
After entering the code on IDE this is the output.
enter price in dollars :
100
Enter the tax:
50
total = 100.000000
Your problem is adding "%" to tax variable. when you enter 50 it should be like 50%.
Then do this
tax = tax / 100
setprice = (price * tax) + price;
printf("total = %f\n", setprice);
or you can try this
setprice = (price * (tax / 100)) + price;
printf("total = %f\n", setprice);
these answers will work!
Related
I am new to C programming and I am getting conflicting types for netPay and then expected specifiers errors within the print function lines. I have tried many suggestions, but still seem to be missing something here. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
int main() {
//Declare the variables with their data types
int hoursWorked;
float wagesPerHour;
//Prompt user to enter the hours worked with a keyboard
printf("Please enter the hours worked this week: ");
scanf("%d", &hoursWorked);
//Prompt the user to enter their hourly pay rate
printf("\nPlease enter your hourly pay rate, ");
printf("enter it in $XX.XX format: $");
scanf("%f", &wagesPerHour);
//Declare the variables and date types used to calculate the grossPay and taxes
/*Calculate the gross pay based on the amount of wagesPerHour entered by the user if hoursWorked is equal to
or less than 40. If the hoursWorked is more than 40, then calculate the gross pay for the first 40 hours as
wagesPerHour hoursWorked and add it to wagesPerHour * 1.5 times the number of hoursWorked over 40*/
float grossPay;
float tax;
int overtime = (hoursWorked - 40);
float oTWagesPerHour = (wagesPerHour * 1.5);
if (hoursWorked<=40)
{
grossPay = hoursWorked * wagesPerHour;
}
else
{
grossPay = grossPay + (overtime * oTWagesPerHour);
}
/*Calculate the tax amount based on the grossPay amount equal to or less than 600 times the tax rate of 0.15.
If the grossPay is more than 600, then calculate the tax amount for the first 600 times the tax rate of 0.20*/
if (grossPay <= 600)
{
tax = grossPay * 0.15;
}
else
tax = tax + (grossPay * 0.20);
}
float netPay;
netPay = (grossPay - tax);
//Print the statements below based on the user input and the calculations for grossPay, tax, and netPay
printf("\nYou entered %d as the hours worked this week and $%.2f as your hourly pay rate.", hoursWorked, wagesPerHour);
printf("\nGross Pay:\t $%.9f", grossPay);
printf("\nTaxes Paid:\t $%.9f", tax);
printf("\nNet Pay:\t $%.9f", netPay);
return 0;
}
enter image description here
You're missing an opening curly brace here:
if (grossPay <= 600)
{
tax = grossPay * 0.15;
}
else
tax = tax + (grossPay * 0.20);
}
There is no { after the else, so the closing curly brace that follows is actually ending the function. This causes the declaration of netPay to be a file-level declaration. The assignment that follows then produces an error since it's outside of a function.
Just add a { after the else to fix it.
I would like overpay to display overpay as the total amount paid if the result was not rounded. calculateMonths must be an integer which makes this much more difficult. I tried using
double dblCalculateMonths = (double)calculateMonths(principal, annualInterestRate, monthlyPayment);
but it did not work properly. I am not sure how to fix this problem while keeping calculateMonths as an integer. Help would be very much appreciated!
I would like the results to look like this:
** Welcome to the CPSC 1010-S3 Payment Calculator **
Enter the principal amount: 5000.00
Enter the annual interest rate (in %): 15.0
Enter the monthly payment: 100.00
Calculating...
Total # of months needed to pay off: 79
App roximate # of years needed to pay off: 6.6
Total interest paid: $2900.00
Total amount paid: $7900.00
You overpaid: $4.43
But the last line displays as
You overpaid: $100.00
I am not sure how to change this. My list of code is below
#include <stdio.h>
#include <math.h>
int calculateMonths(double principal,double annualInterestRate,double monthlyPayment)
{
double x = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return x;
}
int main()
{
double principal, annualInterestRate, monthlyPayment;
printf("** Welcome to the Payment Calculator **\n\n");
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
double roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment)+1;
printf("Total # of months needed to pay off:\t\t %.0f\n", roundedMonths);
double years = roundedMonths/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double dblCalculateMonths = (double)calculateMonths(principal, annualInterestRate, monthlyPayment);
double overpay = amountPaid - dblCalculateMonths*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
printf("%f", dblCalculateMonths);
printf("\n\n");
return(0);
}
what's the logic in keeping calculateMonths() an integer? make it a double and it should work.
double calculateMonths(double principal,double annualInterestRate,double monthlyPayment)
and make roundedMonths an integer. Also you could add 0.5 to have the right round.
int roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment)+0.5;
printf("Total # of months needed to pay off:\t\t %d\n", roundedMonths);
LE: Try this:
int calculateMonths(double principal,double annualInterestRate,double monthlyPayment, double *dblCalculateMonths)
{
*dblCalculateMonths = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return *dblCalculateMonths;
}
int main()
{
double principal, annualInterestRate, monthlyPayment, dblCalculateMonths;
printf("** Welcome to the Payment Calculator **\n\n");
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
double roundedMonths = calculateMonths(principal, annualInterestRate, monthlyPayment, &dblCalculateMonths)+1;
printf("Total # of months needed to pay off:\t\t %.0f\n", roundedMonths);
double years = roundedMonths/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double overpay = amountPaid - dblCalculateMonths*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
printf("%f", dblCalculateMonths);
printf("\n\n");
return(0);
}
I have a price for a car, let's say 10000. I want to apply a 20% sale to this price.
I have a struct in which auta->cena is a float.
int year, i, n=0, pocet=1, sale;
scanf(" %d", &year);
scanf(" %d", &sale);
for(i=1; i<=pocet; i++){
if(year == auta->rok){
++n;
pocet++;
auta->cena *= ((float)(100 - sale) / 100); //calculate price after 20% sale
//temp = ((int)(temp * 100 + 0.5)) / 100.0; //use this formula to round up final price, it doesnt work, I get 0.00
printf("%.2f\n", auta->cena);
}
auta = auta->dalsi;
}
I am not good at converting—can anyone explain it to me, please? How should I go about it?
If you're going to print the value with %.2f, you don't have to do any rounding. But if you want to round the value internally, the following will work. I'm printing it with %.5f to show that the value is really changed.
#include <stdio.h>
int main() {
int discount;
double price;
printf("Enter a price: ");
scanf(" %lf", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);
price *= (100.0 - discount) / 100; // calculate price after discount
printf("after discount: %.5f\n", price);
price = (int)(100 * price + 0.5) / 100.0;
printf("rounded: %.5f\n", price);
}
I'm using double above to preserve enough precision to demonstrate that the calculation works with, for example, a price of 10000 and a discount of 20. If you do it with float, you lose enough precision that there's no point in rounding to the nearest cent. The internal value will be imprecise anyway.
Here is a variant of the same code using float:
#include <stdio.h>
int main() {
int discount;
float price;
printf("Enter a price: ");
scanf(" %f", &price);
printf("Enter a percentage discount: ");
scanf(" %d", &discount);
price *= (100.0 - discount) / 100; // calculate price after discount
printf("after discount: %.5f\n", price);
price = (int)(100 * price + 0.5) / 100.0;
printf("rounded up: %.5f\n", price);
return 0;
}
I am trying to get a c program to ask me how many items i am buying than to ask the price for each item while keeping a rolling total and than ask again in a loop. I have the loop working fine but am having problems getting it to give me proper output.
here is the output i am getting when i run it.
http://i119.photobucket.com/albums/o134/halodude808/assignment2_zpsd46e84b8.jpg
#include <stdio.h>
#include <math.h>
int main(void)
{
//variables used
float penny = .01;
float nickel = .05;
float dime = .1;
float quarter = .25;
int items = 0;
float paid= 0.0;
float total = 0.0;
float price =0.0;
int counter =0.0;
for (;;)
{
printf("Please enter the number of grocery items:");
scanf("%d", &items);
for (counter = 1; counter <= items; counter++)
{
printf("Please enter the price for item #%d:", counter);
scanf("%f", &price);
total += price;
}
printf("items = %d total = %f \n", &items, &total);
getchar();
getchar();
}
}
Change
printf("items = %f total = %f \n", &items, &total);
to
printf("items = %i total = %f \n", items, total);
Also, you might want to consider checking for invalid values (zeroes, negative, characters, etc).
Are there any C language programmers on here that can help me figure this out?
I am having trouble getting the calculation for the average miles per gallon to work and my head is spinning. I would really appreciate if anyone have a solution ^_^
int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;
for (x = 1; x <= 3; x++)
{
printf("Enter the number of gallons used for tank #%i: ",x);
scanf("%f", &num1);
fflush(stdin); /* clear input buffer */
printf("Enter the number of miles driven: ");
scanf("%f", &num2);
fflush(stdin); /* clear input buffer */
/*--------------------------------------------------------------*/
/* calculate and output the miles per gallon from user input. */
/* ------------------------------------------------------------ */
division = num2 / (float) num1;
printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
num2, num1, division);
total_num2 = total_num2 + num2;
printf("The total of miles is %f\n", total_num2);
total_num1 = total_num1 + num1;
printf("The total of gallons is %f\n", total_num1);
}
avg = (double) total_num2 / total_num1;
printf("Overall average miles per gallon for three tanks: %.1f", avg);
You don't initialise your totals, so they are undefined. When you start adding to them, you get undefined results. I bet that's what you mean by it not working.
Do this:
double total_num1 = 0;
double total_num2 = 0;