Linguaggio C: numeri poligonalil - Studentville

Linguaggio C: numeri poligonalil

esercizio svolto o teoria

A cura di: Administrator

Download sorgente

  /*   * Il seguente programma stampa h numeri n-poligonali   * (Cfr. http://mathworld.wolfram.com/PolygonalNumber.html )   */  #include <stdio.h>  unsigned int polynum(unsigned int, unsigned int);  int  main(void)  {  	unsigned int side = 2, numbers, i;  	/* read polygonal side, how much numbers to print */  	while (side < 3) {  		printf("Enter the polygonal number ( > 2): ");  		scanf("%u", &side);  		if (side < 3)  			printf("Invalid value!n");  	}  	printf("How much numbers? ");  	scanf("%u", &numbers);  	for (i = 1; i <= numbers; i++)  		printf("%u, ", polynum(side, i));  	if (numbers > 0)  		printf("...n");  	return 0;  }  unsigned int polynum(unsigned int n, unsigned int h)  {  	if (n < 3)  		return 0;  	return (n - 2) * (h - 1) * h / 2 + h;  }  
  • Programmazione

Ti potrebbe interessare

Link copiato negli appunti