[Courses] C Programming For Absolute Beginners, Lesson 4: Getting Looped
Leslie
leslie.brothers at verizon.net
Tue Mar 6 20:12:32 UTC 2012
On Mon, 2012-03-05 at 18:34 -0800, Carla Schroder wrote:
> HOMEWORK
>
> Modify the last example to display two columns of output that use the 'a' and
> 'total' variables. Play around with spacing and line breaks.
>
Yay for Jacinta! I wanted my columns to be aligned on the rightmost
digit, not the leftmost, but I couldn't figure out how to use the
conditionals (for adding an extra space before a one-digit number)
within the 'for-loop'. I had given up on that extra formatting goody
until I read her helpful hint about the curly braces! Now my table looks
better, and here's the code:
//total.c
#include <stdio.h>
int main ()
{
int a = 0;
int total = 0;
printf ("\n\n");
printf (" *******************************************\n");
printf (" * *\n");
printf (" * Two-column Table *\n");
printf (" * by Leslie *\n");
printf (" * *\n");
printf (" * *\n");
printf (" *******************************************\n\n");
printf(" \t\ta\t total\n");
printf (" ___________________________________________\n\n");
for (a = 1; a<=12; a=a+1)
{
total = total +a;
if (a < 10){
printf ("\t\t %i\t\t",a);
}
else {
printf ("\t\t%i\t\t",a);
}
if (total < 10){
printf (" %i\n",total);
}
else {
printf ("%i\n",total);
}
}
return 0;
}
-Leslie
More information about the Courses
mailing list