I have input the values of d = 10 s = 5 and h = 4 and I got a different output than expected.
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",& d,&s,&h);
double E= sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T=sqrt((2*s)/d*h);
printf ("T=%lf\n",T);
return 0;
}
Expected output
E = 5
T = 0.5
What I get
E = 5
T = 2
double T=sqrt((2*s)/d*h);
'/' and '*' has same precedence so the order of evaluation is left to
right.
so first (2xs) = 10 next 10/10 = 1; at last 1x2 = 2;
Hence T is 2.
For more info - https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/#:~:text=Operator%20precedence%20determines%20which%20operator,one%20operators%20with%20different%20precedence.&text=Operators%20Associativity%20is%20used%20when,Right%20or%20Right%20to%20Left.
Try this one I am getting E=5 and T=0.5
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",&d, &s, &h);
double E = sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T = sqrt((2*s)/(d*h));
printf ("T=%lf\n",T);
}
This is the output I got
E=5.000000
T=0.500000
Related
Example
Input: 12345
Output: (1+2+3+4+5=15)--> (1+5=6) Output is 6
(It shoud be only one number (1-9)
Please tell me how to make sure that when you enter a number, for example 12345, the output is equal to the sum 1 + 2 + 3 + 4 + 5 = 15 and then 1 + 5 = 6. C language. Thank you very much for your answer!
#include <stdio.h>
int main(){
int isicc;
scanf ("%d", &isicc);
while (isicc>0){
int d = isicc%10;
isicc=isicc /10;
}
printf ("Your number ", d);
}
After the command
scanf ("%d", &isicc);
You can use this code to get the sum of digits of the number:
int sum_digits = 0;
while (isicc > 0) {
sum_digits += isicc % 10;
isicc = isicc / 10;
}
isicc = sum_digits;
Now, you just need to repeat this process in another loop , that will continue as long as isicc is bigger than 9.
#include <stlib.h>
#include <stdio.h>
int main(){
int isicc;
scanf ("%d", &isicc);
isicc = abs(isicc);
printf ("Your number %d\n", isicc?isicc%9?isicc%9:9:0 );
}
Try this program, it will add digits and print sum
#include <stdio.h>
int main ()
{
int num,n,sum=0;
printf ("Enter number:");
scanf (" %d",&num);
while (num!=0){
n=num%10;
sum += n;
num = num/10;
}
printf ("%d",sum);
return 0;
}
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;
}
While printing a number, I am trying to print its sign before the number. Is there a way to do it without the actually if...else case mentioned in the comment section of the code below.
I have tried getting the sign of the number. But I don't know how to print just the sign.
#include<stdio.h>
#include<complex.h>
void main(){
double complex s = 3.14 + 5.14*I;
printf("\ns is: %f + %f i", creal(s), cimag(s));
double complex S = conj(s);
printf("\nConjugate of s is: %f + %f i", creal(S), cimag(S));
}
/*
printf("\nConjugate of s is: %f ", creal(S))
if cimag(S) > 0
printf("+ %f i", cimag(S))
else
printf("- %f i", abs(cimag(S)))
*/
If S = 3.14 - 5.14*I, without the if...else condition, I'm expecting to get an output something like this:
3.14 - 5.14 i
You can just use the printf sign flag. +
#include <stdio.h>
int main()
{
float f = 1.0;
printf("%f%+f",f,f);
return 0;
}
Output
1.000000+1.000000
Change to -1:
-1.000000-1.000000
If you really need the spaces, you're going to have to do something like you described:
#include <stdio.h>
#include <math.h>
#include <complex.h>
void complexToString(double complex num, char * buffer){
double imag = cimag(num);
char sign = (imag<0) ? '-':'+';
sprintf(buffer,"%f %c %f i",creal(num),sign,fabs(imag));
}
int main()
{
double complex s = 3.14 + 5.14*I;
char buffer[50];
complexToString(s,buffer);
printf("%s",buffer);
return 0;
}
output:
3.140000 + 5.142000 i
First get the sign character:
double x = ...;
char c = signbit(x) ? '-' : '+';
Then use it however you want:
printf ("%c %f", c, fabs(x));
With the help of answers from #Antoine and #yhyrcanus, the simplest way to code for space is:
double complex s = 3.14 - 5.14*I;
printf("\ns is: %f %c %f i", creal(s), signbit(cimag(s)) ? '-' : '+',cabs(cimag(s)));
I've created a program which takes an integer x input, then loops until x is met while also taking other integer inputs. I then do various calculations, and then find a square root of a certain value. When I divide by square root however I get a 0 when I know I should be getting a different value as the maths doesn't add up.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void) {
int multiply1, multiply2, add, squareRoot;
int i;
int n;
int x;
int s;
double divide, test = 0;
scanf("%d", &x);
for (s = 0; s < x; s++) {
scanf("%d %d", &i ,&n);
}
multiply1 = i * i;
multiply2 = n * n;
add = multiply1 + multiply2;
squareRoot = sqrt(add);
printf("%d", i);
test = (i / squareRoot);
printf("Multiplication = %d\n", multiply1);
printf("Multiplication = %d\n", multiply2);
printf("Added together = %d\n", add);
printf("square root = %d\n", squareRoot);
printf("First output = %.3f\n", test);
return 0;
}
You are dividing two integers so the actual division returns the result rounded down. You should instead cast to double and then divide.
test = ((double)i/squareRoot);
There are two things you can do,
Without changing your program, simply cast the i and squareRoot variables to double
test = (double) i / (double) squareRoot;
Change your program and make i and squareRoot a double.
I, would choose 2 because sqrt() returns a double and that might cause an integer overflow.
I'm trying to calculate the distance between 2 coordinates in 3D, however, when complied, and entered (1,1,1), (1,1,1), output returns 2 instead of 0.
Code here
#include <stdio.h>
#include <math.h>
int get_dist(int x_i,int y_i,int z_i,int x_o,int y_o,int z_o){
int coord_dist = 0;
coord_dist = sqrt(((x_o - x_i)^2) + ((y_o - y_i)^2) + ((z_o - z_i)^2));
return coord_dist;
}
int main(void){
int x0,y0,z0,x1,y1,z1;
int dist0_1 = 0;
printf("coord 0:");
scanf("%d %d %d", &x0, &y0, &z0);
printf("coord 1:");
scanf("%d %d %d", &x1, &y1, &z1);
dist0_1 = get_dist(x0,y0,z0,x1,y1,z1);
printf("%d\n", dist0_1);
return 0;
}
^ is XOR, not exponent. You want the pow() function.