|
-
Aug 6th, 2003, 01:42 PM
#1
Thread Starter
Hyperactive Member
find all multiples? .:Resolved:.
Hello everyone, I again ran into a simple algebra 1 concept that i can't figure out. I have to write a program to add up all the multiples of 3 between 1 and 100; This should be cake once i find out how you find a mutliple of a number. Anyone care to give me the run down on how that works? I'm sure I did it before, I got a final grade of 98 in calc which is sad because I forgot basic algebra concepts. Maybe thats why i scored so low on my SAT's heh anyways, thanks for listening
Last edited by voidflux; Aug 7th, 2003 at 12:45 PM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 6th, 2003, 04:35 PM
#2
A loop like this maybe?
VB Code:
Dim Total As Integer
Dim i As Integer
Total = 0
For i = 1 To 100
If i Mod 3 = 0 Then
Total = Total + i
End If
Next
MsgBox Total
Has someone helped you? Then you can Rate their helpful post. 
-
Aug 6th, 2003, 08:53 PM
#3
Thread Starter
Hyperactive Member
I didn't even look at ur code because I want to do it on my own (coding) I don't want you to tell me how to code it, I just need the logic behind finding multiplies of 3, what does find a multiple mean? Like I need an example, what is a multiple? Thanks for the responce.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 7th, 2003, 12:04 AM
#4
Fanatic Member
A multiple of a number m is a number, n, such that n = m * k where k is some integer.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Aug 7th, 2003, 07:35 AM
#5
-
Aug 7th, 2003, 08:03 AM
#6
Addicted Member
Finding multiples goes like this...
9 is a multiple of 3 because when you multiply 3 × 3 you get 9
10 is not a multiple of 3 because you cannot multiply any whole number with 3 and get 10...
now if you divide 9 into 3 your answer will be 3 with a remainder of 0
if you divide 10 into 3 your answer will be 3 with a remainder of 1
all multiples of a number when divided by that number always have a remainder of 0... so thats what manavo11 was using in his code, he was checking all the numbers from 1 to 100 to see if they had a remainder of 0 when they are divided into 3...
hope this helps
-
Aug 7th, 2003, 11:32 AM
#7
Thread Starter
Hyperactive Member
Excellent! Thank you everyone for your replies! 
Here is what I came up with:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
//write a for statement to add all multiples
//of 3 between 1 and 100.
int sum = 0;
for(int i = 1; i <= 100; i++)
{
if((i%3) == 0)
{
sum += i;
cout << setw(5) << i;
}
}
cout <<"\nThe sum of the multiplies is: "
<< sum << endl;
return 0;
}
Code:
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96
99
The sum of the multiplies is: 1683
Press any key to continue
Last edited by voidflux; Aug 7th, 2003 at 11:44 AM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 7th, 2003, 03:07 PM
#8
Fanatic Member
You could make the loop faster if you counted off by 3's until your number is greater than 100.
Code:
for(int i = 3; i <= 100; i+=3)
{
sum += i;
cout << setw(5) << i;
}
Just a suggestion.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Aug 7th, 2003, 10:38 PM
#9
Thread Starter
Hyperactive Member
That is true, and a good idea but it says It wanted you to test numbers 1-100, that loop would be testing numbers 3-100 even though you really don't need to test 1 and 2 i'm just following directions!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Aug 8th, 2003, 03:46 PM
#10
Fanatic Member
Actually, its testing every third number from 3 to 100. 
It makes me cry to see wasted processor cycles.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Aug 9th, 2003, 07:39 AM
#11
Actually, since you are looking for the sum of the sequence:
a0=0
an=an-1+3
for n=1...floor(100/3)=33
Which is:
(first+last)*number_of_terms/2 = (99+3)*33/2 = 1683
A program would look like:
VB Code:
Dim max As Long
max = 100
MsgBox (3+((max\3)*3))*max\6
This is the maths forum, after all
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|