This blog is under construction

Friday 12 July 2013

C program to calculate GCD of n numbers

Write a C program to calculate GCD of n numbers.


  #include <stdio.h>
  #include <stdlib.h>

  int main() {
        int i, j, n, flag = 0, small, *data;

        /* get the number of inputs from the user */
        printf("Enter the number of inputs:");
        scanf("%d", &n);

        /* allocate memory to store n numbers */
        data = (int *)malloc(sizeof(int) * n);

        /* get n numbers from the user */
        for (i = 0; i < n; i++) {
                printf("Data[%d]: ", i);
                scanf("%d", &data[i]);
        }

        /* find the smallest of n numbers */
        small = data[0];
        for (i = 1; i < n; i++) {
                if (data[i] < small)
                        small = data[i];
        }

        /*
         * use the smallest no to find gcd of n numbers.
         * Start checking from small to 1 whether the 
         * same value divides all the given inputs
         */
        for (i = small; i > 0; i--) {
                for (j = 0; j < n; j++) {
                        if (data[j] % i != 0) {
                                flag = 1;
                        }
                }
                /* print the result */
                if (!flag) {
                        printf("GCD of given %d numbers is %d\n", n, i);
                        break;
                }
                flag = 0;
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of inputs:5
  Data[0]: 144
  Data[1]: 180
  Data[2]: 108
  Data[3]: 117
  Data[4]: 135
  GCD of given 5 numbers is 9



72 comments:

  1. I think in the 'j' loop when the condition arr[j] % i != 0 is satisfied you should break from the loop there only. It is performing unnecessary computations after that.

    ReplyDelete
  2. I hope to really understand for this information.Thanks a lot.

    Manpower Consultancy in Chennai

    ReplyDelete
  3. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    c,c++ training in chennai

    ReplyDelete



  4. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Android training in chennai
    Ios training in chennai

    ReplyDelete

  5. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
    Web Design Company
    Web Development Company
    Mobile App Development Company

    ReplyDelete
  6. Very informative and It was an awesome post.Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. Thanks for sharing it with us. We are so greatful to your sharing.c c++ training in chennai

    ReplyDelete
  7. Very informative and It was an awesome post.Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. Thanks for sharing it with us. We are so grateful to your sharing. c training in chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. I am looking for some good blog sites for studying. I was searching over search engines and found your blog site. Well i like your high quality blog site design plus your posting abilities. Keep doing it.

    App developers
    mobile application companies
    iOS app developers

    ReplyDelete
  11. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.

    Text message marketing
    Texting API

    ReplyDelete
  12. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. Best Java Training Institute Chennai

    ReplyDelete
  13. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    amazon-web-services-training-in-bangalore

    Best Hadoop Training Institute In chennai

    ReplyDelete
  14. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
    datascience training in chennai

    ReplyDelete
  15. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.

    https://www.besanttechnologies.com/robotic-process-automation-rpa-training-in-chennai

    ReplyDelete
  16. Thanks for this wonderful posting!I enjoyed reading it;You are a great author.I will make sure to bookmark your blog and may come back someday.I want to appreciate that you continue your great posts.

    Robotics Project Centers in Chennai | Vlsi Projects in Chennai.

    ReplyDelete
  17. Hi Admin, I went through your article and it’s totally awesome.
    cloud sim training in chennai | arduino training in chennai

    ReplyDelete
  18. I have read your blog its very attractive and impressive. I like it your blog.
    me projects chennai | me project centers chennai.

    ReplyDelete
  19. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.

    Hadoop Training in Chennai

    Hadoop Training in Bangalore

    Big data training in tambaram

    Big data training in Sholinganallur

    Big data training in annanagar

    ReplyDelete
  20. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    java training in omr

    ReplyDelete
  21. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Python training in marathahalli
    Python training in pune
    AWS Training in chennai

    ReplyDelete
  22. Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  23. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    python online training
    python training in OMR
    python training course in chennai

    ReplyDelete
  24. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    aws online training
    aws training in hyderabad
    aws online training in hyderabad

    ReplyDelete
  25. Information from this blog is very useful for me, am very happy to read this blog Kindly visit us @ Coimbatore Christmas Gift boxes | Wallet Box

    ReplyDelete


  26. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Best AWS Training in Chennai | Amazon Web Services Training in Chennai


    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune


    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai

    AWS Training in Chennai |Best Amazon Web Services Training in Chennai

    Learn Amazon Web Services Tutorial |AWS Tutorials For Beginners

    AWS Interview Questions And Answers

    ReplyDelete
  27. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.

    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    ReplyDelete
  28. It was really an interesting blog, Thank you for providing unknown facts.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  29. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    java training in Chennai | java training Institute in Chennai

    ReplyDelete




  30. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    software Testing Training |Best Software Training Institute in Chennai



    ReplyDelete
  31. I have read your blog it is very helpful for us. I couldn't find any knowledge on this matter prior to. I would like to thanks for sharing this article here. please Do keep sharing more blogs like this.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training



    ReplyDelete
  32. Thanks for sharing this wonderful content.its very useful to us.This is incredible,I feel really happy to have seen your webpage. I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.


    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore


    ReplyDelete
  33. If you're feeling a little light on transferable experience or want to beef up your leadership skills, consider taking a management class or working toward a certification. ... Prospective employers will likely be impressed that you took the initiative to sharpen your skills, and it's a great way to show your commitment.keep up!!

    Android Training in Chennai

    Android Online Training in Chennai

    Android Training in Bangalore

    Android Training in Hyderabad

    Android Training in Coimbatore

    Android Training

    Android Online Training

    ReplyDelete
  34. Infycle Technologies, the best software training institute in Chennai offers the leading Data Science course in Chennai for tech professionals, freshers, and students at the best offers. In addition to the Data Science course, other in-demand courses such as Python, Cyber Security, Selenium, Oracle, Java, Power BI, Digital Marketing also will be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  35. Infycle Technologies, the top software training institute and placement center in Chennai offers the Digital Marketing course in Chennai for freshers, students, and tech professionals at the best offers. In addition to the Oracle training, other in-demand courses such as DevOps, Data Science, Python, Selenium, Big Data, Java, Power BI, Oracle will also be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  36. During a OctaFx Review workshop, each attendee will learn how to interpret market action using proven charting techniques, searching for reliable chart patterns and indicators to help them identify trading opportunities.

    ReplyDelete
  37. Your Fxtm Review Account Is Good For You. Give It A Try Today.

    ReplyDelete
  38. An obligation of appreciation is for sharing significant information. Your internet based diaries were helpful. I request to invigorate the blog through little by little. Moreover, find your blog.
    A Homoeopathic approach to Skin Diseases
    Homoeopathy
    Drshwetahomoeopathy

    ReplyDelete
  39. Thank you so much for sharing this information. Do visit ASP.NET training course in chennai

    ReplyDelete