Question

Consider the following C codes to compute the gcd of two integers. /// code 1 #include...

Consider the following C codes to compute the gcd of two integers.

/// code 1

#include <stdio.h>

int gcd(int a, int b) {

    while (a != b) {

        if (a > b) a = a - b;

        else b = b - a;

    }

    return a;

}

/// code 2

#include <stdio.h>

int getint() {

    int i;

    scanf("%d", &i);

    return i;

}

void putint(int i) {

    printf("%d\n", i);

}

int main() {

    int i = getint(), j = getint();

    while (i != j) {

        if (i > j) i = i % j;  

        else j = j % i;

    }

    putint(i);

}

Do code 1 and code 2 work correctly? Why and why not? Can you fix the one(s) not working (if any?) Which one is faster? Explain.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code1:- will work perfectly no issue but it will take more time for one smaller number and one larger number only that is disadvantage.

Code2:- there is some mistake in that Code-2 while condition is wrong the code is turing to fall in infinite loop

Coorect Code2:-

#include<stdio.h>

int getint()

{

int i;

scanf("%d",&i);

return i;

}

void putint(int i)

{

printf("%d",i);

}

int main()

{

int i=getint();int j=getint(),temp;

while(i!=0)

{

if(i>j)

{

temp=i;

i=j;

j=temp;

}

int x=i;

i=j%i;

j=x;

}

putint(j);

}

int main () int i-getint);int j-getint,temp; lh lh while (i!-0) if(i>j) temp-i i-j j-temp int x-i; putint (5)

Code2 will fast because we are not doing repetetion substractions and we are doing modular division

Add a comment
Know the answer?
Add Answer to:
Consider the following C codes to compute the gcd of two integers. /// code 1 #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf...

    can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...

  • Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b; scanf("%d",&a); scanf("%d",&b); a=a<<2; b=b<<2; printf("%d&%d\n",a,b); return 0; }

  • I am told to find the median of random inputs using if statements in C #include...

    I am told to find the median of random inputs using if statements in C #include <stdio.h> #include <stdlib.h> int main() { double a, b, c, d; scanf("%lf", &a); scanf("%lf", &b); scanf("%lf", &c); if (a<b && b<c) d = b; printf("%.0f\n", d); else if (b<a && a<c) d = a; printf("%.0f\n", d); else d = c; printf("%.0f\n", d); return 0; } That is my code, I keep getting an error for the else if and else saying there is no...

  • Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a|b; printf("%d|%d=%d\n",a,b,c); return 0; }

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT