C - Nested For loop never stops [closed] - c

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 9 years ago.
Improve this question
I wrote a simple nested For loop, but for some reason it never stops.
#include <stdio.h>
int main (void) {
int x,y;
for (x=10;x<100;x+=10) {
for (y=10;y<100;x+=10)
printf("x is %d \n",x);
printf("y is %d \n",y);
}
return 0;
}
I'm new at c, but from examples I read, it should stop when x and y gets to 100. But for some reason it keeps on going for ever.

You need to increment y, not x
for (y=10;y<100;y+=10)
Also, it really looks like you meant to put braces around the inner loop
for (y=10;y<100;y+=10)
{ // <-- Did you mean to leave this out?
printf("x is %d \n",x);
printf("y is %d \n",y);
} // <-- and this?

Because y is not updated in the loop, which will make the second for loop always true (i.e. run forever).
You need to change
for (y=10;y<100;x+=10)
^
should be y here
to
for (y=10;y<100;y+=10)

Related

Using %c in scanf and assigning the value to an int variable in C? [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 2 years ago.
Improve this question
int main()
{
int i= 0;
printf("i value is %d\n",i);
scanf("%c", &i); // I am giving an input of 255 here
printf("i after scan %d\n",i); // This prints 50. How???
return 0;
}
Can someone explain how does the printf statement give 50? I have a little-endian machine.
Your program won't even compile as I is undeclared. I am assuming that it is a typo. Since you are scanning %c it will read only one character which is 2 from 255. Now 2 has ascii code of 50 which is being printed.

C language, my function is not returning a value, can not fix it [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 3 years ago.
Improve this question
my function:
int compare(int A, int B){
if(A>B){
return A
}
else {
return B
}
}
int main(void)
{
int A;
int B;
scanf("%d", &A);
scanf("%d", &B);
compare (A,B);
}
but it is not returning, if i write print instead of return however, it works
It is correctly returning[1]; you simply don't do anything with the returned value. For example, if you wanted to print the returned value to stdout, you could use the following:
printf("%d\n", compare(A,B));
If it wasn't for the missing includes, the stray semi-colon, the two missing semi-colons, and the missing right brace that prevent the code from even compiling.

Guessing Game - Can't figure out why it produces a large negative integer [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 4 years ago.
Improve this question
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment

My c program is not giving any result.pls help everyone [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
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

Loop iteration in c [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 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i,x=10;
for(i=0;i<7;i++);
{
x++;
}
printf("%d",x);
}
Output : 11
No matter how many times the for loop iterates, the value of x stays 11. Why is that ?
Remove the semicolon from here:
for(i=0;i<7;i++);
The semicolon makes the for loop have an empty body. It makes it equivalent to
for(i=0;i<7;i++){}
Including warning flags in your compiler(-Wextra in GCC) emits a warning about these kind of issues.
Semicolon (; ) punctuation mark in C means that block of code is finished. That means if you use
for(i=0;i<7;i++);
{
x++;
}
For loop ends before it reaches brackets. Then code between your brackets runs like normal lines out of loop. If you want your loop to include brackets, get rid of the semicolon, like:
for(i=0;i<7;i++)
{
x++;
}

Resources