find the sum of 2 numbers in c with input from user - c

#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.

Do NOT put an "address of" operator (&) on this line:
printf("%d",&sum);
It should be
printf("%d", sum);

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

#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;
}

Related

My code which is calculating sum and average of two numbers returns this error "[Error] ld returned 1 exit status" upon execution and compilation

This is my code. I've read the output log but can't see where the exact error is; I will appreciate if someone can kindly point me to it. The IDE I'm using is Dev C++.
int main(){
int n, i, a, average, sum;
printf("enter the number of integers to be picked\n");
scanf("%d", n);
sum=0;
printf("Enter a number.");
scanf("%d", &a);
for (i=0; i<=n;i++){
printf("%d", i);
sum=sum + a;
}
average=sum/n;
printf("The average is %d", average);
return 0;
}
You're just missing the & (address-of operator) in line 4 (scanf("%d",&n);). Putting this operator will tell the compiler, the address of n, where the value of n will be kept.

How To show original value of a variable after variable has been changed

So I'm having a problem about showing what my original variable value is after changing it in the code.
#include <stdio.h>
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
while (n!=0){
n/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
Example input:123
Output of this code "your number 0 has 3 digits"
I want to know how to be able to refer the variable "n" in the printf to the original value of '123' so the output will be "your number 123 has 3 digits"
I would recommend that you use a separate variable to save your value or count with a different variable.
This code would look something like this:
#include <stdio.h>
int main()
{
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
int buffer = n
while (buffer!=0)
{
buffer/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
This way you save your variable in your code and you only used a buffer and not the actual value n.
You can keep a copy of the original variable and use that copy of the variable while printing.
You can do this:
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
printf("your number %d has ", n);
while (n!=0){
n/=10;
count++;
}
printf("%d digits", count);
return 0;
}
Of course, you may have to do some error checks..

for loop resulting in undefined behaviour c

#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}

C: Extra output in printf

I've just started to learn C programming last week and I've learnt about some basics about it. So now I'm trying to make a program which can add up two numbers and show the result.
Here's my code:
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:%d\n");
scanf ("%d",&a);
printf ("Insert the next number:%d\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
It can be compiled and run but the following result is shown.
[1]http://i.stack.imgur.com/4Xjdv.png
Can someone please help me to get rid of that 4200612, which is output at the first printf statement? Thanks for your help and sorry for my bad english.
There is no need of %d in first two printf statement.
printf("Insert a number: ");
scanf ("%d",&a);
printf ("Insert the next number: ");
scanf ("%d",&b);
Since there is no corresponding argument. It will print some random value.
Try getting rid of the extra %d's in your printfs.
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:\n");
scanf ("%d",&a);
printf ("Insert the next number:\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}

Point or "reset" variables in C

If I have some thing like:
printf("\nEnter 2 numbers: \n");
scanf(" %d %d", &a, &b);
add (a,b)
int a,b;
{
printf ("%d", a+b);
}
Then want to run the block again, but with new variables of "nothing" like when the first printf statement is entered. Any suggestions?
First of all avoid using K&R C syntax
/* Your function
add (a,b)
int a,b;
{
printf ("Sum = %d\n", a+b);
}
*/
/* Use following style*/
void add (int a,int b)
{
printf ("Sum = %d\n", a+b);
}
int main()
{
int i,a,b; // Declare variables
int n=5; // Call it say n=5 times
for(i=0;i<n;i++) //Use a for loop to iterate for n times
{
printf("\nEnter 2 numbers: \n");
if(scanf(" %d %d", &a, &b)==2) // with 2 new inputs
add(a,b); //Call your add function
}
}

Resources