C program Always print 0.000000 - c

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;
}

Related

float number is not displaying correctly in my C program

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));

How to terminate a program using while loop [duplicate]

This question already has answers here:
Why do interleaved scanf() + printf() statements result in both scanf() calls executing first, then both printf() calls?
(2 answers)
Closed 5 years ago.
I have made this calculator.
Everything works fine.
However, I want to use a while loop as follows:
char cont = 'y'
while (cont == 'y') {
/code/
}
printf("Do you want to continue (y/n)")
scanf("%c", & cont)
This prints:
Do you want to continue (y/n)
But when I enter something the program ends unexpectedly.
Full Code:
#include < stdio.h >
#include < conio.h >
void main() {
float x, y, result;
int select;
char cont = 'y';
clrscr();
while (cont == 'y') {
clrscr();
printf(
"Please Enter The Respective Number For Following Operation\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n"
);
scanf("%d", & select);
clrscr();
switch (select) {
case 1:
{
printf("\nEnter The First Number To Add\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Add\n");
scanf("%f", & y);
clrscr();
result = x + y;
printf("Addition of two numbers %f and %f is %f", x, y,
result);
break;
}
case 2:
{
printf("\nEnter The First Number To Subtract\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Subtract\n");
scanf("%f", & y);
clrscr();
result = x - y;
printf("Subtraction of two numbers %f and %f is %f", x, y,
result);
break;
}
case 3:
{
printf("\nEnter The First Number To Multiply\n");
scanf("%f", & x);
printf("\nEnter The Second Number To Multiply\n");
scanf("%f", & y);
clrscr();
result = x * y;
printf("Multiplication of two numbers %f and %f is %f", x,
y, result);
break;
}
case 4:
{
printf("\nEnter The Numerator\n");
scanf("%f", & x);
printf("\nEnter The Denominator\n");
scanf("%f", & y);
clrscr();
result = x / y;
printf("\nDivision of two numbers %f and %f is %f", x, y,
result);
break;
}
default:
{
printf("Invalid Choice");
break;
}
}
printf("\n\nCalculator By XXX\n\nDo you want to Continue (y/n)\n");
scanf("%c", & cont);
}
getch();
}
Your while loop terminates because the scanf("%c",&cont); reads the leftover \n from the buffer which makes your while statement to fail.
You should modify your scanf to scanf(" %c",&cont);.
Put that scanf statement inside while loop or you can use do..while loop in place of while.
do {
// code
printf("Do you want to continue (y/n)"); scanf("%c",&cont);
} while(cout=="y");

Program is not responding in Code::blocks after reading my input

