I am currently doing college task to create a simple program using C, to enter and display prices of shirts in store. The problem is about that it doesn't show prices after I enter them in console. I don't really understand what is the problem, because my experience in coding on C ~1 week. Can anyone help with that? Thanks.
int main(void)
{
float small = 0.0;
float medium = 0.0;
float large = 0.0;
float total = 0.0;
printf("Set Shirt Prices\n");
printf("================\n");
printf("Enter the price for a SMALL shirt: $");
scanf("%f", &small);
printf("Enter the price for a MEDIUM shirt: $");
scanf("%f", &medium);
printf("Enter the price for a LARGE shirt: $");
scanf("%f\n", &large);
printf("Shirt Store Price List\n");
printf("======================\n");
printf("SMALL : $%f\n", small);
printf("MEDIUM : $%f\n", medium);
printf("LARGE : $%f\n", large);
return 0;
}
This line is problematic:
scanf("%f\n", &large);
It's expecting the user to type a float followed by additional text.
Change it to:
scanf("%f", &large);
And if want the end-of-line character, you can prepend it to the next statement.
printf("\nShirt Store Price List\n");
Related
I have faced a problem with c program.
In this case, whatever value I entered it's only output is 0.000000.
Please check this and give me a solution.
{
long double x,y,m,E,c;
printf("\t\t\t Enter a mass Hydrogen atoms: ");
scanf("%Lf", &x);
printf("\t\t\t Enter a mass Helium atoms: ");
scanf("%Lf", &y);
m=x-y;
c=3*(1*10^27);
E=m*c;
printf("\t\t\t Energy: %Lf", &E);
return 0;
}
You can change the length of the format in the output, using the following notation:
printf("\t\t\t Energy: %i.dlf", &E);
where "i" are the integers you want to show and "d" are the decimals.
Replace "i" and "d" with other numbers and experiment.
while you are getting input from user its become 0 and it's result into 0.0 cause you are using %Lf insted of %lf and also to power 10 by 27 you need to use pow function of math.h
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
double x,y,m,E,c;
clrscr();
printf("\t\t\t Enter a mass Hydrogen atoms: ");
scanf("%lf", &x);
printf("\t\t\t Enter a mass Helium atoms: ");
scanf("%lf", &y);
m=x-y;
c=3*(1*pow(10,27));
printf("\n%lf and %lf",x, y);
E=m*c;
printf("\n\t\t\t Energy: %lf", E);
getch();
return 0;
}
I'm just trying out a BMI (body mass index) calculator in C, but I keep on getting 0 as the final answer. Here's the code:
#include <stdio.h>
int main(){
int weight, height;
printf("Enter your weight in kilograms: ");
scanf("%d", &weight);
printf("Enter your height in meters: ");
scanf("%d", &height);
printf("Your BMI(body's mass index) is %f\n", weight/height*height);
return 0;
}
It displays 0 as the result.
I just need it to display the number with decimals (using %f and using int for weight and height).
Since the variables are integers, it's doing integer arithmetic, and returning an integer result. Printing an integer with %f causes undefined behavior.
Cast one of the variables to float to get a float result.
printf("Your BMI(body's mass index) is %f\n", (float)weight/(height*height));
Also, you have the formula wrong, it should be weight/(height*height).
i figured it out.
i just used this:
int weight;
float height;
printf("Enter your weight in kilograms: ");
scanf("%d", &weight);
printf("Enter your height in meters: ");
scanf("%f", &height);
printf("Your BMI(body's mass index) is %.2f\n", (weight)/(height*height));
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 would like my program to have a yes no prompt to ask if the program should be run again. Right now, it skips the user input completely. How can I do this effectively?
Any help would be appreciated. Thank you!
#include <stdio.h>
#include <math.h>
double Months(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 calculateMonths(double y)
{
double principal, annualInterestRate, monthlyPayment;
y = Months(principal, annualInterestRate, monthlyPayment);
return y;
}
int main()
{
double principal, annualInterestRate, monthlyPayment;
char response = 'Y';
printf("** Welcome to the CPSC 1010-S3 Payment Calculator **\n\n");
do{
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");
int roundedMonths = Months(principal, annualInterestRate, monthlyPayment)+.5;
printf("Total # of months needed to pay off:\t\t %d\n", roundedMonths);
double years = Months(principal, annualInterestRate, monthlyPayment)/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 - Months(principal, annualInterestRate, monthlyPayment)*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
int extraMonthlyPayment = monthlyPayment*.1;
int monthsEarly = ((log((monthlyPayment*1.1))-log((monthlyPayment*1.1)-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
int newMonths = Months(principal, annualInterestRate, monthlyPayment)-monthsEarly;
printf("Did you know if you paid an additional $%d per month, you would pay off your loan %d months earlier?\n\n", extraMonthlyPayment, newMonths);
printf("Do you wish to calculate another payoff? (Y/N):\t");
scanf("%c", &response);
}
while(response == 'Y' || response == 'y');
printf("Thank you for using the Payment Calculator!\n\n");
return(0);
}
Using scanf is suboptimal.
I'd add a
while (getchar());
line before the last scanf.
Please make sure you don't miss or mangle other data due to the newlines in the input buffer.
Here is my code:
#include <stdio.h>
float calculateBalance(float payment, float balance, float rate);
int main(void){
float loanAmount, interestRate, monthlyPayment;
printf("Enter amount of loan:");
scanf("%.2f", &loanAmount);
printf("\nEnter interest rate:");
scanf("%.1f", &interestRate);
printf("\nEnter monthly payment:");
scanf("%.2f", &monthlyPayment);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after first payment: %.2f\n", loanAmount);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after second payment: %.2f\n", loanAmount);
loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
printf("Balance remaining after third payment: %.2f\n", loanAmount);
}
float calculateBalance(float payment, float balance, float rate){
return (balance-payment + balance*rate);
}
I am trying to calculate the balance after three payments are made; However, when I try and run the code in terminal, it's not letting me input anything, and instead is just reading in random numbers and then calculating the balance after three payments(I want to be able to input things, but it isn't allowing me to). How do I fix this?
The issue is in the fmt string itself. As #T.C. said in a comment
try using
scanf("%f", &var);
instead. If there are only 2 dec values, then it will only read that.
You can try a adding a getc(stdin); after each scanf.