Where do I wrong ? I don't get expected output - c

I try to write the code according to the problem. But I face problem with rounding issue. Can anyone explain me where I face the problem ?
M=12, T=20, X=8 tip=(20×12)/100=2.4 tax=(8×12)/100=0.96 final
price=12+2.4+0.96=15.36 Officially, the price of the meal is $15.36,
but rounded to the nearest
dollar (integer), the meal is $15.
Here's My Full Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t, x;
double m;
scanf("%lf", &m);
scanf("%d", &t);
scanf("%d", &x);
double tip, tax;
tip= m*t/100;
tax= t*x/100;
int total= (int)(round( tip + tax +m ));
printf("The final price of the meal is $%d.", total);
return 0;
}
When I take input 15.91,15,10 it shows output 19 instead of 20
Where have I made mistakes?

When you do
tax= t*x/100;
you are making an integer division, so it will be rounded.
You will have 15*10/100 = 1, instead of 1.5
Also, you have t*x instead of m*x, as you put on your question
M=12, T=20, X=8
tip=(20×12)/100=2.4
T M
tax=(8×12)/100=0.96
X M
Change it to
tip= m*t/100.0;
tax= m*x/100.0;
And you will get the expected 20 as the final result

Related

triangle area calculator, It keeps showing 0, why?

I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
#include <math.h>
float main ()
{
float a,b,c,pi ;
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c :");
scanf("%f",&c);
printf("%f\n",(a*(1/2)*b*(c*pi)/180));
return 0;
}
The sub-expression (1/2) is always equal to 0 due to the integer arithmetic. You need to write for example (1.0f/2).
Apart from this the variable pi is not initialized
float a,b,c,pi ;
Also according to the C Standard the function main without parameters shall be declared like
int main( void )

Program output is different from manual calculation why is this happening? C language [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
#include <stdio.h>
#define s scanf
#define p printf
void main (){
int P,I,A;
float T,R;
clrscr();
p("PRINCIPAL: ");
s("%i",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %i\nMA: %i",I,A);
getch();
}
This really bugs me, if i put PRINCIPAL: 1200 RATE: 3 TERM:1 I: 35 MA: 1235 but if you compute in manually the answer should be I: 36 MA: 1236 the number is decreasing by 1. Why is it happening? why does the answer differ from computer and manual computing?
You try to typecast float to int that causes some data loss. Just like we can not store the big object in the small bag.
#include <stdio.h>
#define s scanf
#define p printf
int main (){
int P;
float T,R,I,A;
p("PRINCIPAL: ");
s("%i",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("\nI: %f\nMA: %f",I,A);
return 0;
}
Your problem is with typecasting, please research on this subject a look at this, it's a little bit hude to explain in a simple text here.
But I can tell you, you are problably losing information when casting from float to int, because this two primitive types in C have diferent max length.
You can see changin the int variables to float and running your own program again, like this:
#include <stdio.h>
#define s scanf
#define p printf
void main (){
float P,I,A;
float T,R;
p("PRINCIPAL: ");
s("%f",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %f\nMA: %f",I,A);
getch();
}
This will produce your desired output, just in float.
Your problem is in the conversion of float to int. If you do your program with everything typed as float, you get the expected results:
#include <stdio.h>
#define s scanf
#define p printf
int main (){
float P,I,A;
float T,R;
p("PRINCIPAL: ");
s("%f",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %f\nMA: %f",I,A);
return 0;
}
outputs:
PRINCIPAL: 1200
RATE: 3
TERM: 1
I: 36.000000
MA: 1236.000000
However, when you convert your float values to int, you just take the integer part; everything to the right of the decimal point gets deleted. So, even though it's printing as 36.000000 when I do it, it's possible that the value of I may be coming out to something like 35.9999999, due to imprecision in floating-point math, and simply displaying as 36.000000 due to rounding in the display process. In this case, you'll just get the 35, and lose everything else.
To solve your problem, either leave everything as a float, or convert your floats to ints by rounding them—for example, by using lroundf in math.h—instead of just casting them.

Surface area of a hexagon

I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.
Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}

-1.#IND00 results from fourier series sum

I'm getting the "-1.#IND00" as a solution to this Fourier series.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n, x=50, L=100, q1=2;
float pi = 3.141592;
float flux1=0;
double flux2=0;
for(n=0;n<=50;n++)
{
flux1=q1*(2/(pi*n))*(cos(n*pi)-1)*(sin((n*pi*(x+L))/(2*L)));
flux2+=flux1;
}
flux2+=q1;
printf("%f\n", flux2);
return 0;
}
flux2 is coming out as "-1.#IND00" I can't work out why, since each term in the sum, (flux1), comes out as a rational number and the sum seems to converge.
Can you guys help?
This computation is in error on the first iteration when n == 0
2.0/(pi*n)
It's a "divide by zero" error.
On subsequent iterations, the computation is good, but the sum flux2 has already been corrupted by the bad value of the first flux1 and never recovers.

power int number while in loop

OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.

Resources