The Program
/*****************************************
File: do_ten.c
Author: Unknown
Date: Unknown
An example of a C 'for' loop that
prints a message ten times.
*****************************************/
#include
int main()
{
int i ;
/* Repeat 10 times */
for (i = 0 ; i < 10 ; i++)
{
printf("I will not chew gum in class.\n") ;
}
return 0;
}
and the sample run.
linux1[97] % gcc -Wall -ansi do_ten.c
linux1[98] % a.out
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
I will not chew gum in class.
linux1[99] %
Adding a list of 10 numbers
The Program
/*****************************************
** File: add10.c
** Author: Sue Bogar
** Date: 3/3/97
** Section: 101
** SSN: 123-45-6789
** EMail: bogar@cs.umbc.edu
**
** This program adds a list of ten numbers,
** printing the total at the end. Instead
** of reading the numbers into separate
** variables, this program reads in each
** number and adds it to a running total.
*****************************************/
#include
int main()
{
int i, value, total;
/* Print description for user */
printf("This program adds a list of 10 numbers.\n");
/* Initialize total */
total = 0;
/* Get 10 integers from the user,
** adding each to total */
for (i = 0; i < 10; i++)
{
printf("Enter an integer : ");
scanf ("%d", &value);
total += value;
}
/* Print the result */
printf("The total is %d\n", total);
return 0;
}
and the sample run.
linux1[99] % gcc -Wall -ansi add10.c
linux1[100] % a.out
This program adds a list of 10 numbers.
Enter an integer : 1
Enter an integer : 2
Enter an integer : 3
Enter an integer : 8
Enter an integer : 21
Enter an integer : 22
Enter an integer : 1
Enter an integer : 17
Enter an integer : 19
Enter an integer : 20
The total is 114
linux1[101] %
Nested For Loops
Multiplication Tables
The Program
/*****************************************
* File: timestab.c
* Author: S Bogar
* Date: 8/3/99
* Section: 0101
* SSN: 123-45-6789
* E-Mail: bogar@cs.umbc.edu
*
* Generates a multiplication table where each
* axis runs from LowerLimit to UpperLimit.
*****************************************/
#include
/*********************
* Constants
* ---------
* LOWER -- Starting value for the table
* UPPER -- Final value for the table
*********************/
#define LOWER 1
#define UPPER 10
/* Main program */
int main()
{
int i, j;
for (i = LOWER; i <= UPPER; i++)
{
for (j = LOWER; j <= UPPER; j++)
{
printf(" %4d", i * j);
}
printf("\n");
}
return 0;
}
The use of #define for constants simplifies later changes.
Drawing a box
The Task
Draw a 9x5 box of *'s.
The Program
/*****************************************
Program: box2.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
SSN: 123-45-6789
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw another box.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 9 ; j++)
{
printf("*") ;
}
printf("\n") ;
}
return 0;
}
The Sample Run
*********
*********
*********
*********
*********
Diagonal
The Task
Draw a 5x5 box of *'s but with O's on the diagonal.
The Program
/*****************************************
Program: diag.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
SSN: 123-45-6789
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw
a diagonal of O's.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if (i == j)
{
printf("O") ;
}
else
{
printf("*") ;
}
}
printf("\n") ;
}
return 0;
}
The Sample Run
O****
*O***
**O**
***O*
****O
Triangle
The Task
Draw a triangle of *'s.
The Program
/*****************************************
Program: triangle.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
SSN: 123-45-6789
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw a triangle.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= i ; j++)
{
printf("*") ;
}
printf("\n") ;
}
return 0;
}
The Sample Run
*
**
***
****
*****
Another triangle
The Task
Flip the triangle.
The Program
/*****************************************
Program: triangle2.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
SSN: 123-45-6789
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw
a triangle facing the other way.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if ( i > 5 - j)
{
printf("*") ;
}
else
{
printf(" ") ;
}
}
printf("\n") ;
}
return 0;
}