Introduction - A Look at Digital Computation

Week 3

TO PRINT NUMBERS FROM 1 TO 20

TO PRINT EVEN NUMBERS FROM 1 TO 20
2










/*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!!!“);
}
3










/*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! “);
}
4



















#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);
}
else
wilma = (a*b*c);
printf ("%d\n", wilma);
return (0);
}
5

TO AVERAGE ODD NUMBERS FROM 1 TO 20


















/*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);

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.



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.

The while statement permits the repetition of the statements
until the condition becomes false.







int n;
n = 1; /* initialization */
while (n <= 100) /* condition */
{
printf ("%d ", n); /* body */
n = n + 1; /* update */
}
 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

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.

TO PRINT INTEGER NUMBERS FROM 1 to 100 USING
WHILE

TO PRINT INTEGER NUMBER FROM 1 TO 100 USING FOR
LOOP
/* TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE*/
#include <stdio.h>
#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);
 }






















/*TO PRINT INTEGER NUMBER
FROM 1 TO 100 USING FOR LOOP*/
#include <stdio.h>
#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?

GET INTEGER NUMBERS AND ADD THEM DO THIS AS
LONG AS THE NUMBER ENTERED IS POSITIVE



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.
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.





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.










/* input validation loop */
int n;
do
{
printf ("Enter a number not between 1 and
5");
scanf ("%d", &n);
}while (n < 1 || n > 5);
/* rest of program */
...

TO PRINT INTEGER NUMBERS 1 THROUGH 100
USING DO WHILE LOOP.














/* TO PRINT INTEGER NUMBERS 1 THROUGH 100
USING DO WHILE LOOP
*/
#include <stdio.h>
#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?

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

/*For while-2 */

#include <stdio.h>

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);
}