Loop Issues

Reply

Join Date: Oct 2004
Posts: 9
Reputation: tonja1196 is an unknown quantity at this point 
Solved Threads: 0
tonja1196 tonja1196 is offline Offline
Newbie Poster

Loop Issues

 
0
  #1
Oct 5th, 2004
I have a lab to create a loop to determine the positive divisors of an interger and then determine if the interger is a perfect,deficient, etc. I am having trouble determining how to write the statement to determine the divisors of the numbers. I believe the % operator is used. Just not sure. Help!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Loop Issues

 
0
  #2
Oct 5th, 2004
Greetings,

Well, you're right on. Finding the divisor of a positive number can be easily found using the modulus operator, %. The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In C++, the modulus operator is a percent sign, %. The syntax is exactly the same as for other operators:

int quotient = 7 / 3;
int remainder = 7 % 3;
The first operator, integer division, yields 2. The second operator yields 1. Thus, 7 divided by 3 is 2 with 1 left over.

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another: if x % y is zero, then x is divisible by y.

Now lets use this same principal by finding the divisor of 12. We know 1, 2, 3, 4, 6, and 12 are divisible by twelve. Maybe this can help us figure out how to determine the divisors using a for loop.

#include <stdio.h>

int main() {
	int num = 12;

	printf("The divisors of 12 are ");
	for (trialDivisor = 1; trialDivisor <= num / 2; ++trialDivisor)
		if (num % trialDivisor == 0) {
			divisorSum += trialDivisor;    // in other words: divisor_sum = divisor_sum + trial_divisor;
			printf("%d ", trialDivisor);
		}

	return 0;
}
This example is quite simple. You might ask yourself why we call the expression trialDivisor <= num / 2;. Well, I believe that is because 2 will always be a divisor of a positive number. It's usually dividend / divisor, and a divisor of 2 will never fail. Plus, we know that 24 is divisible into 24, though divisors only start at '/ 2' and down.

24 is divisbile by 12, 8, 6, etc...
36 is divisible by 18, 12, etc...


Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 9
Reputation: tonja1196 is an unknown quantity at this point 
Solved Threads: 0
tonja1196 tonja1196 is offline Offline
Newbie Poster

Re: Loop Issues

 
0
  #3
Oct 6th, 2004
Originally Posted by Stack Overflow
Greetings,

Well, you're right on. Finding the divisor of a positive number can be easily found using the modulus operator, %. The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In C++, the modulus operator is a percent sign, %. The syntax is exactly the same as for other operators:

int quotient = 7 / 3;
int remainder = 7 % 3;
The first operator, integer division, yields 2. The second operator yields 1. Thus, 7 divided by 3 is 2 with 1 left over.

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another: if x % y is zero, then x is divisible by y.

Now lets use this same principal by finding the divisor of 12. We know 1, 2, 3, 4, 6, and 12 are divisible by twelve. Maybe this can help us figure out how to determine the divisors using a for loop.

#include <stdio.h>

int main() {
	int num = 12;

	printf("The divisors of 12 are ");
	for (trialDivisor = 1; trialDivisor <= num / 2; ++trialDivisor)
		if (num % trialDivisor == 0) {
			divisorSum += trialDivisor;    // in other words: divisor_sum = divisor_sum + trial_divisor;
			printf("%d ", trialDivisor);
		}

	return 0;
}
This example is quite simple. You might ask yourself why we call the expression trialDivisor <= num / 2;. Well, I believe that is because 2 will always be a divisor of a positive number. It's usually dividend / divisor, and a divisor of 2 will never fail. Plus, we know that 24 is divisible into 24, though divisors only start at '/ 2' and down.

24 is divisbile by 12, 8, 6, etc...
36 is divisible by 18, 12, etc...


Hope this helps,
- Stack Overflow




Thank You!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 9
Reputation: tonja1196 is an unknown quantity at this point 
Solved Threads: 0
tonja1196 tonja1196 is offline Offline
Newbie Poster

Re: Loop Issues

 
0
  #4
Oct 6th, 2004
Thank you so much for the help. If the divisors of 12 are 1,2,3,4,6 what do I need to do to show the total of the divisors?? : (1+2+3+4+6=16)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop Issues

 
0
  #5
Oct 6th, 2004
>what do I need to do to show the total of the divisors?
Keep a running total. In StackOverflow's example, look at divisorSum that he neglected to declare. That's the general idea.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 9
Reputation: tonja1196 is an unknown quantity at this point 
Solved Threads: 0
tonja1196 tonja1196 is offline Offline
Newbie Poster

Re: Loop Issues

 
0
  #6
Oct 8th, 2004
See my attachement below: I believe one one braces is in the wrong location. I am trying to loop a range of #20-30 and #490-500 and have each number printed and stating is the number is deficient,perfect or abundant. The ouput I am tryint to achieve should be in this format. I hope this is making sense.

Number Classification
20 deficient*(*I am guessing the outcome)
21 perfect
"" ""
500 abundant

#include <stdio.h>
#include <iostream>
using namespace std;
#include<iomanip>

int main() {
int num = 20,divisor,divisorSum,total=0,num2=490,count;


cout<<setw(2)<< "\n Number Classification \n";
cout <<"___________________________________" <<"\n" ;

while (num<=30,num2<=500)
{ cout<< num<<num2;
num++,num2++;

for (divisor = 1; divisor <= num / 2; ++divisor)
if (num % divisor == 0)
{
total=total+divisor;
}
if (total>num)
{ cout<< setw(4)<<"\t"<<num<< "\tAbundant \n";
cout<< setw(4)<<"\t"<<num2<< "\tAbundant \n" ;
}
else if (total<num)
{ cout<< setw(4)<<"\t"<<num<<"\tDeficient \n";
cout<< setw(4)<<"\t"<<num2<<"\tDeficient \n";
}
else if (total=num)
{ cout<< setw(4)<<"\t"<<num<<"\tPerfect \n";
cout<< setw(4)<<"\t"<<num2<<"\tPerfect \n";
}




}

system ("pause");
return 0;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC