Sign in with
Sign up | Sign in
Your question

What is wrong with my code? ( C programming)

Last response: in Applications
Share
January 27, 2013 8:15:52 AM

I want to create an arithmetic mean program. For e.g. n=6, (1+2+3+6)/4=3. n is the number which is given by the user. b is calculating the numbers, if n%a=0 (in the example 1,2,3,6). d is calculating the numbers (in the example the 4). In the end x=b/d and we will have the answer. But when I enter the n in the scanf, the program crashes. What is the problem? Sorry for the bad english, if it is bad.


#include <stdio.h>
#include <stdlib.h>



int main(int argc, char *argv[])
{
int n,a,b=0,d=0,x;
scanf("%d", &n);
for(a=0;a<=n;a++){

if(n%a==0){

b=b+a;
d++;
}

}


x=b/d;
printf("%d", x);
system("PAUSE");


}

More about : wrong code programming

a b L Programming
January 27, 2013 8:34:53 AM

Ask yourself what result would you expect from trying to evaluate the expression:

n%0
January 27, 2013 8:49:12 AM

Oh, I'm stupid! Thanks! It's working!:)  I rewrote the a to 1.
!