Why this programm doesn't work?

+1 vote
asked Nov 13, 2019 by Anton
#include <stdio.h>
#include <math.h>
int
main ()
{
  
int n, i, c,s, b,a,r=0;
scanf ("%d %d", &a, &n);
s=s+a;
for (i=0; i<=n; i++)
{
    b=10*r;
c=a*b;
s=s+c;
    r++ ;
}
printf ("%d", s);
return 0;
}

14 Answers

–1 vote
answered Nov 14, 2019 by (1,160 points)
it's working you need to enter the value just only i checked
–1 vote
answered Nov 14, 2019 by L

you need to initialize variable s and b, because they get garbage when you declare them

Local Variables

VariableValue
n0
i0
c0
s-4912
b0
a4195504
r32767

you can check this by using the debugger

Local Variables

VariableValue
n0
i0
c0
s-4912
b0
a4195504
r32767
0 votes
answered Nov 15, 2019 by Artem

What do you mean by "doesn't work"? Actually it works from begin to end. But result is quite unpredictable because you use uninitialized variable s.

0 votes
answered Nov 15, 2019 by (330 points)
You use uninitialized variable s.
0 votes
answered Nov 16, 2019 by anonymous
The main issue is variable s is not intialised so it uses a garbage value that each time the code executes, a different number would be ouputted.

A good way to help understand code is set variable names to something meaningful e.g. counter for loop
0 votes
answered Nov 18, 2019 by atharv
you havent decleared the value of n by user or by yourself.
0 votes
answered Nov 18, 2019 by (550 points)
uninitialized variables
+1 vote
answered Nov 20, 2019 by anonymous
#include <stdio.h>
#include <conio.h>
int
main ()
{
  
int n=0,c=0,s=0,b=0,a=0,i;
scanf ("%d", &a);
printf("\n");
scanf("%d",&n);
s=a;

for(i=0; i<=n; i++)
{
    b=i*10;
c=a*b;
s=s+c;

}

printf("%d\n",s);

return 0;

}

Run this, it will work for sure!
0 votes
answered Dec 21, 2019 by (480 points)
because variable "s" declared and not initilized

and again it is got add with s

so it takes a garbage value and it doesnot work
0 votes
answered Jan 8 by (360 points)

in this programe no need to add #include<math.h>

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...