Question

In: Computer Science

#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...

#include <stdio.h>
int main() {
    int i, j;
    printf("Enter two positive integers: ");
    scanf("%d%d", &i, &j); // need to validate both being positive
    while (i != j) {
        i > j ? (i -= j) : (j -= i);
    }
    printf("GCD: %d\n", i);
    return 0;
}

The above code returns a GCD from two numbers entered. Use the above program to design

a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi + b x j.

EXAMPLE RUNS:
Enter two positive integers: 446 39
GCD: 1 = 23 * 446 + (-263) * 39
Enter two positive integers: 48 112
GCD: 16 = (-2) * 48 + 1 * 112
Enter two positive integers: 3 2
GCD: 1 = (-1) * 3 + 2 * 2

Thank you!!

Solutions

Expert Solution

#include <stdio.h>

// Returns gcd of two integers i and j
int gcd(int i, int j)
{
   while (i != j)
   {
       i > j ? (i -= j) : (j -= i);
   }
   return i;
}

int main()
{
   int i, j;
   printf("Enter two positive integers: ");
   scanf("%d%d", &i, &j);

   int GCD = gcd(i,j);
   printf("GCD: %d", GCD);
  
   int x=1,a,b;
   while(1)
   {
       // For a = 1, 2, 3,....
       a = x;
       b = (GCD-a*i)/j;
       if(a*i + b*j == GCD)
       {
           printf(" = %d * %d + (%d) * %d",a,i,b,j);
           break;
       }
       // For a = -1, -2, -3,....
       a = x-2*x;
       b = (GCD-a*i)/j;
       if(a*i + b*j == GCD)
       {
           printf(" = (%d) * %d + %d * %d",a,i,b,j);
           break;
       }
       x++;
   }
   return 0;
}

output screenshot:


Related Solutions

#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements one by one for (i = 0; i < c; i++) { // Checking if the picked element is already printed for (j = 0; j <= i; j++) { // If current element is already there in the array, break from j loop if (arr[i] == arr[j]) { break; } } // If it is not printed earlier and is within 10-100, then...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
#include <stdio.h> int main() { float sum_salary=0,sum_raise=0,sum_newsalary=0; while(1) { float salary,rate,raise,newsalary; printf("Enter Salary :"); scanf("%f",&salary); if(salary==-1)...
#include <stdio.h> int main() { float sum_salary=0,sum_raise=0,sum_newsalary=0; while(1) { float salary,rate,raise,newsalary; printf("Enter Salary :"); scanf("%f",&salary); if(salary==-1) { if(sum_salary==0) return 0; else { printf("Total Salary :%f Total raise :%f Total New Salary: %f",sum_salary,sum_raise,sum_newsalary); return 0; } } else { if(salary>=0 &&salary<30000) rate=7; else if(salary>=30000 &&salary<=40000) rate=5.5; else if(salary>40000) rate=4; raise=rate*salary/100; newsalary=salary+raise;    sum_salary=salary+sum_salary; sum_raise=raise+sum_raise; sum_newsalary=newsalary+sum_newsalary; printf("Salary :%f rate :%f Raise :%f New Salary: %f\n",salary,rate,raise,newsalary); } } } Hi can you please make sure that the following program will ask the user...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main(){ int numItms, j,...
Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main(){ int numItms, j, data, sum; scanf("%d",&numItms); sum = 0; for(j = 1; j <= numItms; j++){ scanf("%d",&data); sum += data; } printf("Sum:%d\n",sum); return 0; } SAMPLE INPUT: 48-376 SAMPLE OUTPUT: Sum: 18
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT