Exercice langage C corrigé programmes plus complexes

1)

/* Affichage de la table ASCII */
#include 

int main()
{
	int car;

	for (car=0; car<32; car++)
	{
		printf("%3d - \'%c\'", car+32, car+32);
		printf("%3d - \'%c\'", car+64, car+64);
		printf("%3d - \'%c\'\n", car+96, car+96);
	}
	return 0;
}

2)

/* Resolution d'une equation du second degre */
#include 
#include 

int main()
{
	double a, b, c, d;
	printf(" a: "); scanf("%lf", &a);
	printf(" b: "); scanf("%lf", &b);
	printf(" c: "); scanf("%lf", &c);

	if (a==0.0)
	{
		if (b==0.0)
		{
			if (c==0.0)
			{
				printf("Infinite de solutions\n");
			}
			else
			{
				printf("Pas de solutions\n");
			}
		}
		else
		{
			printf("Racine unique : %f\n", -c/b);
		}
	}
	else
	{
		d = b*b - 4*a*c;
		if (d==0.0)
		{
			printf("Racine unique : %f\n", -b/2*a);
		}
		else if (d>0.0)
		{
			printf("Deux racines reelles :\n");
			printf("x1 = %f\n", (-b + sqrt(d))/2*a);
			printf("x2 = %f\n", (-b - sqrt(d))/2*a);
		}
		else /* d<0.0 */
		{
			printf("Deux racines complexes :\n");
			printf("x1 = %f + i x %f\n", -b/2*a,  sqrt(-d)/2*a);
			printf("x2 = %f - i x %f\n", -b/2*a, -sqrt(-d)/2*a);
		}
	}
	return 0;
}

 

/* Calcul du PGCD de deux entiers
 * Methode des soustractions successives
 * Par convention, pgcd(x,0) = x */

#include 

int main()
{
	int a, b;
	printf(" a: "); scanf("%d", &a);
	printf(" b: "); scanf("%d", &b);

	printf("pgcd(%d,%d) = ", a, b);
	if (a == 0)
	{
		printf("%d\n", b);
	}
	else if (b == 0) 
	{
		printf("%d\n", a);
	}
	else 
	{
		while (a != b)
		{
			if (a > b)
			{
				a = a - b;
			}
			else
			{
				b = b - a;
			}
		}

		printf("%d\n", a);
	}

	return 0;
}

3) b

/* Calcul du PGCD de deux entiers
 * Methode des modulos successifs
 * Par convention, pgcd(x,0) = x */

#include 

int main()
{
	int a, b;
	printf(" a: "); scanf("%d", &a);
	printf(" b: "); scanf("%d", &b);

	printf("pgcd(%d,%d) = ", a, b);
	if (a == 0)
	{
		printf("%d\n", b);
	}
	else if (b == 0) 
	{
		printf("%d\n", a);
	}
	else 
	{
		while (a != 0 && b != 0)
		{
			if (a >= b)
			{
				a = a % b;
			}
			else
			{
				b = b % a;
			}
		}

		if (b == 0)
		{
			printf("%d\n", a);
		}
		else
		{
			printf("%d\n", b);
		}
	}

	return 0;
}

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *