Greatest Common Divisor

The greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers without remainder.

A common factor is a number which is a factor of two different problems; thus, in 1 x 4 the factors are 1, 2 and 4 (1 x 4 = 4; 2 x 2 = 4) and in 1 x 6 the factors are 1, 2, 3 and 6 (1 x 6 = 6; 2 x 3 = 6}; therefore the common factors of the two problems are 1 and 2, with 2 being the highest common factor.

Here’s a code for Greatest Common Divisor…

INPUT:

#include <stdio.h>

#include <conio.h>

int main()

{

int num, choice = 0;

int a, counter, center;

scanf(“%d”, &num);

while (choice != num){

scanf(“%d%d”, &a, &counter);

printf(“\n”);

for(counter=1;counter!=1;counter++){

center = counter;

counter = a % counter;

a = center;

}

printf(“#%d – %d\n”, (choice + 1), a);

choice++;

}

getch();

}

OUTPUT:

//Reminders: Don't just copy and paste the code, try to understand it. It's for your own benefit.

//Reminders: Don't just copy and paste the code, try to understand it. It's for your own benefit.

~ by smush143 on March 6, 2009.

5 Responses to “Greatest Common Divisor”

  1. helo i need your help sir… about programming… can you solve it? i need it tomorrow because i’ll pass it on tuesday(march 17) please help me? thank you….

    Problem 2 – Sum It!

    Write a program that will generate every third integer, beginning with 2 and continuing for all integers that are less than N. Calculate the sum of those integers that are evenly divisible by 5.

    Input
    The first line of input is the number of input cases (n), which is a positive number that does not exceed 10. Each line of the input will contain a single positive integer N.

    Output
    Your program should print the sum in a single line, for each case.

  2. Problem 3 – Perfect Numbers

    Greek mathematicians took a special interest in numbers that are equal to the sum of their proper divisors (a proper divisor of n is any divisor less than n itself). They called such numbers perfect numbers. For example, 6 is a perfect number because it is the sum of 1, 2 and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7 and 14.

    Your job is to create a program that determines if a given positive number is a perfect number or not.

    Input
    The first line of input is the number of input cases (n), which is a positive number that does not exceed to 30. The succeeding n lines specify the arbitrary number that your program will check if it is a perfect number or not. These numbers are positive integers that should not exceed 4,294,967,296 as well.

    Output
    For each input case, print out the corresponding answer (“Perfect” or “Not Perfect”) in a single line as standard output.

  3. The Cube

    Write a program to get the surface area and the volume of a cube, given a length of its sides.

    The surface area of a cube is six times the area of any side. In other words, the surface area of a cube is the area of the six squares that cover it. The area of one of them is a*a, or a2. Since these are all the same, you can multiply one of them by six, so the surface area of a cube is 6 times one of the sides squared.

    The volume is calculated by cubing the side. Volume is measured in “cubic” units. The volume of a figure is the number of cubes required to fill it completely, like blocks in a box. The volume of a cube is side x side x side. Since each side of a square is the same, it can simply be the length of one side cubed.

    Required: input(), surfaceArea(), volume() and display() FUNCTIONS

  4. Peso-Dollar Conversion

    Your brother, Harry, is in the States working. From time to time, he sends you money for some of your school expenses. However, sometimes, since the rate of exchange changes every now and then, you don’t know how much to ask from your brother or how much he has sent you. So, you decided to create a program that would help you and your brother in the conversion from Php to USD and vice versa.

    PROGRAM DESIGN
    In the program, the conversion should be performed by a function that returns a float value. Call this function from main() and pass the value to be converted. Since the conversion actually depends on the currency in which the value is specified, also pass an indication of the currency to the called function. Take note that you are not going to create an input function.

  5. Quadratic Equation

    In high school algebra, you learned that the standard quadratic equation ax2 + bx + c = 0 has two solutions given by the formula

    The first solution is obtained by using + in place of ±; the second is obtained by using – in place of ±. Write a program that calculates the two solutions.

    PROGRAM DESIGN
    In the program, the computation should be performed by a function that does not return a value. Call this function from main() and pass the values for the a, b and c. The program needs to include math.h interface to be able to use the sqrt() function, which returns a double value. Notice that you are not required to create a separate display function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 330 other followers