[ create a new paste ] login | about

Link: http://codepad.org/98Tb2shJ    [ raw code | output | fork | 3 comments ]

C, pasted on Aug 23:
#include<stdio.h>
#include<conio.h>

int main()
{
	int a, b;

	do{
		printf("\nNhap vao so nguyen duong a = ");
		scanf_s("%4d", &a);
		if (a < 0)
		{
			printf("\nGia tri nhap vao khong hop le. Xin kiem tra lai !");
		}
	} while (a < 0);

	do{
		printf("\nNhap vao so nguyen duong b = ");
		scanf_s("%4d", &b);
		if (b < 0)
		{
			printf("\nGia tri nhap vao khong hop le. Xin kiem tra lai !");
		}
	} while (b < 0);

	int Max = a > b ? a : b;
	int Min = a < b ? a : b;

	//for(int i = Min; i >= 1; i--)  // ước chung lớn nhất luôn là ước của min
	//{
	//	if(Max % i == 0 && Min % i == 0)
	//	{
	//		printf("\nUSCLN = %d", i);
	//		break;
	//	}
	//}

	// Cách 2:
	if (Max % Min == 0)
	{
		printf("\nUSCLN = %d", Min);
	}
	else
	{
		for (int i = Min / 2; i >= 1; i--)
		{
			if (Min % i == 0 && Max % i == 0)
			{
				printf("\nUSCLN = %d", i);
				break;
			}
		}
	}

	// Cách 3:
	while(a != b)
	{
		if(a > b)
			a = a - b;
		else
			b = b - a;
	}
	printf("\nUSCLN = %d", a); // hay in ra b cũng được vì  lúc này a == b
	getch();
	return 0;
}


Output:
1
2
3
Line 17: error: conio.h: No such file or directory
In function 'main':
Line 45: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments:
posted by lethi200397 on Oct 20
#include<stdio.h>
#include<conio.h>
int main()
{
int i = 1, a, b, P;
printf(" nhap a = ");
scanf("%d", &a);
printf(" nhap b = ");
scanf("%d", &b);
do
{
if(a % i == 0 && b % i == 0)
{
P = i;
}
i++;
}
while(i <= a);
printf(" UCLN = %d", P);
getch();
return 0;

}


reply
posted by dangtiendat86@gmail.com on Feb 11
#include<iostream>
#define ll long long
using namespace std;
main(){
ll a, b;
cout<<"a="; cin>>a;
cout<<"b="; cin>>b;
while (a*b!=0){
if (a>b) a%=b;
else b%=a;
}
cout<<"UCLN="<<a+b<<endl;
}
reply
posted by phuquy12 on Mar 26
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <stdint.h>
/* Private variables */

/*Private Function */

/**
* @brief The application entry point.
* @retval int
*/
int main(){
unsigned int m,n,i,temp,max;
int count;
while(1)
{
count =0; i=1 ; max =1;
printf("\nNhap N (N>0):");
scanf("%d",&n);
printf("\nNhap M (M>0):");
scanf("%d",&m);
if((n<=0)||(m<=0)) printf("Xin nhap lai!");
else
{
if(m<n) temp = m;
else temp = n;
while(i<=temp)
{
if(((n%i)==0)&&((m%i)==0))
{
if(max<i) max = i;
}
++i;
}
printf("\nUoc chung lon nhat cua %d va %d la %d ",n,m,max);
}
}
}

reply