Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.

Similar presentations


Presentation on theme: "Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2."— Presentation transcript:

1 Week 3

2  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2

3  /*TO PRINT NUMBERS FROM 1 TO 20 */  #include  main( ){  int i;  int limit=20;  for(i = 1; i <= limit; i=i+1){  printf(“%d “, i);  }  puts(“ STOPPPP!!!“);  } 3

4  /*TO PRINT EVEN NUMBERS FROM 1 TO 20 */  #include  main( ){  int i;  int limit=20;  for(i= 2; i<= limit; i=i+2){  printf(“%d “, i);  }  puts(“ EVEN NUMBER! “);  } 4

5 #include 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); } else wilma = (a*b*c); printf ("%d\n", wilma); return (0); } 5

6  TO AVERAGE ODD NUMBERS FROM 1 TO 20

7  /*TO AVERAGE ODD NUMBERS FROM 1 TO 20 */  #include  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);  }

8  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.

9  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.

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

11  int n;  n = 1; /* initialization */  while (n <= 100) /* condition */  {  printf ("%d ", n); /* body */  n = n + 1; /* update */  }

12  In a while loop, the initialization is performed once, then the condition is checked. If the condition is true, the body and update statements are executed and the condition is then checked again. If the condition is false, the loop ends and the program continues to the next statement. init Condition body update

13  The for loop is used when we know, A PRIORI, the number of iterations (counting loops).  The while loop is used when we don't.  This is a style issue only. We know that the for statement works exactly like the while statement.

14  TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE  TO PRINT INTEGER NUMBER FROM 1 TO 100 USING FOR LOOP

15  /* TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE*/  #include  #define NUMBER 100  main( ){  int counter= 1;  /*Print the numbers 1 through 100 */  while(counter <= NUMBER){  printf("Value of counter = %d\n", counter);  counter++;  } printf("The value of cnt after the while loop is over: %d\n", counter);  }

16  /*TO PRINT INTEGER NUMBER  FROM 1 TO 100 USING FOR LOOP*/  #include  #define NUMBER 100  main( ){  int counter;  /* Print the numbers 1 through 100 */  for(counter= 1; counter <= NUMBER; counter = counter + 1)  printf("Counter is %d. \n", counter);  printf(“The counter after the loop is finished: %d.\n”, counter);  }  Q) What is the difference between these 2 programs?

17  GET INTEGER NUMBERS AND ADD THEM DO THIS AS LONG AS THE NUMBER ENTERED IS POSITIVE

18  Use a loop to repeat steps in a program. two kinds of loops occur frequently in programming: counting loops and sentinel-controlled loops.  For a counting loop, the number of iterations required can be determined before the loop is entered.  For a sentinel-controlled loop, repetition continues until a special data value is scanned.

19 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.

20  Similar to while-loop  However,  The WHILE LOOP tests the loop condition at the top of the loop but  The do while loop tests the condition at the end of the loop  Therefore, regardless of the condition, the body of the DO WHILE LOOP is executed at least once.  General Syntax:  do{  statement;  …  }while (condition);  As long as the condition is true, the body of the loop will be repeated.

21  /* input validation loop */  int n;  do  {  printf ("Enter a number not between 1 and  5");  scanf ("%d", &n);  }while (n 5);  /* rest of program */ ...

22  TO PRINT INTEGER NUMBERS 1 THROUGH 100 USING DO WHILE LOOP.

23 /* TO PRINT INTEGER NUMBERS 1 THROUGH 100 USING DO WHILE LOOP */ #include #define NUMBER 100 main( ){ int counter=1; /*Print numbers until number is greater than 100*/ do{ printf("counter is %d\n", counter); counter++; }while(counter <= NUMBER); printf("The value of the counter after the loop is over: %d.\n", counter); } Q) What is the value of counter after the loop is over?

24  In a do-while loop, the initialization is performed once, then the body and update statements are executed, and finally the condition is checked. If the condition is true, the body and update are executed once more. If the condition is false, the loop ends and the program continues to the next statement. init Body update condition

25 /*For while-2 */ #include void main(void) { int i=4, j=1; do { printf("old_i=%2d, ",i); i--; printf("new_i=%2d\n",i); } while(i>0); do ++j; while(j>999); printf("j=%2d\n",j); }


Download ppt "Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2."

Similar presentations


Ads by Google