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 2 years ago.
Improve this question
How do I first convert numbers that I'm adding to scientific notation in C program . Then add them together, and once I have my final answer, then convert it back to scientific notation and return it?
What's wrong with:
#include <stdio.h>
int main(void)
{
double a, b, c;
printf("Enter two numbers: ");
if (scanf("%lf %lf", &a, &b) != 2)
{
fprintf(stderr, "Expected two floating point numbers\n");
return 1;
}
c = a + b;
printf("%13.6e + %13.6e = %13.6e\n", a, b, c);
return 0;
}
Example run:
Enter two numbers: 13.456e34 +11.44225534e33
1.345600e+35 + 1.144226e+34 = 1.460023e+35
Related
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 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.
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 4 years ago.
Improve this question
How can I call a function that has printf in it - in printf BUT without it printing the value entered twice?
in the code written below is how it is now, it prints the inserted value.
can I just use the function without printing the value again?
can I use printf(roll(num))
#include<stdio.h>
int roll(int);
int main()
{
int num;
printf("%d",roll(num));
return 0;
}
int roll(int a)
{
printf("Enter a number between 1-6:\n");
scanf("%d",&a);
while(a<1 || a>6)
{
printf("Wrong input! please enter a number between 1-6:\n");
scanf("%d",&a);
}
return a;
}
Just call the function without printf.
roll(num);
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 want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);
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 6 years ago.
Improve this question
is it possible to use same integers in multiple scanf's? For example, I input int i and j, then give them a value in scanf, and print their sum. Then use another scanf to assign different values to the same integers, and now add THEIR sum..
Do you mean this?
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
Of course it would work. The variable's value simply changes. Its the same if you wrote
int a;
a = 4;
. . .
a = 8
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 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;