Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1) (2) body (3) (2) body (3) (2) body … body (3) (2) * Example for ( (1); (2); (3) ) { // for-repetition body // {} is not necessary // if there is only one statement in body } (2) body TRUE FALSE (1) (3) int main() { for(counter = 1; counter <= 10; counter++ ) printf(“%d\n”,counter); }
1. Write a program that calculates the squares and cubes of the numbers from 0 to 10 and prints the following table of values. (Be careful of the alignment.) number square cube
#include int main() { int n, i; int sum=0; scanf("%d",&n); for (i=1;i<=n;i++) { sum+=i; } printf("%d\n",sum); return 0; } 2.(a) Given an integer number n as a user input, compute and print Sum(n)=1+2+3+…+n #include int main() { } 2.(b) Given an integer number n as a user input, compute and print the product of odd numbers between 1 and n. Assume n is less than 20. Result : 1*3*…*n (if n is odd) 1*3*…*n-1 (if n is even)
#include int main() { int i, j; for (i=1;i<=5;i++) { for (j=1;j<=i;j++) { printf("%d ",i); } printf("\n"); } return 0; } 3.(a) #include int main() { } 3.(b) * * * * * * * * * * * * * * *
4. Write a C program that calculates and prints the total sum and average of 5 floating point values that are taken as user input. #include int main() { } Input example Total Sum = Average = output example