Im making a shipping calculator that ask how many pounds your item weighs. If it weighs less than 50 pounds the charge is 6.00. If it weighs 50 pounds but less than 100 pounds the charge is 10.50. The shipping charge also doubles for every 1000 miles. Im having a problem figuring out how to make the calculator double if user inputs over 1000 miles.
int main()
{
double weight, distance, charge;
printf("Enter your package weight: \n");
scanf("%lf", &weight);
printf("How far are you sending the package? \n");
scanf("%1lf", &distance);
if(weight <= 50)
charge = 6;
else
if(weight <= 100)
charge = 10.50;
printf("Shipping charge: %.2lf\n", charge);
}
When I run the program I dont get the correct shipping charge.
Maybe you should printf the charge instead of the weight?
In your code you are outputting weight in the end and not the charge, the one that was actually mutated.
Related
I have to make a program that calculates the amount of coins needed to pay for something. I.E.
total cost = $1.35
$1.00 = 1, remaining $0.35
$0.25 = 1, remaining $0.10
$0.10 = 1, remaining $0.0
$0.05 = 0, remaining $0.0
$0.01 = 0, remaining $0.0`
My question is how do I do these math operations using modulus? as part of the assignment I have to do the calculations using modulus and integer division but i can't see how using modulus would work for this code. The code I started out with is the following
#include<stdio.h>
#include<math.h>
int main(){
float topay, change;
float gst = 1.13; //gst amount
int loonies, quatres, nickles, dimes, pennies; //coin variables
printf ("Please enter the amount to be paid: $");
scanf ("%f", &topay); //Input for amount to be paid
printf ("GST: %.2f\n", gst);
printf ("Balance owing: $%.2f\n", topay=topay*gst);
loonies = topay; //Math for amount of loonies
change = topay - loonies; //Math for balance owing
quatres = change/0.25; //Math for how many quarters needed
printf ("Loonies required: %d, balance owing $%.2f\n", loonies, change);
printf ("Quarters required: %d, balance owing $%.2f\n", quatres, change = change-(quatres*0.25));
return 0;
}
Modulus tells you the remainder following a division operation. So:
$1.35 modulus $1.00 yields 35 cents
So, you need to store 35 cents somewhere but also subtract it from the balance:
$1.35 - $0.35 = $1.00
Now you use integer division to see how many dollar coins in $1.00. Answer is one.
Okay, now the balance is $0.35. And the next coin is a quarter, $0.25.
So, $0.35 modulus $0.25 is $0.10. Store the ten cents.
Subtract $0.10 from the balance gives $0.25. How many quarters in $0.25? Answer one.
Okay, now the balance is $0.10.
And around you go.
This can be done in a simple loop with an array holding the coin denominations.
i wanted to ask is my coding have something missing or i did wrong? I was doing function to calculate the employee salary. it can compile and run, but not showing the output that should produce.
I'm trying to get the out like this: (example the value of the output)
Please enter your salary: 1200
Please enter the persentage of epf deduction:11
Your epf deduction is $132.00
Your bonus (half of salary) is $600.00
Your total pay is $1200.00-$132.00+$6000.00=$1668.00
My coding is like this:
void BasicSalary(); //Ask user to enter basic salary.
void EPF(double); //To calculate the amount of employees evident fund (EPF) deduction from basic salary.
void CalculateBonus(double); //To calculate the amount of bonus. Assume bonus is half of basic salary.
double DisplayTotalPay(double,double); //To calculate the total pay after EPF deduction and bonus reward.
double salary,epf,totalepf,totalbonus;
int main()
{
void BasicSalary();
void EPF(double);
printf("\nYour EPF deduction is $%f ",totalepf);
printf("\nYour bonus (half of salary) is $%f ");
void CalculateBonus(double);
double DisplayTotalPay(double,double);
}
void BasicSalary()
{
printf("\nPlease enter your salary: ");
scanf("%f",&salary);
}
void EPF(double)
{
printf("\n\nPlease enter the percentage of EPF deduction: ");
scanf("%f",epf);
totalepf= ( epf / 100 ) * salary;
}
void CalculateBonus(double)
{
totalbonus = salary / 2;
}
double DisplayTotalPay(double,double)
{
printf("\nYour total pay is $%f - $%f + $%f = $%f ", salary, totalepf, totalbonus, salary-totalepf+totalbonus);
}
you have kept everything in your main under comment. How it will work .?
Also your functions argument variables are not named. Since all your variable are global, you don't require any argument. Better remove those data type declaration.
Another point is, use "%lf" with Double when scanning. "%f" is for float.
I am in programming 1 right now. I am creating a program that is meant to show the calculation of a teachers discount (12%) on a total purchase amount. When printing the amount of the discount on screen based on purchase total, I am coming up with an incorrect total. Here is the Code:
double total_b4_tax;
printf("\nEnter the purchase total : ");
scanf("%lf", &total_b4_tax);
getchar ();
if (teach_music == 'y' || 'Y')
{
double discount_total = teach_music * .12;
printf("Total purchase $ %.2lf\n", total_b4_tax);
printf("Discount total $ %.2lf\n", discount_total);
}
My discount total is coming to 14.52 when in actuality 122.0 * .12 is 14.64. How do I solve this issue?
You are trying to calculate a discount by multiplying a character with a float:
double discount_total = teach_music * .12;
before that, you made sure that teach_music is 'y' or 'Y'. Im curious how that compiled, and how C comes to the conclusion that 'y' or 'Y' is 121.
EDIT: Harr, 'Y' is ASCII 121. So multiplying that with .12 results in 14.52
Anyway, I assume that
double discount_total = total_b4_tax * .12;
makes it all better.
I'm writing code that reads the input for water consumption and displays the bill. the fixed price is $10. for the first 30 meters it costs $0.6 per meter, for the next 20 meters it costs $0.8, then the next 10 meters it cost $1.2 per meter and additional meters are $1.2 but for some reason my code asks the user to input a value and does nothing else. what am I doing wrong? Is it my if statements? I feel as though they are okay and my calculations may be the problem. here is the code so far:
#include <stdio.h>
int main()
{
int watercom, fixedrate, first30, next20, next10, additional, firstcal, secondcal,thirdcal, fourthcal;
printf("what is our water consumption?\n");
scanf("%i\n",&watercom);
fixedrate = 10;
first30 = 0.6;
next20 = 0.8;
next10 = 1.0;
additional = 1.2;
firstcal = fixedrate * first30;
secondcal = fixedrate * next20;
thirdcal = fixedrate * next10;
fourthcal = fixedrate * additional;
if ( watercom <= 30)
printf("your bill is %i", &firstcal);
else if (watercom >= 31 && watercom <= 50)
printf("your bil is %i", &secondcal);
else if (watercom >= 51 && watercom >= 60)
printf("your bill is %i", &thirdcal);
else if (watercom >= 61)
printf("your water bill is %i", fourthcal);
return 0;
}
if ( watercom <= 30)
printf("your bill is %i", &firstcal);
else if (watercom >= 31 && watercom <= 50)
printf("your bil is %i", &secondcal);
else if (watercom >= 51 && watercom >= 60)
printf("your bill is %i", &thirdcal);
else if (watercom >= 61)
printf("your water bill is %i", fourthcal);
There are a number of simplifications that can be applied:
You don't need the >= part in each if statement as if watercom <= 30 then it is implicitly >= 31.
Also you don't pass in the address-of (&) values to printf, you pass in the values directly.
You also don't have a default case for when watercom is outside the range of expected values:
if ( watercom <= 30 )
printf("your bill is %i", firstcal);
else if ( watercom <= 50)
printf("your bill is %i", secondcal);
else if ( watercom <= 60)
printf("your bill is %i", thirdcal);
else
printf("your water bill is %i", fourthcal);
In addition to what others have pointed out, all your variables are int but some need to be double [because you're setting them to 0.6, etc.]:
int watercom;
double fixedrate;
double first30;
double next20;
double next10;
double additional;
int firstcal;
int secondcal;
int thirdcal;
int fourthcal;
Yeah your only int is fixedRate. The rest are floats.
Also by what you say in your question that is not what is in your program. If the fixedRate is 10 and the first 30 is at a rate of .6 then if I input 30 I should get fixedRate + (30*.6).
You need to add the fixedRate to all calculations and add the calculations for each tier together for your final amount.
Work the problem on paper before you try to write the program. You should know what output is expected before you test your program.
😊Happy Day!
The algebra of the calculations, as they appeared in the first version of this post, seems all wrong to me. To get you started on that: as well as these amount * rate multiplications, you need to be performing an addition --- a fixed rate term, plus another amount if you go over the initial allowance, plus another amount when you enter the next price bracket, and so on. To calculate each of these terms, you need to be subtracting the amount of water already-paid-for in previous terms as you go along.
But that's not the question. You asked why the program was not printing anything at all. For me, after making a command-line executable out of this using Visual Studio on Windows, I did replicate that problem: I input the value, then nothing seems to happen. But then the answer is finally printed when I press control-C to halt the program.
To fix this, remove the \n from the scanf command.
Then proceed to your other problems, which include (a) getting the algebra right, and (b) printing the value (e.g. firstCal) not the address (&firstCal) of your variables.
Need some help with calculating the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of i. The given formula is: P = L[i(1 + i)n]/[(1 + i)n - 1]. I wrote a code but it didn't calculate Payment. I'm wondering if it is because I use double type together with int (for number of months) or the problem with formula?! Please help.
#include<stdio.h>
#include <math.h>
double calculatePayments(double rate, double loan, int payments);
int main() {
double principal, i, monthlyP;
int month;
printf ("Enter the principal amount: ");
scanf ("%f", &principal);
printf ("Enter the interest amount: ");
scanf ("%f", &i);
printf ("Enter the term in months: ");
scanf ("%d", &month);
monthlyP = calculatePayments (i, principal, month);
printf ("The monthly payment amount is %.2f: ", monthlyP);
return 0;
}
double calculatePayments(double rate, double loan, int payments) {
double mPayments;
mPayments = loan*(rate*(1 + rate)*payments)/((1 + rate)*payments - 1);
return mPayments;
}
Your scanf() requests %f format for a double; it should be %lf.
In addition to the need to fix the input (%lf instead of %f for doubles), I think your payment formula is wrong: since the future value of money grows exponentially, the formula should feature raising numbers to a certain power, which it does not.
The correct formula looks as follows (from here):
Since the loan needs to be paid off completely, FV is equal to zero.
Since pow(i+1, n) is used twice, it's a good idea to compute it once, and use the resultant variable in two places. The final version of this computation looks like this:
double calculatePayments(double rate, double loan, int payments) {
double mul = pow(1+rate, payments);
return (loan * mul * rate) / (mul - 1);
}
Demo on ideone computes the payment on $100,000 at 0.004% per month for 30 years at $524.67, which is the same value that excel's PMT function returns.
Note : When you enter the rate of 5.6% in your formula and in another calculator, don't forget that your formula takes the rate per month, not per year. Therefore, the rate that you plug into outside calculators must be 12 times what you enter into your calculator (for 5.6% annually you should enter 0.00466666
One of the first rules of debugging is to make sure you print the inputs to ensure that the program got the values you expected. It didn't, because you wrote:
scanf ("%f", &principal);
Since principal is a double, the format needs to be "%lf". Repeat for the interest rate. You should also check that the inputs succeeded:
if (scanf("%lf", &principal) != 1)
{
fprintf(stderr, "Failed to read principal\n");
return(1);
}
If you'd printed out the input values, you'd have known what was wrong immediately.
Read what dasblinkenlight said.
Also,
Fix your declarations and the variables you are using scanf() for.
principal should be loan. month should be payments.
i is okay. You need to calculate the monthly decimal number of the percentage given.
For instance,
interest = (i/100) /12;
Do that before your function call. Then just basically use dasblinkenlight's function at the bottom of your main.
hope you score a "10" ;)