[Courses] Answers to the exercises for Lesson 7
anca14 at anca14.ro
anca14 at anca14.ro
Mon Nov 4 13:54:29 EST 2002
heya ;)
here are my answers to the exercises for Lesson Seven.
=============================================================================
/*
1] Print a checkerboard (5x5 grid). Each square should
be five
by three characters wide.
*/
//anca14
#include <stdio.h>
int main(void)
{
horiz();
vert();
horiz();
vert();
horiz();
vert();
horiz();
vert();
horiz();
vert();
horiz();
printf("\n");
return 0;
}
int horiz()
{
printf("\n+-----+-----+-----+-----+-----+");
}
int vert()
{
int i=0;
for (i;i<3;i++)
printf("\n| | | | | |");
}
*********************
OUTPUT:
*********************
forevah# gcc -o 1 ex1-ls7.c
forevah# ./1
+-----+-----+-----+-----+-----+
| | | | | |
| | | | | |
| | | | | |
+-----+-----+-----+-----+-----+
| | | | | |
| | | | | |
| | | | | |
+-----+-----+-----+-----+-----+
| | | | | |
| | | | | |
| | | | | |
+-----+-----+-----+-----+-----+
| | | | | |
| | | | | |
| | | | | |
+-----+-----+-----+-----+-----+
| | | | | |
| | | | | |
| | | | | |
+-----+-----+-----+-----+-----+
==============================================================================
/*
2] Write a program to average n numbers.
*/
//anca14
#include <stdio.h>
int main(void)
{
int i=0,x=0,n;
float avg;
char buf[10];
printf("n= ");
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&n);
for(i;i<=n;i++)
x+=i;
avg=x/n;
printf("avg: %.2f\n",avg);
return 0;
}
**********************
OUTPUT:
**********************
forevah# gcc -o 2 ex2-ls7.c
forevah# ./2
n= 50
avg: 25.00
==============================================================================
/*
3] Write a program to print out the multiplication
table.
*/
//anca14
#include <stdio.h>
int main (void)
{
int i;
for (i=1;i<=10;i++)
printf("1 x %2d = %2d\t 2 x %2d = %2d\t 3 x %2d =
%2d\n",i,1*i,i,2*i,i,3*i);
printf("\n");
for (i=1;i<=10;i++)
printf("4 x %2d = %2d\t 5 x %2d = %2d\t 6 x %2d =
%2d\n",i,4*i,i,5*i,i,6*i);
printf("\n");
for (i=1;i<=10;i++)
printf("7 x %2d = %2d\t 8 x %2d = %2d\t 9 x %2d =
%2d\n",i,7*i,i,8*i,i,9*i);
printf("\n");
return 0;
}
***********************
OUTPUT:
***********************
forevah# gcc -o 3 ex3-ls7.c
forevah# ./3
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30
4 x 1 = 4 5 x 1 = 5 6 x 1 = 6
4 x 2 = 8 5 x 2 = 10 6 x 2 = 12
4 x 3 = 12 5 x 3 = 15 6 x 3 = 18
4 x 4 = 16 5 x 4 = 20 6 x 4 = 24
4 x 5 = 20 5 x 5 = 25 6 x 5 = 30
4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
4 x 7 = 28 5 x 7 = 35 6 x 7 = 42
4 x 8 = 32 5 x 8 = 40 6 x 8 = 48
4 x 9 = 36 5 x 9 = 45 6 x 9 = 54
4 x 10 = 40 5 x 10 = 50 6 x 10 = 60
7 x 1 = 7 8 x 1 = 8 9 x 1 = 9
7 x 2 = 14 8 x 2 = 16 9 x 2 = 18
7 x 3 = 21 8 x 3 = 24 9 x 3 = 27
7 x 4 = 28 8 x 4 = 32 9 x 4 = 36
7 x 5 = 35 8 x 5 = 40 9 x 5 = 45
7 x 6 = 42 8 x 6 = 48 9 x 6 = 54
7 x 7 = 49 8 x 7 = 56 9 x 7 = 63
7 x 8 = 56 8 x 8 = 64 9 x 8 = 72
7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
7 x 10 = 70 8 x 10 = 80 9 x 10 = 90
===============================================================================
/*
4] Write a program that reads a character and prints
out if it is
a vowel or a consonant.
*/
//anca14
#include <stdio.h>
#define OMG "\n%c is a vowel\n\n"
int main (void)
{
char ch, buf[2];
printf("character: ");
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%c",&ch);
switch (ch)
{
case 'A':
case 'a': printf(OMG,ch);
break;
case 'E':
case 'e': printf(OMG,ch);
break;
case 'I':
case 'i': printf(OMG,ch);
break;
case 'O':
case 'o': printf(OMG,ch);
break;
case 'U':
case 'u': printf(OMG,ch);
break;
default: printf("\n%c is a consonant\n\n",ch);
break;
}
return 0;
}
**********************
OUTPUT:
**********************
forevah# gcc -o 4 ex4-ls7.c
forevah# ./4
character: a
a is a vowel
forevah# ./4
character: w
w is a consonant
==============================================================================
/*
5] Write a program that converts numbers to words, for
example, 431
results in "four three one".
*/
//anca14
#include <stdio.h>
int main (void)
{
char buf[10],num;
int i;
printf("number: ");
fgets(buf,sizeof(buf),stdin);
printf("\n");
for(i=0;i<10;i++)
{
num=buf[i];
switch(num)
{
case '0': printf("zero ");
break;
case '1': printf("one ");
break;
case '2': printf("two ");
break;
case '3': printf("three ");
break;
case '4': printf("four ");
break;
case '5': printf("five ");
break;
case '6': printf("six ");
break;
case '7': printf("seven ");
break;
case '8': printf("eight ");
break;
case '9': printf("nine ");
break;
default: break;
}
}
printf("\n\n");
return 0;
}
************************
OUTPUT:
************************
forevah# gcc -o 5 ex5-ls7.c
forevah# ./5
number: 734623
seven three four six two three
===========================================================================
peace :)
Anca M. Holban
More information about the Courses
mailing list