I am trying to make a program within code::blocks that will allow me to select multiple unit conversions to do. However, whenever I build and compile and reach the point in the program where it scans in my input for the variable "choice", code::blocks displays a window immediately afterwards that says that my .exe has stopped working and I can't figure out why this is the case. I am using the GNU GCC Compiler. Any help would be enormously appreciated.
#include <stdio.h>
int main()
{
float fahrenheit;
float celsius;
float pound;
float kilogram;
float inch;
float centimeter;
float mph;
float kph;
int foc;
int pok;
int ioc;
int mok;
int choice;
printf("\n1. Temperature Conversion\n");
printf("\n2. Weight Conversion\n");
printf("\n3. Length Conversion\n");
printf("\n4. Speed Conversion\n");
printf("\n5. Exit Program\n");
printf("\n");
printf("\nEnter the number of the program you would like to run :\n");
printf("\n");
scanf("%d", choice);
//Temperature Conversion
if(choice == 1) {
printf("\n1. Convert from Celsius to Fahrenheit\n");
printf("\n2. Convert from Fahrenheit to Celsius\n");
printf("\nEnter the number that corresponds with the conversion you would like to do:\n");
printf("\n");
scanf("%d", &foc);
if(foc == 1) {
//option 1
printf("\nEnter your temperature in Celsius : ");
scanf("%f", &celsius);
fahrenheit = 32 + (celsius * 1.8);
printf("\nYour temperature in Fahrenheit : %f ", fahrenheit);
}
else {
//option 2
printf("\nEnter your temperature in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 0.55555555;
printf("\nYour temperature in Celsius : %f ", celsius);
}
}
//Weight Conversion
else if(choice == 2) {
printf("\n1. Convert from Pound to Kilogram ");
printf("\n2. Convert from Kilogram to Pound ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &pok);
if(pok == 1) {
//option 1
printf("\nEnter your weight in pounds : ");
scanf("%f", &pound);
kilogram = (2.20462 * pound);
printf("\nYour weight in kilograms : %f ", kilogram);
}
else {
//option 2
printf("\nEnter your weight in kilograms : ");
scanf("%f", &kilogram);
pound = (kilogram/2.20462);
printf("\nYour weight in pounds : %f ", celsius);
}
}
//Length Conversion
else if(choice == 3) {
printf("\n1. Convert from inches to centimeters ");
printf("\n2. Convert from centimeters to inches ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &ioc);
if(ioc == 1) {
//option 1
printf("\nEnter your length in inches : ");
scanf("%f", &inch);
centimeter = (inch/2.54);
printf("\nYour length in centimeters : %f ", centimeter);
}
else {
//option 2
printf("\nEnter your length in centimeters : ");
scanf("%f", &centimeter);
inch = (centimeter*2.54);
printf("\nYour length in inches : %f ", inch);
}
}
//Speed Conversion
else if(choice == 4) {
printf("\n1. Convert from mph to kph ");
printf("\n2. Convert from kph to mph ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &mok);
if(mok == 1) {
//option 1
printf("\nEnter your speed in mph : ");
scanf("%f", &mph);
kph = (mph/1.60934);
printf("\nYour speed in kilometers: %f ", kph);
}
else {
//option 2
printf("\nEnter your speed in kph : ");
scanf("%f", &kph);
mph = (1.60934*kph);
printf("\nYour length in inches : %f ", mph);
}
}
else if(choice == 5) {
printf("\nProgram has ended. Thanks for your time!");
}
else {
printf("\nThat is not a valid program number. ");
}
}
You have to pass the pointer to the choice variable (i.e. &choice) to your scanf() call, just as you've done for your other scanf() uses.
I.e. instead of
scanf("%d", choice);
use
scanf("%d", &choice);
scanf() reads a value from the user and the address of the variable must be represented in the syntax
if not represented the program takes the garbage value so

How to solve Error : else without a previous if [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
I've been trying to solve this error for half an hour now.
How do I get this code to work?
Here's the input, please point out my mistake(s).
#include <stdio.h>
#include <math.h>
void main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
else if(choice==2)
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
else if(choice==3)
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
else
printf("You've entered an invalid choice...");
getch();
}
Use braces if a if/else block has multiple lines:
if (choice==1) {
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
} else if (choice==2) {
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
} else if (choice==3) {
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
} else printf("You've entered an invalid choice...");
Without braces your code is interpreted like
// Standalone if block
if (choice==1)
printf("Enter value of radius (cm) : ");
// End of the if block
// ...
// else without previous if (syntax error)
else if (choice==2)
printf("Enter value of voltage (volts) : ");
Use if..else if..else block properly with braces.
If you are using GCC compiler then use following code and compile code with math library.
For example (correct way): gcc test.c -lm
otherwise it gives error : undefined reference to 'pow' (math library function)
#include <stdio.h> //Standard IO functions
#include <math.h> //math functions
int main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
{
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
}
else if(choice==2)
{
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
}
else if(choice==3)
{
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
}
else
printf("You've entered an invalid choice...");
return 0;
}
missing brackets in if else command
#include <stdio.h>
void main()
{
const float pi = 3.142;
int choice;
float radius, volume, volts, ohms, watts, mass, accel, force;
printf("\n1. Calculate the volume of a sphere");
printf("\n2. Calculate the power of a circuit");
printf("\n3. Calculate the force of an object");
printf("\n-----------------------------------\n");
printf("What is your choice? : ");
scanf("%d", &choice);
if(choice==1)
{
printf("Enter value of radius (cm) : ");
scanf("%f", &radius);
volume = 4/3 * pi * pow(radius,3);
printf("Volume of a sphere is %.2f", volume);
}
else if(choice==2)
{
printf("Enter value of voltage (volts) : ");
scanf("%f", &volts);
printf("Enter value of resistance (ohms) : ");
scanf("%f", &ohms);
watts = pow(volts,2) / ohms;
printf("Power of circuit is : %.2f watts", watts);
}
else if(choice==3)
{
printf("Enter mass of object (kg) : ");
scanf("%f", &mass);
printf("Enter acceleration (meters per second squared) : ");
scanf("%f", &accel);
force = mass * accel;
printf("The force of the object is : %.2f Neutons", force);
}
else
printf("You've entered an invalid choice...");
getch();
}
You are missing the curly braces {}.
Do: if() { ... } else if() { ...}. You should take a look at: if statement without brackets

What am I doing wrong with this C program?

I have the following code, and the only thing I can physically see it evaluating is line 18 which is the call to printf(). It doesn't go any further.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int cylNum;
double disp, pi, stroke, radius;
pi = 3.14159;
printf("Welcome to the Engine Displacement Calculator!\n");
cylNum = scanf("Enter number of cylinders (then press enter): \n");
stroke = scanf("Enter stroke: \n");
radius = scanf("Enter radius: \n");
disp = radius * radius * pi * stroke * cylNum;
printf("Displacement is: %f", disp);
getchar();
printf("Press any key to exit!");
return 0;
}
the variables you're trying to read should be parameters to "scanf()", not the result of scanf():
printf("Enter number of cylinders (then press enter): ");
scanf("%d", &cylNum);
...
The scanf function is to read in values.
So the line
cylNum = scanf("Enter number of cylinders (then press enter): \n");
should be the following lines
printf("Enter number of cylinders (then press enter): \n");
scanf("%d", &cylNum);
You need to check the return value of scanf to make sure that it is 1, i.e. conversion has taken place.
So perhaps the code should read
do {
printf("Enter number of cylinders (then press enter): \n");
} while (scanf("%d", &cylNum) != 1);
For the variables disp, pi, stroke, radius you need to use "%lf" in the scanf function instead of "%d.
See scanf and printf
"scanf" doesn't take parameter like the way you are trying.
printf("Enter number of cylinders (then press enter): \n");
scanf(" %d", &cylNum);
printf("Enter stroke: \n");
scanf(" %lf", &stroke);
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int cylNum;
float disp, pi, stroke, radius;
pi = 3.14159;
printf("Welcome to the Engine Displacement Calculator!\n\n");
printf("Enter number of cylinders (then press enter): ");
scanf("%d", &cylNum);
printf("Enter stroke: ");
scanf("%f", &stroke);
printf("Enter radius: ");
scanf("%f", &radius);
disp = radius * radius * pi * stroke * cylNum;
printf("Displacement is: %f\n\n", disp);
return 0;
}

Resources