"Wrong answer" in HackerRank "Day 2: Operators" challenge - c

I am a novice programmer and coder. So, I have taken 30 days of coding challenge in HackerRank, but when I am running the "Day 2: Operators" problem in C, it shows no errors. The code is:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int total;
double meal_cost;
scanf("%lf", &meal_cost);
int tip_percent;
scanf("%i", &tip_percent);
int tax_percent;
scanf("%i", &tax_percent);
double tip = (meal_cost * tip_percent) / 100;
double tax = (meal_cost * tax_percent) / 100;
total = (int)(meal_cost + tip + tax);
printf("The total meal cost is %d dollars.", total);
printf("The total meal cost is %d dollars.", total);
return 0;
}
Input:
12.00
20
8
Expected output:
The total meal cost is 15 dollars.
Actual output:
Wrong Answer

This is more clear program. As it was discovered tips are NOT taxed.
Calculations are done on double to avoid early truncations. Final result is printed as double or as needed by Hackerrank rounded to the int number.
Contribution and help of Weather Vane is greatly appreciated.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("The total meal cost is %.2f dollars.\n",total);
printf("The total meal cost is %d dollars.\n", (int)(total+0.5));
return 0;
}
Output:
12
20
8
The total meal cost is 15.36 dollars.
The total meal cost is 15 dollars.

Running code "Day 2: Operators" problem in C
- Given the meal price (base cost of a meal), tip percent (the
percentage of the meal price being added as tip), and tax percent
(the percentage of the meal price being added as tax) for a meal,
find and print the meal's total cost in a HackerRank
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("%d", (int)(total+0.5));
return 0; }
Input
12.0
20
8
Output
15

Related

Calculation producing whole number instead of a decimal

I have some code:
#include <math.h>
#include <stdio.h>
int main(void) {
int amount;
// user input for annual income
printf("Enter your amount: ");
scanf("%d", &amount);
if(amount <= 34000)
amount = amount * .33;
double new_amount = (double) amount;
printf("your calculated amount: %.2f\n", new_amout);
return 0;
}
I am trying to take in an integer value and then convert it to a double after making my calculation. However, when i enter my int, it prints a decimal but with no non-integer part.
For example, if my amount is 19522, my code prints 6442.00 instead of 6442.26.
The easiest fix is to change the data type of amount to double and update the format strings, and eliminate the new_amount variable as it doesn't do anything for you. Also, check that scanf() was successful otherwise you may be operating on uninitialized data:
#include <math.h>
#include <stdio.h>
int main(void) {
double amount;
printf("Enter your amount: ");
if(scanf("%lf", &amount) != 1) {
printf("scanf failed\n");
return 1;
}
if(amount <= 34000)
amount *= .33;
printf("your calculated amount: %.2lf\n", amount);
}

How to solve Half Practice Problem from CS50's? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I'm struggling to solve this problem from CS50's introduction to computer science. It might seem simple but I still couldn't get it why it's not working properly.
So, I have to prompt a bill amount (type float) before tax and tip, calcule tax and tip using its percentage and then add them all and split it in two. The user prompt is a bill amount (type float) before taxes, tip and tax percentages. I have to complete a given function as shown below.
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
return 0.0;
}
And we should have
Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
You will owe $8.17 each!
https://cs50.harvard.edu/x/2023/problems/1/half/
What I've been trying
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
float bill = bill_amount;
float tax = tax_percent / 100;
int tip = tip_percent / 100;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * tax + bill;
float bill_after_tax_tip = bill_after_tax * tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
Your task is to complete the function half, not to make changes in the function main. Therefore, the changes you made to the function main violate the constraints of the task.
For this reason, you should divide tax and tip by 100 in the function half, not in the function main:
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * ( tax / 100.0 ) + bill;
float bill_after_tax_tip = bill_after_tax * ( tip / 100.0 ) + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
Note that it is important to write ( tip / 100.0 ) instead of ( tip / 100 ), because the former is a floating-point division, whereas the latter is an integer division, which always has an integer result. This is because tip and 100 are both of type int. For example, ( 20 / 100.0 ) evaluates to the floating-point number 0.2, whereas ( 20 / 100 ) evaluates to the integer 0. You want the result to be 0.2, not 0.
The type of tip should be float since you are dividing it by 100 which results in a decimal number.
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
float bill = bill_amount;
float tax = tax_percent / 100.0;
float tip = tip_percent / 100.0;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, float tip)
{
float bill_after_tax = bill \* tax + bill;
float bill_after_tax_tip = bill_after_tax \* tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}

During compilation it is not asking for user input of rate and time

// Q. Find out the simple interest.
#include <stdio.h>
#include <math.h>
int main()
{
int principal, rate, time;
printf("principal\n");
scanf("% d", &principal);
printf("rate\n");
scanf("% d", &rate);
printf("time\n");
scanf("% d", &time);
printf("Simple interest is %d", principal * rate * time / 100);
return 0;
}
For scanf to work, you need to remove the space between the % and the d in the format strings: % d.
#include <stdio.h>
#include <math.h>
int main()
{
int principal, rate, time;
printf("principal\n");
scanf("%d", &principal);
printf("rate\n");
scanf("%d", &rate);
printf("time\n");
scanf("%d", &time);
printf("Simple interest is %d", principal * rate * time / 100);
return 0;
}
Also, the program cannot ask for input during compilation. It will only ask for input when it is run.

How do I only print the integers after a decimal point in C

This is my output now
cost = 12.88;
printf("Cost: %.0lf dollars and %.2lf cents", cost, cost-floor(cost));
//output
12 dollars and 0.88 cents
I need my output to look something like this
cost = 12.88
printf("%d Dollars and %d cents", cost)
output
12 dollars and 88 cents
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void print_cost(double cost)
{
double int_part;
double fract_part = modf(cost, &int_part);
printf(
"%d Dollars and %d cents", //"%d" based on your example
(int) int_part, //we must therefore cast to integer
abs((int)(fract_part * 100.0)) //fract is signed, abs value needed
);
}
int main()
{
print_cost(12.88);
print_cost(-12.88);
print_cost(12.8899);
return 0;
}
Reference:
abs
modf

Score Reporting problems

Can u guys help me how to make a simple report calculation
1st score x 20%
2nd score x 40%
3rd score x 40%
Sample :
Input :
65 56 100
Output :
75.00
My code :
#include <stdio.h>
#include <math.h>
int main() {
int score1, score2, score3;
float amount;
float n;
float percent1 = 0.2;
float percent2 = 0.4;
float percent3 = 0.4;
scanf("%f %f %f",&n, &n, &n);
score1 = (float)n*percent1;
score2 = (float)n*percent2;
score3 = (float)n*percent3;
amount = score1+score2+score3;
printf("%.2f\n", amount);
getchar();
return 0;
}
my input:
65 56 100
my output:
100.00
u can see it there, the output must be 92.00
is there any mistakes?
Help me pls, ty
You are using the same variable (n) for all three input values. You need a separate variable for each input value. Use the score variables to store the values and then use them in a single calculation.
#include <stdio.h>
#include <math.h>
int main()
{
int score1, score2, score3;
float amount;
/* Variable 'n' not required, since not used. */
float n;
float percent1 = 0.2;
float percent2 = 0.4;
float percent3 = 0.4;
scanf("%f %f %f",&score1, &score2, &score3);
amount = (score1 * percent1) + (score2 * percent2) + (score3 * percent3);
printf("%.2f\n", amount);
getchar();
return 0;
}

Resources