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



37 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. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Web Design Company
    Web Development Company

    ReplyDelete
  7. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Android App Development Company
    Android App Development Company

    ReplyDelete
  8. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    PHP training in chennai

    ReplyDelete
  9. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    iOS App Development Company

    ReplyDelete
  10. 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
  11. 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
  12. Great post…. you have done great job…its very cool blog. Linking is very useful thing you have really helped lots of people who visit this blog and provided them this useful information on opinionated astrologer. Thanks a lot for this.After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
    Hadoop Training in Chennai
    Big Data Training in Chennai
    Python Training in Chennai
    Python Training Centers in Chennai
    Data Science Training in Chennai
    Data Science Course in Chennai
    Data Analytics Training in Chennai
    Best AngularJS Training in Chennai
    AngularJS Training in Chennai
    QlikView Training in Chennai
    Informatica Training in Chennai

    ReplyDelete
  13. 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
  14. 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
  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. Best Java Training Institute Chennai

    ReplyDelete
  16. 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
  17. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    amazon-web-services-training-in-bangalore

    ReplyDelete
  18. 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
  19. 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
  20. 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