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

}

2
answers
Last reply

More aboutwhat wrong code programming

Ijack

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

n%0

Lacee666

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