Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,123 people online right now. Registration is fast and FREE... Join Now!




Simple script using for loop to find highest common divisor of two num

 
Reply to this topicStart new topic

Simple script using for loop to find highest common divisor of two num

Nyclassic2006
27 Sep, 2006 - 04:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2006
Posts: 2


My Contributions
Hi, I am two days into C++. I am looking into a teach yourself book on C++ if anyone has any recomendations, I'm all ears. I'm taking a class the textbook doesn't even cover C++ , its more about JAVA yet we are assigned a few scripts in C++. I have been okay with the last few, this one I just can't figure out.

I"m supposed to write a simple script using a for loop that will ask the user to input two numbers and it will output the highest common divisor of both numbers. I have this so far. My teacher has a habit of asking for us to do a project then the following day tells us he hasn't taught us what we needed to know to figure it out. If someone can offer help or the solution I would greatly appreciate it. I have been up until 1 am trying to figure it out, I think it is a concept I haven't been taught yet that I'm missing. Sorry I know my code is a mess so far.
CODE

# include <iostream.h>
int main ()
{
int p,q,i;
{
cout<<"Please enter two integers: ";
}
cin>>p>>q;
for (i=2; i<=p; i++);
{if(p%i==0 && q%i==0)

cout<<i<<" is your greatest common divisor of both numbers";}

return 0;
}

User is offlineProfile CardPM
+Quote Post

Nyclassic2006
RE: Simple Script Using For Loop To Find Highest Common Divisor Of Two Num
28 Sep, 2006 - 07:00 AM
Post #2

New D.I.C Head
*

Joined: 27 Sep, 2006
Posts: 2


My Contributions
Hey All, I found the solution if any other newbies like myself need it, sorry to the pros.

CODE

# include <iostream.h>
int main ()
{
int p,q,i,gcd;
{
cout<<"Please enter two integers: ";
}
cin>>p>>q;
for (i=1; i<=p; i++)
{
if(p%i==0 && q%i==0)
gcd=i;
}
{
cout<<"The gcd of "<<p<<" and "<<q<<" is "<<gcd;
}
return 0;
}


EDIT : born2c0de: Code Tags Added.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month