943,852 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3426
  • C RSS
Oct 9th, 2003
0

Please help me out, need help

Expand Post »
At least show me the logic on how to solve the math problem parts. I will put it into program if you just explain to me how to get the factors, tell the computer how many factors, find the sum and print it to screen, and find the product and print it to screen. Then i will put it all together.

Hi, im kinda new to using loops in my programming. I'm still a little rusty. could someone hep me out a little on this problem?
I sure could use some quick help from you guys. You've always been there in the past. Thanks so much.

I need a program that will prompt the user for a single positive integer greater than 1. If the user does not enter a valid integer, then the program should continue to reprompt the user for a value until a valid integer is entered. Once the integer is read in, the program will print out the following information about the integer:

1) A list of each of the positive factors of the given integer.
2) The number of factors the given integer has.
3) The sum of the factors of the integer.
4) The product of the factors of the integer.

An example output would be :

Enter a positive integer greater than one.
12

Here is a list of the positive factors of 12:
1 2 3 4 6 12

The number of positive factors of 12 is 6.
The sum of the positive factors of 12 is 28.
The product of the positive factors of 12 is 1728.

thanks so much guys.

Heres what i got so far :



int main()

{

int LoopCount = 0;
int integer;


printf("Enter a positive integer greater than one.\n";
scanf("%d", integer);


for (int j=1; j <= LoopCount; j++){
for (int integer=1; integer <=LoopCount; integer++){
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stehigs321 is offline Offline
4 posts
since Oct 2003
Oct 9th, 2003
0

Re: Please help me out, need help

user enters NUMBER
for loop counting X from NUMBER to 1 counting backwards
if ( (NUMBER % X) == 0 ) then X is a factor
print X to the screen
continue looping


In the above algorithm, we use modular division. (e.g. number % x = number mod x) What modular division does, is it gives you the remainder when regular division is performed. Take the following example.

NUMBER = 12

x = 12
12/12=1
12%12=0
12 is a factor

x = 11
12/11=...
12%11=.....
11 is not a factor
.
.
.
x= 6
12/6=2
12%6=0
6 is a factor
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Oct 9th, 2003
0

Re: Please help me out, need help

so how would i put this into code? I also have to say how many factors there are, anf the sum and product of the factors.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stehigs321 is offline Offline
4 posts
since Oct 2003
Oct 9th, 2003
0

COMPILING ERROR??

why do i get this compiling error with the following code??

numbers.c: In function `main':
numbers.c:33: `for' loop initial declaration used outside C99 mode

  1. int main() {
  2. int oneNumber =0; // hold the valid integer entered
  3. int countPositiveFactors =0; // accumulate a sum , so should be initialize to 0
  4. int sumPositiveFactors =0; // accumulate a sum , so should be initialize to 0
  5. int prodPositiveFactors = 1; // accumulate a product so should be initialize to 1
  6.  
  7. while (1)
  8. {
  9. printf("Enter an integer > 1 :");
  10. scanf("%d", &oneNumber);
  11. if (oneNumber > 1)
  12. break;
  13. else
  14. printf("Error: Number should be > 1\n");
  15. _flushall(); // to flush all streams e.g. stdin
  16.  
  17. }
  18.  
  19. printf("\n\nHere is a list of the positive factors of %d\n",oneNumber);
  20. for (int i=1;i<=oneNumber;i++)
  21. {
  22. if (oneNumber%i==0) // one factor found
  23. {
  24. countPositiveFactors++; // add 1 to count of positive factors
  25. printf("%d ",i); // print this factor
  26. sumPositiveFactors+=i; // add this factor e.g. i to the sum
  27. prodPositiveFactors*=i; // multiply this factor with the previous factors
  28.  
  29. }
  30. }
  31.  
  32. printf ("\n\nThe number of positive factors of %d is %d.\n",oneNumber, countPositiveFactors);
  33. printf ("The sum of the positive factors of %d is %d.\n",oneNumber, sumPositiveFactors);
  34. printf ("The product of the positive factors of %d is %d.\n",oneNumber, prodPositiveFactors);
  35.  
  36. return 0;
  37. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stehigs321 is offline Offline
4 posts
since Oct 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help with DirectX code
Next Thread in C Forum Timeline: struct error (!?)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC