Using same integers multiple times in scanf [closed] - c

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

Related

How can I fix this code and print the output [closed]

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);

no answer from simple subtraction code in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.

How do you get C to produce output from number inputted in code? [closed]

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
What code do you have to include to get C to produce an output from number inputted into the system.
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
return 0;
}
I want the programme to work out a mathematical equation to the number that the user enters, for example if I enter 5 I want it to work out the exponent 2 of 1 up to that integer input.
You can use scanf function to get input from user like this:
#include <stdio.h>
int main()
{
int z;
printf("Please enter a number");
scanf("%d", &z);
// do your calculations on z
printf("Result: %d", z);
return 0;
}
You can use the scanf function.
For a number (or in your case an integer specifically) the line is
scanf("%d", &z);
where the %d specifies that its reading in an integer and the &z is a pointer to your integer variable z.
Information here: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
I'm guessing you're new to C programming, so once you get the basics down I would suggest familiarizing yourself with pointers as they are a critical aspect of the language. Some useful resource here using google-fu : https://www.tutorialspoint.com/cprogramming/c_pointers.htm

Unable to understand the meaning of errors in C language using Turbo C. I've coded this code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is what I've coded - the exchange of 2 numbers in c language using turbo c. I've tried real hard to google the solution but the explanation is not understandable as I'm a mere beginner. I'm having my internals on 20th and this is what they might ask along with 40 other programs.....
You should write printf("please enter"); instead of printf{"please enter"};
The same with scanf.
When you see Statement missing ; error, you definitely have an language error.
Functions take arguments between () and {} are used for blocks of code. For example:
if (1) {
printf("Printing\n");
printf("Printing again\n");
}
Another thing is, that we assign value to the left side of =.
a = a-b
a = a+b
Your code should be:
#include<stdio.h>
void main() {
int a,b;
printf("Enter two (2) numbers, please\n");
scanf("%d%d", &a, &b);
a = a+b; //assign a to the sum of current a and b
b = a-b;
a = a-b;
printf("The numbers a, b after some calculations are: a=%d and b=%d\n", a, b);
}
Use parenthesis () instead of braces {} in your functions printf() , scanf().
printf("please enter 2 nos");
scanf("%d %d" &a,&b);
printf("our exchanged numbers are a = %d" and b=%d" a,b);
And you can't assign value to a expression, it will give lvalue error. So to remove this error change,
a-b = a change it to a=a-b and change all the expressions.

How to design looping number sequence(increment)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want make a program to print the number sequence from beginning to end with a negative increment specified.
Input format:
I Put a single line consisting of three integers: the beginning, the negative increment, and end sequence. (ex: scan 9 -2 3
Output format:
Output a sequence of numbers from start to finish is printed per line. End output with a newline character.(it will be:
9
7
5
3
Here is the solution:
#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}
#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}
int start,end,inc;
scanf("%d %d %d",&start,&inc,&end);
for(;start>=end;start-=inc)
printf("%d ",start);

Resources