Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // exo1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <cmath>
- #define MAX 1000
- float discriminant(float a, float b, float c);
- int nmbreRacines(float a, float b, float c);
- int sommeDiviseursPropres(int n);
- int ami(int a, int b);
- void printNombreParfaits(int n);
- void printTriangle(int base);
- int estPremier(int n);
- int prochainPremier(int n);
- void printEntiersBizarres(void);
- int estEntierBizarre(int n);
- int _tmain(int argc, _TCHAR* argv[])
- {
- int a, b, c;
- printf("nombre : ");
- scanf("%d", &a);
- printEntiersBizarres(); // 153, 370, 371, 407
- printf("nombre : ");
- scanf("%d", &a);
- printf("%d est premier : %d\n", a, estPremier(a));
- printf("Prochain nombre premier de %d est : %d\n", a, prochainPremier(a));
- printf("Base du triangle : ");
- scanf("%d", &a);
- printTriangle(a);
- printf("first number : ");
- scanf("%d", &a);
- printf("second number : ");
- scanf("%d", &b);
- printf("Les nombres parfaits jusqu'a %d :\n", a);
- printNombreParfaits(a);
- printf("Somme des diviseurs propres de %d : %d\n", a, sommeDiviseursPropres(a));
- printf("Somme des diviseurs propres de %d : %d\n", b, sommeDiviseursPropres(b));
- printf("%d et %d sont amis : %d\n", a, b, ami(a, b));
- scanf("\n");
- printf("a : ");
- scanf("%d", &a);
- printf("b : ");
- scanf("%d", &b);
- printf("c : ");
- scanf("%d", &c);
- switch (nmbreRacines(a, b, c)) {
- case 2:
- printf("L'equation %dxx + %dx + %d admet deux solutions :\n", a, b, c);
- printf("x1 = (-%d -sqrt(discrim)) / (2%d)\t= %.2f\n", b, a, (-b-sqrt(discriminant(a, b, c))) / (2*a));
- printf("x1 = (-%d +sqrt(discrim)) / (2%d)\t= %.2f\n", b, a, (-b+sqrt(discriminant(a, b, c))) / (2*a));
- break;
- case 1:
- printf("L'equation %dxx + %dx + %d admet une racine double :\n", a, b, c);
- printf("x1 = x2 = -%d / (2%d)\t= %.2f\n", b, a, -b/(2.*a));
- break;
- default:
- printf("Il n'y a pas de solutions reelle a l'equation %dxx + %dx + %d\n", a, b, c);
- break;
- }
- scanf("\n");
- return 0;
- }
- int sommeDiviseursPropres(int n)
- {
- int i;
- int res = 0;
- for (int i = 1; i <= n / 2; ++i) {
- if ((n % i) == 0) {
- res += i;
- }
- }
- return res;
- }
- int ami(int a, int b)
- {
- if ((a == sommeDiviseursPropres(b)) && (b == sommeDiviseursPropres(a))) {
- return 1;
- }
- return 0;
- }
- float discriminant(float a, float b, float c)
- {
- return b*b - 4*a*c;
- }
- int nmbreRacines(float a, float b, float c)
- {
- float discrim = discriminant(a, b, c);
- if (discrim > 0.) {
- return 2;
- } else if (discrim == 0.) {
- return 1;
- } else {
- return 0;
- }
- }
- void printNombreParfaits(int n)
- {
- int i;
- for (i = 1; i <= n; ++i) {
- if (i == sommeDiviseursPropres(i)) {
- printf("%d\n", i);
- }
- }
- }
- void printTriangle(int base)
- {
- int nbEtoiles, nbEspaces, etage, i, j, newBase = base;
- if (newBase < 0) {
- newBase = -newBase;
- }
- if (newBase % 2) {
- ++newBase;
- }
- nbEtoiles = 1;
- nbEspaces = (newBase - nbEtoiles) / 2;
- for (etage = 1; etage <= newBase / 2; etage++) {
- for (i = 0; i < nbEspaces; ++i) {
- printf(" ");
- }
- for (i = 0; i < nbEtoiles; ++i) {
- printf("*");
- }
- for (i = 0; i < nbEspaces; ++i) {
- printf(" ");
- }
- printf("\n");
- nbEtoiles += 2;
- nbEspaces = (newBase - nbEtoiles) / 2;
- }
- }
- int estPremier(int n)
- {
- int i;
- if (n <= 1) {
- return false;
- }
- for (i = 2; i <= n / 2; ++i) {
- if ((n % i) == 0) {
- return false;
- }
- }
- return true;
- }
- int prochainPremier(int n)
- {
- int res, m = n;
- while (!(res = estPremier(m))) {
- ++m;
- }
- return m;
- }
- void printEntiersBizarres()
- {
- int i;
- for (i = 2; i <= MAX; ++i) {
- if (estEntierBizarre(i)) {
- printf("%d\n", i);
- }
- }
- }
- int estEntierBizarre(int n)
- {
- int unite, dizaine, centaine;
- unite = n % 10;
- dizaine = ((n - unite) / 10) % 10;
- centaine = ((((n - unite) / 10) - dizaine) / 10) % 10;
- if ((unite * unite * unite + dizaine * dizaine * dizaine + centaine * centaine * centaine) == n) {
- return 1;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement