NWD w C błąd

0

Witam napisałem program obliczający nwd 2 liczb tylko że za każdym razem wynik wynosi 0.
Co robie źle?

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

int nwd(int a, int b)
{
    int nwd=0;
    do
    {
        if (a>b) a=a-b;
        else b=b-a;
    }
    while (a!=b);
    return a;
}

int main()
{
    int a=0,b=0;
    scanf("%d%d",&a,&b);
    printf("%d",nwd(a,b));
    return 0;
}
0

http://pl.wikipedia.org/wiki/Algorytm_Euklidesa
tutaj masz gotowca. skopiuj sobie.

0

Skopiowałem i nadal to samo ;/

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

int nwd(int a, int b)
{
    int c=0;
    while (b != 0)
    {
          c = a % b;
          a = b;
          b = c;

    }
    return a;
}

int main()
{
    int a=0,b=0;
    scanf("%d",&a);
    scanf("%d",&b);
    printf("%d",nwd(a,b));
    return 0;
}
0
#include <stdio.h>
 
int nwd(int a, int b);
 
int main(int argc, char **argv)
{
	int a=0,b=0;
	printf("Podaj a: ");
	scanf("%d",&a);
	printf("Podaj b: ");
	scanf("%d",&b);
	printf("NWD liczb a i b to %d",nwd(a,b));
	
	return 0;
}
 
int nwd(int a, int b)
{
	int c=0;
	while (b != 0)
	{
		c = a % b;
		a = b;
		b = c;
	}
	if(a<0)
	{
		a = -a;
	}
	
	return a;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1