Thread: Finding The Hcf Of A Number

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    Finding The Hcf Of A Number

    I am writing a code to display the HCF(highest common factor) of a number. For those of you who dont know just google it.
    i have come up with this piece of code
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    main()
    {
          int a,a1,b,b1,x=0,y=0;
          cout<<"Enter two numbers :";
          cin>>a>>b;
         
          for(a1=1;a1<=a;b1++)
          {
                             if(a%a1==0)
                             x=a1;
                for(b1=1;b1<=a;b1++)  
                {
                             if(b%b1==0)   
                             y=b1;
                }
          if(x==y)
          cout<<x<<",";
          }
          
          _getch();
          }
    the code returns no value
    why?
    I would rather be hated for who I am than be loved for who I am not!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    What you are doing:
    For each factor of a;
    1. Get the highest factor of b
    2. If this is the factor of a, print it
    This won't print anything unless the higher factor of b is a factor of a.

    This would work:
    For each factor of a;
    1. Test if it is a factor of b. If so, store it
    Print the stored value

    But there's a way better algorithm. See: http://en.wikipedia.org/wiki/Euclidean_algorithm


    Hope that helps,
    EVOEx

  3. #3
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    Thanks mate!...ii learn c++ in school. but they havent thought function.(i know till classes).
    is it possible to have the same logic inside the main method?
    no answers please.
    I would rather be hated for who I am than be loved for who I am not!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    They teach you classes before functions? That's... strange... to say the least .

    Anyway, without a function, it'd be, in pseudocode (sorry, I'm not going to do it all for you):
    Preconditions:
    a = first number
    b = second number

    Algorithm:
    Code:
    while b is not 0
      set c = a modulo b
      set a = b
      set b = c
    Postconditions:
    a is the answer

    Hope that helps

  5. #5
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    ah!. they havent thouight the whole class function and i know till classes.( i learnt it on my own!!!)
    I would rather be hated for who I am than be loved for who I am not!

  6. #6
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    thanks anyway!!!
    a lot. it worked
    I would rather be hated for who I am than be loved for who I am not!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Finding the biggest number?
    By Tarento in forum C Programming
    Replies: 2
    Last Post: 05-17-2006, 09:18 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM