Sum of factors in a loop

I need help creating a sum of factors in my loop

Page 1 of 1

2 Replies - 9014 Views - Last Post: 13 February 2008 - 10:03 PM Rate Topic: -----

#1 salyard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 08

Sum of factors in a loop

Posted 13 February 2008 - 09:57 AM

I have a code where I am making factors of a number but I am unable to figure out how to put the sum of factors in my loop so far I have this
#include <stdio.h>
#include<stdlib.h>
int main (void){
int i,value,factor1,remainder;

	printf("Please enter the integer you want to factor.\n");
	scanf("%d",&value);

for (i=1; i*i<=value; i++)
{
	
	remainder = value % i;
	if (remainder == 0)
	   { 
		factor1 = i;
		/*factor2 = value / i;*/
		printf("FACTOR = %d\n",factor1);
		/*printf("FACTOR 2 = %d\n", factor2);	
		if (factor1>factor2)
		break;*/
		}
		

}

system("pause");

return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Sum of factors in a loop

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Sum of factors in a loop

Posted 13 February 2008 - 10:13 AM

View Postsalyard, on 13 Feb, 2008 - 09:57 AM, said:

I have a code where I am making factors of a number but I am unable to figure out how to put the sum of factors in my loop so far I have this



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

int main (){
	
	int i,value,factor1,sum=0;

	printf("Please enter the integer you want to factor.\n");
	scanf("%d",&value);
	fflush(stdin);
	for (i=1; i<=value; i++){	
		if (!(value % i)){
			factor1 = i;
			sum = sum + factor1;
			printf("FACTOR = %d\n",factor1);
		}
	}
	printf("Sum of Factors: %d\n\n",sum);
	//system("pause");

	return 0;

}



This should do it for you! :)

More help?
Keep posting questions!


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

int main (){
	
	int i,value,factor1,sum=0;

	printf("Please enter the integer you want to factor.\n");
	scanf("%d",&value);
	fflush(stdin);
	for (i=1; i<value; i++){	
		if (!(value % i)){
			factor1 = i;
			sum = sum + factor1;
			printf("FACTOR = %d\n",factor1);
		}
	}
	printf("Sum of Factors: %d\n\n",sum);
	//system("pause");

	return 0;

}



This is a changed version, which doesn't include the inputted integer as a factor.

This post has been edited by letthecolorsrumble: 13 February 2008 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 salyard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-February 08

Re: Sum of factors in a loop

Posted 13 February 2008 - 10:03 PM

Thank you so much! I just needed something like that to remind that I could do it so now I have created it with product of factors and number of factors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1