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 1 year ago.
Improve this question
I just started C programming , My code doesn't work properly . can you help me?
This is my code :
#include <stdio.h>
int main(){
int n,sum=0,a;
scanf("%d", &n);
while (n!=0)
{ a=n%10;
sum=sum+a;
n=n/10;
}
printf("%d",&sum);
return 0;
}
You should remove "&" from your printf statement. In C, using & before a variable name means you are referencing that variable's address location. When printing, placing %d indicates the variable you will pass into the print statement will be a number in decimal format, and the return type of &sum does not match this.
If you replace &sum with sum, you will be properly referencing the value of sum instead of its address, which matches the expected type for %d. Replacing your printf statement will give you this code:
#include <stdio.h>
int main(){
int n,sum=0,a;
scanf("%d", &n);
while (n!=0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf("Sum of digits: %d", sum);
return 0;
}
You are not supposed to use & before sum in your printf. We use & in scanf in order to change change a variable. But by using printf, you are just trying to show the variable's value, not its address.
So all you need to do is to omit the & before sum in you printf.
This is how the last part of your code should look like:
printf("%d", sum);
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
Why is the variable input2 assigned -1 and why does it give me a memory value when I run the code?
#include <stdio.h>
int main(void)
{
int input1;
int input2 = -1;
while(input2 != 0)
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
}
if(input1==99)
puts("Finish.");
return 0;
}
You have a while loop that continues as long as input2 is not equal to 0, so in order to enter it the variable needs to be initialized with some arbitrary value that isn't 0. Functionally, you could have used 1, -123, 999 or any other non zero value and get the same effect.
Arbitrary value with no reason. If uninitialized, it will cause undetermined
behavior whether the while loop is taken or not (depending on what garbage is inside input2). The writer of this code wanted the while loop to enter. Could have rewritten it
int input1;
int input2;
do
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
} while(input2 != 0);
#Jonas Rye Nielsen this program is just a simple calculator to add 2 Integer number and that's it.
Now in order to quit and program, what it does is to check the second Input with some logic behind it.
If you enter a 0 to add 2 integer (or anyway in any kind of addition really) add a 0 would just make no sense at all, hence the progam is implicitly assuming that you want to exit it!.
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 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 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 7 years ago.
Improve this question
Is there any possibilities to read and add two or three different integers by using single variable ( int a ) in C language?
I didn't want to use array
I'm not sure I'm getting you, but for example, if you want to add 2 16 bits integers with a single 32bit integer, you could do
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main()
{
uint32_t a;
printf("Enter number 1: ");
scanf("%hd", (uint16_t *)(&a));
printf("Enter number 2: ");
scanf("%hd", ((uint16_t *)(&a))+1);
printf("%X\n", a);
printf("Sum = %"PRIu32"\n", (uint32_t)(*(uint16_t *)(&a)) + *(((uint16_t *)(&a)) + 1));
return 0;
}
The logic is to think about variable equals to arrays of bytes, and that's it.
This implementation still have problems that are well explained HERE
No. Every time you you atribute a new value to the same variable it replaces the old one. If you don't want to use an array and it's a simple code to add numbers, just declare three variables and atribute each value to one of them.
I do not know if you would like this, but another way you can do this will be to accept inputs like you punch in calculators, and parse to int before applying the operations on them.
Something like this
#include <stdio.h>
#include <string.h>
int main ()
{
char buffer[256];
char * pch;
printf("input your numbers in this format ${number1}+${number2}...: ");
fgets (buffer, 256, stdin);
int sum = 0;
pch = strtok (buffer, "+");
while (pch != NULL)
{
sum += atoi (pch);
pch = strtok (NULL, "+");
}
printf("the sum is %\n", sum);
return 0;
}
so, run it and input 2+2+2 and it does the calculation for you. thanks