Lösungen zu Blatt 5 //Aufgabe 5.1 #include <stdio.h> #include <stdlib.h> int main() { int i,p,n; n=2; p=3; for(i=0;i<n;++i) p+=p; printf("p = %i",p); } -------------------------------------------------------------------------------------------------------------------------------------------------/Aufgabe 5.2 #include <stdio.h> #include <stdlib.h> int main() { int i; for(i=0;i<10;++i) printf("%i ",++i); } -------------------------------------------------------------------------------------------------------------------------------------------------//Aufgabe 5.3 #include <stdio.h> #include <stdlib.h> int main() { int i; for(i=0;i<13;++i) if (!(i%4)) printf("%i ",i+1); } -------------------------------------------------------------------------------------------------------------------------------------------------//Aufgabe 5.4 #include <stdio.h> #include <stdlib.h> int main() { int i; i=0; do { printf("%i ",i); i+=1; } while(i<=10); printf("\n\n"); for(i=0;i<=10;i++) printf("%i ", i); } //Aufgabe 5.6 #include <stdio.h> #include <stdlib.h> int main() { int x,y; for (x=1; x<6; x = x+1) { for (y=2; y<=4; y = y+1) { printf("Hello World! } printf("\n"); } return 0; } "); -------------------------------------------------------------------------------------------------------------------------------------------------//Aufgabe 5.7 #include <stdio.h> #include <stdlib.h> int main() { int i,erg,n; n=6; erg=0; for(i=1;i<=n;i=i+2) { erg=erg+i; } printf("\nAlle ungeraden Zahlen bis %i ergeben %i\n\n",n,erg); //dasselbe mit while-Schleife erg=0; i=1; while(i<=n) { erg=erg+i; i+=2; } printf("\nAlle ungeraden Zahlen bis %i ergeben %i\n\n",n,erg); return 0; } -------------------------------------------------------------------------------------------------------------------------------------------------//Aufgabe 5.8 # include <stdio.h> main () { int zeile , spalte ; for ( zeile =20; zeile >= 1; zeile =zeile -1) { for ( spalte =1; spalte <= zeile ; spalte = spalte +1) printf ("*"); printf ("\n"); } for (zeile=1;zeile<=20;zeile++) { for ( spalte =1; spalte <= zeile ; spalte = spalte +1) printf ("*"); printf ("\n"); } } //Aufgabe 5.9 #include <stdio.h> #include <stdlib.h> int main() { int i,n,erg; erg=1; printf("Bitte eine natuerliche Zahl eingeben: "); scanf("%i",&n); for(i=1;i<=n;i++) { erg=erg*i; } printf("\n\n%i\! = %i\n\n",n,erg); return 0; }