printing float value 0.9 in C [duplicate] - c

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
Please do flog me for this really basic question, but I still can not get why this happen.
I read that for printing several value behind coma, I should use %.f in C.
So I have this problem, counting 90/100. I expect to print 0.9
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90/100;
printf("%.1f\n", c);
}
And it shows me 0.0 ..(err..) . tried to change it into (printf("%f\n",c)) return me with 0.00000.. (err..)
Can anyone help me with this? (sorry, really new in programming..)
Thank you

The problem is that you are doing integer division. 90/100 = 0 in integer terms.
If you want to get 0.9, do : 90.0/100.0

The problem is at
c = 90/100;
Although, it will be assigned to a double data type, but the computation itself is all integer and that's why the value is 0.0.
Try,
c = 90.0/100;

it is integer division, do:
c = 90.0/100;
c = (float)90/100;
you need to make atleast one operant a double to evaluate the whole equation as double

You are dividing two integers, thus the result is an integer too. Try the following
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90.0/100;
printf("%.1f\n", c);
}

Related

"warning: division by zero [-Wdiv-by-zero]" when not dividing by zero [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 3 years ago.
My code for the following simple coding exercise produces a "division by zero" warning, and I'm wondering why.
#include <stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
}
return 1;
}
temps.c: In function ‘main’:
temps.c:6:45: warning: division by zero [-Wdiv-by-zero]
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
I realised while writing this question that it's because I should have written 5.0/9.0, since C handles division with integers in a way that I didn't expect. Posting this anyway since I couldn't find this particular error linked to this particular problem on SO.

How to write (a^n-1) in c programming [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 4 years ago.
Improve this question
I want to write (a^n-1) in c programming. I wrote pow(a,n-1) and the output is
wrong. How can I solve this problem? Thanks.
Here is my code:
#include <stdio.h>
#include<math.h>
int main() {
float a, r,n;
float sum = 0;
a = 1.04*pow(a, n-1);
r = 1.02*pow(1.04,-1);
n = 2;
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
The correct output should be 286376 but the program showed 2903773
Every C program executes line by line. So, at the time when compiler came on
a = 1.04*pow(a, n-1);
this line, variable a and n was not assigned with any value, resulted in giving you a garbage value...
So, the problem is, you had not assigned values in variables, and still, you were using them.
You have to first assign values in variables before using them. Otherwise, they will pick any garbage value from memory (Any Random number).
Assign value in a and n and try again.
Edit: As chux's comment suggests, if your program is supposed to give 286376 as output, then value of a should be 1 and value of n should be 2.
So, your correct code would be this:
#include <stdio.h>
#include <math.h>
int main() {
float a=1, r, n=2;
float sum = 0;
a = 1.04*pow(a, n-1) ;
r = 1.02*pow(1.04,-1);
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
pow(a, n-1) translates into exp(log(a) * (n-1)) which isn't precisely the same.. You can try to round the output like this:
round(pow(a, n-1))

Wrong output of program [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ float t=1/2;
printf("\n%.4f",t);
return 0;
}
i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not.
cant seem to find the problem.
please help.
Thanks.
I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.
TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

C++ main function and program [duplicate]

This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 5 years ago.
I am trying to get this code to run, i have tried these 2 ways but to no avail. The aim is to receive an input using scanf and print out the statement using printf. Error message i get is the Compiler just hangs.
Also, i am not to use any math functions such as square root or power
Test Cases and Expected answers
Input1: 1.41421356237
Output1: Area of the circle is 3.141590
Input2: 5.65
Output2: Area of the circle is 50.143703
Trial 1
#include <stdio.h>
double function(double a){
double area;
area = 3.14159*((a/2)*(a/2)*2);
return area;
}
int main(void){
double a;
scanf("%f",a);
double result = function(a);
printf("Area of the circle is %f\n",result);
return 0;
}
Trial 2
#include <stdio.h>
int main(void){
scanf("%f",a);
area = 3.14159*((a/2)*(a/2)*2);
printf("Area of the circle is %f\n",area);
return 0;
}
would appreciate any help, not sure why the function is not working. Thank you for your time.
In your first trial you need to change your scanf("%f",a); line to:
scanf("%lf",&a);
because correct type specifier for double is %lf which stands for "long float". Also you need to send &a as argument because scanf expects an address and not a value. Same thing with second trial.

C uninitialized int has a value of 1 instead of 0 [duplicate]

This question already has answers here:
Initializing variables in C
(10 answers)
Closed 6 years ago.
#include <stdio.h>
#include <string.h>
#include "prac.h"
#define MYNAME "Butter"
int main() {
int numberOfKids;
int weight;
int shirt;
printf("If I eat a Watermelon I will weigh %d lbs \n", weight + numberOfKids+ shirt );
return 0;
}
I compiled and ran the program and the result was 1; although I expected it to be 0. When I checked the value of each variable individually, the weight variable's value was 1. Can someone explain why that specific variables result was not 0? I am new to C and want to experiment with the basics to get a deeper understanding of the nuances of C. Any help would be appreciated.
Variables inside a function in C are not guaranteed to be set to anything by default. In memory, whatever was last stored there (which might not be flushed/erased to be 0) will be what the int is initialized to.
This is answered in Initializing variables in C
EDIT: As chux has stated below, local static variables are initialized to 0 if they aren't given an initial value. Also covered Is un-initialized integer always default to 0 in c?

Resources