Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as C++ by registered user xujiayu ( 4 years ago )
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{   
	/*nhap vao 1 so nguyen duong n , dem xem n co bao nhieu chu so ,tim ra chu so lon nhat va chu so nho nhat 
	vd : n = 216935
	=> co 6 chu so 
	=> chu so min = 1 , chu so max = 9 */

	printf("\nhay nhap vao 1 so nguyen duong kiem tra xem co bao nhieu chu so va tim mix max");

	int n;

	do
	{
		printf("\nnhap vao so : ");


		scanf("%d",&n);
		if (n < 0)
		{
			printf("\nso khong hop le xin hay nhap lai cam on !");
		}
	}
	while (n < 0);
	// dem so chu so 
	/*
	lay so do lien tuc chia cho 10 , moi lan chia thi tang bien dem len , chia cho den khi so do la 0 thi 
	dung lai 

	vd : so n = 1234
	1234 / 10 = 123 => dem = 1
	123/ 10 = 12 => dem = 2
	12 / 10 = 1 => dem = 3
	1 / 10 = 0 => dem = 4 */

	int dem ;
	int temp = n;
	/*while (temp != 0 )
	{
	temp = temp / 10;  // lien tuc chia n cho 10
	dem++;
	}*/

	if( n == 0 )
	{
		dem = 1;
	}
	else
	{
		dem = log10((double)n) + 1;
	}

	printf("\nso %d co %d chu so",n,dem);

	/*
	1234 lam cach nao de ra duoc chu so : 1 , 2 , 3, 4

	1234 % 10 = 4 (1)
	1234 / 10 = 123 
	123 % 10 = 3 ( 2)
	123 / 10 = 12 
	12 % 10 = 2 ( 3)
	12 / 10 = 1
	1 % 10 = 1 (4)
	1 / 10 = 0 dung lai */

	int max , min;

	max = min = temp % 10;//dat max  = min = so cuoi
	temp / 10; // bo so cuoi 

	while (temp != 0)
	{
		int chuso = temp % 10; // lay chu so cuoi 
		temp /= 10; // loai chu so cuoi 

		if (chuso > max)
		{
			max = chuso;
		}
		if (chuso < min)
		{
			min = chuso;
		}
	}
	
	printf("\nso cua max la %d",max);
	printf("\nso cua min la %d",min);

	/*
	5207
	b1 : max = min = 7
	5207 = 10 = 520

	520 % 10 = 0 => min = 0 , max = 7
	520 / 10 = 52 

	52 % 10 = 2 => min = 0 , max = 7
	52 / 10 = 5 

	5 % 10 = 5 => min = 0 , max = 7
	5 / 10 = o dung lai */

	/* neu khong dat max = mix = temp % 10 thi 
	dat max la chu so nho nhat de dam bao moi chu so khi lay ra luon > max 
	dat mix la chu so lon nhat de dam bao moi chu so khi lay ra luon < mix */

	

	


	



	
	


	getch();
	return 0;



}

 

Revise this Paste

Children: 118007
Your Name: Code Language: