Vous êtes sur la page 1sur 5

/* Calculating the Sum of Even Integers Feb 17 2014 Johnny L Dao CIS 130 excersise 4.12 */ #include <stdio.

h> #include <stdlib.h> int main(int argc, char *argv[]) { int i; // designating counter int sum = 0; // assigning sum and initilize sum // loop using for statements for (i = 2; i <= 30; i += 2) { sum += i; // adding i to sum } // end for statement printf( "Sum of the even integers from 2 to 30 is: %d\n\n", sum); // Print Goodbye! printf( "Goodbye!\n"); getchar(); getchar(); return 0; } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* Print Triangles Feb 20 2014 Johnny L Dao CIS 130 excersise 4.16 */ #include <stdio.h> #include <stdlib.h> int main(void) { int row; // row counter int col; // column counter int space; // spaces counter // begin triangle A for ( col = 1; row <= 10; row++ ) {

//print row asterisks for ( col = 1; col <= row; col++ ) { printf( "*" ); } printf( "\n" ); } // end for triangle A printf( "\n" ); // begin triangle B codes for ( row = 10; row >=1; row--) { // begin for for ( col = 1; col <= row; col++) { printf ( "*" ); } // end for printf ( "\n" ); } //end for triangle b printf( "\n" ); // begin triangle C for ( row = 10; row >= 1; row--) { // print 10 - row for (space = 1; space <= 10 - row; space++) { printf ( " " ); } //end for // print fow asterisk for ( col = 1; col <= row; col++) { printf( "*" ); } // end for printf( "\n" ); } // end triangle b printf( "\n" ); // begin triangle D for ( row = 1; row <= 10; row++ ) { // print 10 - row for ( space = 1; space <= 10 - row; space++) { printf( " " ); } // end for

for ( col = 1; col <= row; col++ ) { //print asterisk printf( "*" ); }// end for printf( "\n" ); } // end for printf( "\n"); getchar(); getchar(); return 0; }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++ /* Calculating the value of Pi Feb 25 2014 Johnny L Dao CIS 130 excersise 4.26 */ #include <stdio.h> #include <stdlib.h> int main (void) { int x; int i; float sum = 0; double Pi; printf("Please enter the terms of that you want to calculate: >> \n "); scanf("%d", &x); for (i=1; i<x; i+=2) { if (i%4==1) { sum=sum+1./(double)i; } else sum=sum-1./(double)i; Pi = 4*sum; }

printf(" \n Approximate value of PI is %f\n", Pi); getchar(); getchar(); return 0; }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++ /* Johnny Dao / Print Diamond Shape Feb 25 2004 CIS 130 / Excercise 4.31 Extra Credit */ #include <stdio.h> #include <stdlib.h> int main() { int n, c, k, blank = 1; printf("Enter number of rows\n"); scanf("%d", &n); blank = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= blank; c++) printf(" "); blank--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } blank = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= blank; c++) printf(" "); blank++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n");

} getchar(); getchar(); return 0; } ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++