Please tell me what's wrong with this code [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 2 years ago.
Improve this question
#include<stdio.h>
void main()
{
int i=1,s=0;
do{
s+=i;
}while(i<=10);
printf("sum is %d",s);
}
this code is not giving any output, please tell me what is wrong.

You have an infinite loop because the variable i that is used in the condition of the do-while loop is not being changed within the loop. It stays equal to 1 as it was initialized.
do{
s+=i;
}while(i<=10);
It seems you mean
do{
s += i++;
} while( i <= 10 );
or
do{
s += i;
} while( ++i <= 10 );
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Also it is desirable to flush the output buffer by including the new line character '\n' in the output stream like
printf( "sum is %d\n", s );

you didn't increment i;
do
{
s+=i;
i++;
}while(i<=10);

Related

The program is not terminating [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 8 months ago.
Improve this question
#include<stdio.h>
int reverse(int );
int main()`
{
int num,rem,r;
printf("Enter a number:");
scanf("%d",&num);
r=reverse(num);
r=reverse(num);
return 0;
}
int reverse(int num)
{
int rem,rev=0;
while(num>=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
return rev;
}
Its not showing any errors and after entering the number the program stay still, its neither showing any output nor terminating
You have not used printf for output.
r = reverse(num);
printf("%d",r);
return 0;
Use this.
And also while condition should be
while (num > 0)
There is no need to call reverse function twice as well.

why n mod 10 in loop doesn't show output [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 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );

Whats wrong with my code i cant use scanf in vscode [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 1 year ago.
Improve this question
#include <stdio.h>
int main()
{
int ab;
printf("Whats 5+5:");
int scanf("%d", int ab);
printf("\nbla,bla,blabla,bla,blabla,bla,bla");
if (ab == 10) {
printf("Correct anwser!!!");
}
else {
printf("Incorrect anwser!!!");
}
//it says that the scanf isnt compatible with the declaration and excpected type
//specifier
return 0;
}
int scanf("%d", int ab);
the parameters beyond the 'format string' has to be address of where the input data is to be stored. The function returns EOF or the number of successful input conversions. suggest:
if( 1 != scanf("%d", &ab) )
{
fprintf( stderr, "scanf failed\n" );
exit( EXIT_FAILURE );
}
It's because you forgot to put '&' int the scanf, which signals where your input goes. You don't have to state the 'int' part again. You just have to write:
scanf("%d",& ab);
That's it. It's just a simple distraction error.

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.

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.

Resources