[Courses] Re: anca14

Bidea Cristian phaser_programmer at yahoo.com
Tue Nov 5 01:26:41 EST 2002


Problem #3  
I don't understand why you did this so complicated! It's very simple...

#include <stdio.h>

int main(void)
{
  int i,j;
  for (i = 1; i<=9; i += 3)
    {
      for (j = 1; j<=10; j++)
      printf("%2d x %2d = %2d %2d x %2d = %2d %2d x %2d = %2d\n",
                    i   ,j    ,i*j,i+1,   j,(i+1)*j,i+2,  j, (i+2)*j);
      printf("\n");
    }

}

The ideea is that you generate 3 or 4 multiplication tables at a time. So now the program is simple without complications. If you have questions you can give a message. I'll be glad to answer all your questions :) or you can ask me face to face (la un pahar cu bere :P )

Problem #5

#include <stdio.h>
typedef char *string;
string a[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int i, n, j;
int stack[20];

int main(void)
{
  printf("Please give a number: ");
  scanf("%d", &n);
  j = 0;
  while (n>0)
   {
     i = n % 10;
     stack[j] = i; j++;
     n = (n - i) / 10;
   }
  for (i=j-1; i>=0; i--)
    printf("%s ", a[stack[i]]);
  return 0;
}

Ok! this is with numbers! I put this solution because it may help you understand how to find out the digits of an integer number. Your solution is good but you can improve it by making a conversion from character to integer using the ascii code of the character. The characters 1, 2, 3 .. 9 have an ascii code. Let's say that the ascii code of 0 is n. That means that 1 = ascii_code(1)-n, 2 = ascii_code(2)-n, 3 = ascii_code(3) - n... and so on. 
OK! Peace my friend :) 
anca14 at anca14.ro wrote: 

/*
3] Write a program to print out the multiplication 
table.
*/

//anca14 

#include 
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


/*
5] Write a program that converts numbers to words, for
example, 431
results in "four three one".
*/

//anca14

#include 

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



---------------------------------
Do you Yahoo!?
HotJobs - Search new jobs daily now-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://linuxchix.org/pipermail/courses/attachments/20021105/ed515819/attachment.xhtml


More information about the Courses mailing list