Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
#include<stdio.h>
int main()
{
float a=5,b=2;
int c,d;
c=a%b;
d=a/2;
printf("%d\n",d);
return 0;
}
The program is incorrect because the operator % is not defined for float numbers.
From the C Standard (6.5.5 Multiplicative operators)
2 Each of the operands shall have arithmetic type. The operands of
the % operator shall have integer type.
As for this statement
d=a/2;
then there is a conversion from the expression with the float type a /2 to the type of the left-hand side operand that has the type int. So the value of d will be equal to 2.
Pehaps you mean a program similar to the following program
#include <stdio.h>
int main(void)
{
int a = 5, b = 2;
int c, d;
c = a % b;
d = a / b;
printf( "d = %d, c = %d\n", d, c );
return 0;
}
In this case the program output is
d = 2, c = 1
Instead of the two variables c and d and two expressions with the operators / and % you could use the standard function div declared in the header <stdlib.h>. For example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 5, b = 2;
div_t result = div( a, b );
printf( "quotient = %d, remainder = %d\n", result.quot, result.rem );
return 0;
}
The program output is
quotient = 2, remainder = 1
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
#include <stdio.h>
int fun1(int a, int b) {
a = a - 3;
b = a / 2;
return b;
}
int main() {
int a = 10;
int b = 17;
int c = fun1(b, a);
int d = fun1(c, b);
printf("%d", d);
}
the code runs I am just trying to figure out what it prints out and why.
In this function
int fun1(int a, int b){
a= a-3;
b=a/2;
return b;
}
the value of the parameter b is not used. So in fact the function may be rewritten like
int fun1(int a, int b){
a = a-3;
return a / 2;
}
or
int fun1(int a, int b){
return ( a - 3 ) / 2;
}
(though the compiler can issue a message that the parameter b is not used,)
In the first call
int c=fun1(b,a);
the parameter a of the function is initialized by the argument b that has the value 17
int b = 17;
So the return value of the function is calculated like ( 17 - 3 ) / 2 and is equal to 7.
In the second call of the function
int d=fun1(c,b);
the function parameter a is initialized by the argument c that has the value 7 due to the previous call of the function.
So the returned value of the function is calculated like ( 7 - 3 ) / 2 and is equal to 2. This value is assigned to the variable d that is outputted.
This question already has answers here:
How to write absolute value in c
(5 answers)
Closed 2 years ago.
Let a = 4 and b = 5
on adding a+b = 9
on subtracting a-b = -1
But I want the absolute value of the answer, i.e., |a-b|=4-5=1. All I am getting is a negative 1.
How can I do that?
#include <stdio.h>
void add(int*a,int*b){
int c = *a+*b;
printf("%d\n",c);
}
void subtract(int*a,int*b){
int c = *a-*b;
printf("%d\n",c);
}
int main() {
int a, b; //enter the value of a and b
scanf("%d %d", &a, &b);
add(&a,&b);
subtract(&a,&b);
return 0;
}
simply put a condition using ternary operator.
replace assigning to c as following.
int c = *a > *b ? *a - *b : *b - *a;
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 2 years ago.
If i enter a value lets say "3" i get a result of 0. Please explain why and if there is a way please show me. Should I use float?
#include <stdio.h>
int getResult (int nVal)
int c , f;
int nExp;
double dSum;
f = 1;
dSum = 0;
nExp = nVal;
for (c = 1; c <= nVal; c++)
{
f = f * c;
dSum += f / nExp;
nExp = nExp * nVal;
}
return dSum;
int main ()
{
int nVal, nCompute;
double dSum;
printf("Enter Number to be Computed");
scanf("%d",&nVal);
nCompute = getResult (nVal);
printf("%d", nCompute);
return 0;
}
f and nExp are ints, so you're dividing them using integer division, keeping only the "whole" part of the result, to the left of the decimal point, which is zero. Instead, you define one or both of them as doubles, so you'd be performing floating-point division.
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 3 years ago.
Improve this question
I have the following equation I need to represent in C:
20 * 2^((1-x)/5)
my c representation is as follows, but it seems like the pow function is always returning a high integer value (1073741824) with my n values ranging from 1-5.
double x = 20 * pow(2.0, (n/5.0));
I assume it is because both arguments are not double values, but I do not see why. Is there a different way to format this equation to get it to work?
I just compiled the example you give and it works
#include <stdio.h>
#include <math.h>
int main () {
for (int n = 1; n <= 5; ++n) {
double x = 20 * pow(2.0, ((1-n)/5.0));
printf("%lf ", x);
}
}
Output
20.000000 17.411011 15.157166 13.195079 11.486984
Make sure you use int n and not unsigned n. In case of unsigned you will get (1 - n) overflow and pow will return inf.
Your compiler is assuming pow() returns an int,
Remeber to #include <math.h> for the proper prototype
#include <math.h>
#include <stdio.h>
// ipow works as pow when the compiler assumes an int return value
int ipow(double base, double exp) {
double res = pow(base, exp);
return *(int*)((void*)&res);
}
int main(void) {
for (int n = 1; n < 6; n++) {
double x = 20 * pow(2.0, ((1-n)/5.0)); // with correct prototype
double y = 20 * ipow(2.0, ((1-n)/5.0)); // when compiler assumes int
printf("n=%d, x=%f, y=%f\n", n, x, y);
}
return 0;
}
See https://ideone.com/XvPWX6
Output:
n=1, x=20.000000, y=0.000000
n=2, x=17.411011, y=422418048.000000
n=3, x=15.157166, y=1240833840.000000
n=4, x=13.195079, y=-1971682192.000000
n=5, x=11.486984, y=-2036727536.000000
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I assume output to be 8 but the line s=a+b assign value 5 to s variable not sum of a+b. I know that I am using %d instead of %f.
#include <stdio.h>
void summ(int, int);
int main(){
int a = 3, b = 5;
summ(a, b);
return 0;
}
void summ(int a, int b){
float s;
s = a + b;
printf("%d", s);
}
You can't use any old format specifier in a printf(). It always has to match the type of the variable you are passing in. If you want to print a value as another type, you have to convert it first, then print the converted value. So either this has to be:
void summ(int a, int b)
{
float s;
s = a + b;
printf( "%f\n", s );
}
or
void summ(int a, int b)
{
int s;
s = a + b;
printf( "%d\n", s );
}
In the case of your sum function, you won't see a noticeable difference (except that %d does not print a ".0000" after the number, but you could have suppressed that by adding a length of 0 to the fraction of your %f by writing it as %.0f).
OTOH, if you have a division:
void divv(int a, int b)
{
float s;
s = a / b;
printf( "%f\n", s );
}
the CPU will perform a (often faster) integer division. That means that 3 / 5 (which would be 0.6) just gives you 0 because it didn't bother calculating the fraction. The compiler doesn't look at the type of variable an expression is assigned to, so it doesn't see that it could actually store the fraction. It just looks at the operands of the operator, which are both int, calculates an int, and expands it into a float. So if you wanted a precise result, you'd have to do:
void divv(int a, int b)
{
float s;
s = (float)a / (float)b;
printf( "%f\n", s );
}
because as soon as one of the arguments is a float, it will perform a floating point division and also calculate the fraction.
here is a possible version of the code that cleanly compiles and performs the desired functionality.
#include <stdio.h>
void summ(int, int);
int main( void )
{
int a = 3;
int b = 5;
summ(a, b);
return 0;
} // end function: main
void summ(int a, int b)
{
float s;
s = (float)(a + b);
//printf("%d", s);
printf( "%f\n", s );
} // end function: summ
The output from the above code is:
8.000000