2; hour--) { minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }"> 2; hour--) { minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6 CPS125.

Similar presentations


Presentation on theme: "Week 6 CPS125."— Presentation transcript:

1 Week 6 CPS125

2 /*For Lesson for-1 */ #include <stdio.h> void main(void) { int day, hour, minutes; for(day=1; day<=3; day++) printf("Day=%2d\n", day); for (hour=5; hour>2; hour--) { minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }

3 /*For Lesson for-2 */ #include <stdio.h> void main(void) { int i, j, k, m=0; for (i=1; i<=5; i+=2) { for (j=1; j<=4; j++) k = i+j; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } m=k+i; printf("i=%3d, j=%3d, k=%3d, m=%3d \n", i, j, k,m);

4 HOME WORK!!!!!!!!! EXTRA PRACTICE!
/*For Lesson for-3 */ #include <stdio.h> void main(void) { int i, isum1=0, isum2=0, N=9; float f, x, sum1=0.0, sum2=0.0; /*What is the result if we use double type*/ x=1.0/N; printf("x=%f\n",x); for (i=1;i<=N;i++) {sum1 +=x; isum1+=1;} printf("sum1=%f, isum1=%d\n",sum1, isum1); for (f=x;f<=1.0;f+=x) {sum2+=x; isum2+=1;} printf("sum2=%f, isum2=%d\n",sum2, isum2); }

5 Write a Program!!!!!!! TO PRINT NUMBERS FROM 1 TO 20
TO PRINT EVEN NUMBERS FROM 1 TO 20

6 /* PROGRAM # 24 */ /*TO PRINT NUMBERS FROM 1 TO 20 */
#include<stdio.h> main( ){ int i; int limit=20; for(i = 1; i <= limit; i=i+1){ printf(“%d “, i); } puts(“ STOPPPP!!!“);

7 /* PROGRAM # 25 */ /*TO PRINT EVEN NUMBERS FROM 1 TO 20 */
#include<stdio.h> main( ){ int i; int limit=20; for(i= 2; i<= limit; i=i+2){ printf(“%d “, i); } puts(“ EVEN NUMBER! “);

8 Nested if … else #include <stdio.h> int main (void) {
int wilma, a, b,c; scanf("%d%d%d", &a,&b,&c); if (a > b&& a>c){ wilma = (a - b*c); printf ("%d\n", wilma);} else if (b>a||b>c){ wilma = (b); printf ("%d\n", wilma); } wilma = (a*b*c); return (0);

9 Write a Program!!!!!!! TO AVERAGE ODD NUMBERS FROM 1 TO 20

10 /* PROGRAM # 26 */ /*TO AVERAGE ODD NUMBERS FROM 1 TO 20 */
#include<stdio.h> main( ){ /* Declare the variables */ int i; float ave; int limit=20, sum=0, count=0; /* Initialize variables */ /* Print odd numbers 1 through 20 */ for(i= 1; i< limit; i=i+2){ printf(“%d “, i); sum+=i; count++; } /* Calculate average print the result */ ave=(float)sum/count; printf(“The average is %f.\n”, ave);

11 HOME WORK!! TO PRINT MULTIPLES OF 3 FROM 1 TO 99 IN A MATRIX FORMAT HAVING 5 COLUMNS TO PRINT MULTIPLES OF 3 FROM 1 TO 99 IN A MATRIX FORMAT HAVING 4 COLUMNS.

12 The while Loop condition is true */ Another type of looping structure
Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.

13 The while statement The while statement permits the repetition of the statements until the condition becomes false.


Download ppt "Week 6 CPS125."

Similar presentations


Ads by Google