C max of sums - need to modify it to stop adding the sums -
this program needs let users enter numbers , calculate sum of factors each entered number.
it needs until user enters number odd , dividable 5 (it not calculate sum of last number). , needs list number of sums calculated , biggest sum.
the problem i'm having program , instead of showing biggest sum (max) shows sums of sums.
suma sum variable
#include <stdio.h> #include <stdlib.h> main() { int x ,i ,suma=0 ,nr=0 ,max=0; while(1) { printf("introduceti un intreg:\n"); scanf("%d",&x); for(i=2;i<=x/2;i++) //the checks if factor of x, adds //sum of factors { if(x%i==0) suma = suma + i; } if (x%2!=0 && x%5==0) break; nr++; if (suma>=max) max=suma; } printf("numarul sumelor calculate este %d\n\n",nr); //the number of sums calculated printf("suma este : %d",max); //the maximum sum }
suma
should initialized before for
loop (so re-initialized each sum), not @ stop of program.
Comments
Post a Comment