menu

Questions & Answers

find the sum of 2 numbers in c with input from user
#include<stdio.h>
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("%d",&sum);
return 0;
}

This is what i am trying but 23+23 is coming out to be 6422292 in this way.I cant find the error. Please help.

Comments:
2023-01-11 09:12:54
Rishi Gandhi, Why did you code and put a & in printf("%d",&sum);?
Answers(3) :

Hey actually the error is in the printf() function

The & is your telling to print the value stored in the sum variable

Make the following changes to your code

printf("%d", sum);

Hope you got fixed the error

Do NOT put an "address of" operator (&) on this line:

printf("%d",&sum);

It should be

printf("%d", sum);

#include<stdio.h>

int main() {

int a , b;

printf ("enter a\n");

scanf ("%d", &a);

printf ("enter b\n");

scanf ("%d", &b);

int sum = a + b;

printf ("the sum is : %d", sum);

return 0;

}