1 / 5

Exercise 2 : Using for loop

(1). FALSE. (2). TRUE. body. (3). Exercise 2 : Using for loop. Repetition (loop) control variable initialization Test Conditon Modification of control variable value order : (1) (2) body (3) (2) body (3) (2) body … body (3) (2) * Example. for ( (1); (2); (3) )

Download Presentation

Exercise 2 : Using for loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (1) FALSE (2) TRUE body (3) Exercise 2 : Using for loop • Repetition (loop) • control variable initialization • Test Conditon • Modification of control variable value order : (1) (2) body (3) (2) body (3) (2) body … body (3) (2) * Example for ( (1); (2); (3) ) { // for-repetitionbody . . . . . . . . // {} is not necessary // if there is only one statement in body } int main() { for(counter = 1; counter <= 10; counter++ ) printf(“%d\n”,counter); }

  2. 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 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000

  3. 2.(a) Given an integer number n as a user input, compute and print Sum(n)=1+2+3+…+n 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 <stdio.h> 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; } #include <stdio.h> int main() { }

  4. 3.(a) 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 3.(b) * * * * * * * * * * * * * * * #include <stdio.h> int main() { int i , j; for (i=1;i<=5;i++) { for (j=1;j<=i;j++) { printf("%d ",i); } printf("\n"); } return 0; } #include <stdio.h> int main() { }

  5. 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 <stdio.h> int main() { } Input example output example 85.5 92.57 75.45 98.2 80.2 Total Sum = 431.92 Average = 86.38

More Related