Question

In the Source Folder (src) of this project create a new C source file called "gcd.c"....

In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer.

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

int main(int argc, char *argv[]) {
//==============================
        setbuf(stdout, NULL);           // turns standard output buffering off

        int i;                          // Loop conter/index.
    int a;                          // The first number entered
    int b;                          // The second number entered.
    int gcd = -1;                               // GCD of "a" and "b". Initialized to -1 only for the purpose of viewing in debugger.

                                                                // Read an integer from the keyboard.
    printf("\n");
    printf("Enter the first  Positive Integer [0 to quit]: ");
        fflush(stdout);                 // Only required for GDC debugger to process input!
    scanf("%d", &a);

    printf("Enter the second Positive Integer [0 to quit]: ");
        fflush(stdout);                 // Only required for GDC debugger to process input!
    scanf("%d", &b);

    while ( (a >= 1) && (b >= 1) ) {
                                                                        // Calculate the "gcd" of the two entered values.
        for (i=1; i<=a && i<=b; i++) {
                                                                        // Check if "i" is a factor of both integers
                                                                        // and keep the largest one!
                if ( (a%i == 0) && (b%i == 0) ) gcd = i;
        }

        printf("\n");
        printf("===> G.C.D. of %d and %d is: [%d]\n", a, b, gcd);
        printf("\n");
                                            // Read the next pair of integers.
        printf("Enter the first  Positive Integer [0 to quit]: ");
        fflush(stdout);                     // Only required for GDC debugger to process input!
        scanf("%d", &a);

        printf("Enter the second Positive Integer [0 to quit]: ");
                fflush(stdout);                     // Only required for GDC debugger to process input!
        scanf("%d", &b);
    }
    printf(":::\n");
    printf("::: Program Terminated.\n");
    printf(":::\n");
    return 0;
}

The program output should look like the following:

Enter the first  Positive Integer [0 to quit]: 0
:::
::: Program Terminated.
:::
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Just add loop at line 39 line

it checks if a==0 then it continues to loop

code:

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

int main(int argc, char *argv[]) {
//==============================
setbuf(stdout, NULL); // turns standard output buffering off

int i; // Loop conter/index.
int a; // The first number entered
int b; // The second number entered.
int gcd = -1; // GCD of "a" and "b". Initialized to -1 only for the purpose of viewing in debugger.

// Read an integer from the keyboard.
printf("\n");
printf("Enter the first Positive Integer [0 to quit]: ");
fflush(stdout); // Only required for GDC debugger to process input!
scanf("%d", &a);

printf("Enter the second Positive Integer [0 to quit]: ");
fflush(stdout); // Only required for GDC debugger to process input!
scanf("%d", &b);

while ( (a >= 1) && (b >= 1) ) {
// Calculate the "gcd" of the two entered values.
for (i=1; i<=a && i<=b; i++) {
// Check if "i" is a factor of both integers
// and keep the largest one!
if ( (a%i == 0) && (b%i == 0) ) gcd = i;
}

printf("\n");
printf("===> G.C.D. of %d and %d is: [%d]\n", a, b, gcd);
printf("\n");
// Read the next pair of integers.
printf("Enter the first Positive Integer [0 to quit]: ");
fflush(stdout); // Only required for GDC debugger to process input!
scanf("%d", &a);
  
if(a==0) //addes this loop
{
   continue;
       }
  
printf("Enter the second Positive Integer [0 to quit]: ");
fflush(stdout); // Only required for GDC debugger to process input!
scanf("%d", &b);
}
printf(":::\n");
printf("::: Program Terminated.\n");
printf(":::\n");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In the Source Folder (src) of this project create a new C source file called "gcd.c"....
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
  • Could you do that in C language? Here is the code which we got #include <stdio.h>...

    Could you do that in C language? Here is the code which we got #include <stdio.h> #define MAX_SIZE 20 // function definitions void displaySpiral(int matrix[][MAX_SIZE], int size); void displayMatrix(int matrix[][MAX_SIZE], int size); int takeInput(int inputMatrix[][MAX_SIZE]); int main() { int matrix[MAX_SIZE][MAX_SIZE]; int matrixSize = takeInput(matrix); printf("Displaying the whole matrix:\n"); fflush(stdout); displayMatrix(matrix, matrixSize); printf("Now, displaying the matrix in a spiral way:\n"); fflush(stdout); displaySpiral(matrix, matrixSize); return 0; } // already implemented for you int takeInput(int inputMatrix[][MAX_SIZE]) { int size; printf("What is the size...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • I have a question on an assignment for C++. I have my code available here and...

    I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Create a C project named login_l03t1. Create an array of int from parameters passed on the...

    Create a C project named login_l03t1. Create an array of int from parameters passed on the command line. Use argc to define the size of the array. Print the contents of the array in the format and Generate and print the total of the values in the array twice: once by using an index i, and a second time by incrementing a pointer to the array. I need help with this please! what I have done is below: int n...

  • the coding language is just the basic c language and here is my first attempt at...

    the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...

  • 1. (20 pts.) Consider the following C program. Assume that all functions complete successfully. Complete the...

    1. (20 pts.) Consider the following C program. Assume that all functions complete successfully. Complete the Process Model, given that the process id's (pids) are as shown for each process created. Fill in the boxes for each variable, p1 and p2, showing how the values change in each process. Just cross out old values (don't erase). int main ) Process Model int p1 = o, p2-0; pl-fork ) if (pl !# 0) p2 fork ( ); if (plp2 0) printf...

  • Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and...

    Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and "Creating a Flowchart" assignments previously implemented in C. Compare and contrast the C and MATLAB versions of your codes. convert this to a matlab program #include <stdio.h> int main() { int a; printf("Enter first number: "); scanf("%d", &a); int b; printf("Enter second number: "); scanf("%d", &b); int limit; printf("Enter limit: "); scanf("%d", &limit); printf("\nFirst number: %d\n", a); printf("Second number: %d\n", b); int c; c...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

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