Getting Error while calculation of two float numbers [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I ran into a problem while calculating density of a box.
The output always comes out to be 0.00.
I tried type-casting but nothing worked.
Please help, I will be very grateful.
Here is the code...
#include <stdio.h>
void main()
{
int l, b, h;
float wt, vol;
float den = 0.0;
// Taking Length
printf("Enter Length ");
scanf_s("%d", &l);
//Taking Breadth
printf("\nEnter Breadth ");
scanf_s("%d", &b);
//Takhing Height
printf("\nEnter Heigh ");
scanf_s("%d", &h);
//Calculating Volume of the Box
vol = (l * b * h);
//Printing Volume
printf("\nVolume Of The Box Is %.2f\n\n", vol);
//Now taking mass of the box
printf("Enter Weight ");
scanf_s("%f", &wt);
//Calculating Density
den = wt /(float) vol;
//Printing Density
printf("\nDensity of the Box Is %.2f\n\n", &den);
}
The output of the program is as follows :
Enter Length 10
Enter Breadth 10
Enter Height 10
Volume Of The Box Is 1000.00
Enter Weight 5000
Density of the Box Is 0.00
Press any key to continue. . .
Note
Density = Mass(Weight)/Volume
Thanks :)

At least: wrong format specifier.
// double address
printf("\nDensity of the Box Is %.2f\n\n", &den);
Save time, enable all warnings. A good well enabled compiler will warn about this mismatch.

Recall &v prints the address of the variable v in memory, we use this in scanf (if you're a beginner), so change your code to:
printf("\nDensity of the Box Is %.2f\n\n", den);

Related

Calling printf() after scanf() returns nothing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to learn C and I've started by building a simple change calculator. At the moment, I'm just trying to take the user input 8.68 and output it, output the loonies and output the quarters. However, the last 3 printf() calls return nothing:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void){
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", value);
printf("%lf", value);
printf("%d", loonies);
printf("%lf", remainder);
}
This is the output. The prompt asks for the user input and then the program ends:
Please enter the amount to be paid: $8.68
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
You are missing an '&'. Scanf requires you to pass pointers to the variables you want to read into.
scanf("%lf", &value);
The scanf expects the address of the variable that you are passing. Here in your case you have to give &value.
The full code I am pasting here
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", &value);
printf("Values- %0.2lf\n", value); //'\n' and 'Values-' added
printf("Loonies - %d\n", loonies); //'\n' and 'Loonies-' added
printf("remainder - %0.2lf\n", remainder); //'\n' and 'remainder-' added
}
Output I am getting is
Please enter the amount to be paid: $8.68
Values- 8.68
Loonies - 0
remainder - 0.00
Edit: Here I am giving %0.2lf for printing only two decimal characters
You are missing a & in your scanf.

Why can't I get the program to answer anything but 0.00? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes

Having some trouble with float in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm having some trouble with float because whenever I enter a decimal number, the program just ends. When I don't and enter an integer, it works perfectly fine (My compiler is codeblocks)
Here's the code
#include <stdio.h>
int main()
{
float kmm; /* Monthly traveled Kilometers */
float costo; /* Cost for a liter of gas*/
float kmp; /* Traveled Kilometers with a liter of gas*/
float ln; /* Needed liters monthly */
float costot; /* Monthly total cost*/
float costom; /*Average daily cost*/
printf("Enter traveled monthly Kms\n");
scanf("%f", &kmm);
printf("Enter cost for a liter of gas\n");
scanf("%f", &costo);
printf("Enter traveled Kms with a single liter\n");
scanf("%f", &kmp);
ln=kmm/kmp; /* Needed liters monthly */
costot=ln*costo;
printf("La spesa mensile totale è %.3f\n", costot);
costom=costot/30;
printf("Il costo medio giornaliero è %.3f\n", costom);
return 0;
}
Can you explain what's going wrong?
UPDATE: I edited in English.

The program show no errors, but it is not compiling. Can you please help me on what i need to do [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The program shows no errors, but it is not compiling. Can you please help me with what I need to do to solve this problem.
int main(void) {
printf("\n*************************************************************\n");
printf("\n*************************************************************\n");
printf("\n******** Program find the solution to which car you *********\n");
printf("\n******* could still earn if you received A's for the ********\n");
printf("\n********* remainder of your undergraduate classes. **********\n");
printf("\n*************************************************************\n");
printf("\n*************************************************************\n");
printf("\n You will be asked to enter your current GPA with at most 3 \n");
printf("\n Decimal places and the number of credit hours that the GPA \n");
printf("\n Was based on, which should be a positive number. \n");
printf("\n And the honors you qualify for will be displayed to screen.\n");
printf("\n*************************************************************\n");
printf("\n*************************************************************\n");
printf("\nPlease enter your current GPA with at most 3 decimal places.\n");
printf("\nFor example, for a GPA of 2.3333333333, enter 2.333\n");
printf("\nThis GPA should fall between 0 and 4.0 --> ");
scanf("%d, &numb1\n");
printf("\n*************************************************************\n");
printf("\nPlease enter the number of credit hours that this GPA was based");
printf("\n on. This should be a positive integer\n");
printf("\nFor example, for twelve credit hours, simple enter 12.\n");
printf("\n-->");
scanf("%d, &numb2");
printf("***************************************************************\n");
}
You are passing a single argument to scanf because of where you put the double quote.
It should be: scanf("%f", &numb1); and scanf("%d", &numb2);
Note that numb1 is %f since it's floating point and numb2 is %d for integer.
Additionally, you never declared variables numb1 or numb2 so you also need: float numb1; int numb2; at the top of the function.

i get Error while trying to compile and Run, [Linker error] undefined reference to `scand' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So i am making a Program using Dev C++ and here is the Program works:
Allow user to enter a weight in pounds then your program will convert the value into kilograms and grams.
Allow a user to enter a height in centimeters then your program will convert it to meters, feet and inches.
and my code in Dev C++
#include <stdio.h>
#define CENTIMETER 0.01
#define POUNDS 0.453592
int main (void)
{
float Pounds;
float Kilograms;
float Grams;
float Centimeters;
float Meters;
float Feet;
float Inches;
printf("Please Enter your weight in Pounds : ");
scanf("%f",&Pounds);
Kilograms=Pounds*POUNDS;
Grams=Kilograms*1000;
printf(" Weight in kilograms is %.2f", Kilograms);
printf(" Weight in grams is %.2f", Grams);
printf("\n\n\n\n\n\nAnd ");
printf("Please Enter your height in Centimeters: ");
scand("%f",&Centimeters);
Meters=Centimeters*CENTIMETER;
Feet=Meters*3.28084;
Inches=Feet*12;
printf(" Height in Meters is %.2f", Meters);
printf(" Height in Feet is %.2f", Feet);
printf(" Height in Inches is %.2f", Inches);
getch();
return 0;
}
and the problem is i can't save it and it said:
"[Linker error] undefined reference to `scand' " ,
" ld returned 1 exit status"
am new into programming, so i hope someone can help me..
The code contains a typo. In this statement
scand("%f",&Centimeters);
there shall be scanf instead of scand.
scanf("%f",&Centimeters);

Resources