Make your own free website on Tripod.com

programming with c language (4)

/* Write a C program that allows the user to insert a number
 from the keyboard. It then makes a call-by-reference to the
 function long int factorial()
 which calculates its factorial.
 Use recursion to solve this problem
*/
#include<stdio.h>
long int factorial(long int *);

main(){
 int num=0;
 printf("Enter a number to calculate the factorial:");
 scanf("%d",&num);
 printf("=>%ld\n",factorial(&num));
 return 0;
}

long int factorial(long int *n){
 long int digit = *n;
 if(digit==0) return 1;
 printf("->%ld\n",*n);

 --digit;
 *n=*n * factorial(&digit);
 return *n;
}

/* difference in accepting a string using the
 command scanf in two different formats
 and
 command gets
*/
#include<stdio.h>

main(){
 char str1[80], str2[80], str3[80];
 printf("Enter a string:");
 scanf("%s",str1);
 fflush(stdin);
 printf("Enter 2nd string:");
 scanf("%[^\n]",str2);
 fflush(stdin);
 printf("Enter 3rd string:");
 gets(str3);

 printf("First string: ");
 puts(str1);
 printf("Second string: %s\n", str2);
 printf("Third string: %s\n", str3);
 return 0;
}

/*
 Eccept positive number. This number is passed to a
 function convert() so that it converts the number input
 into words.
 e.g. if 11230 is input, the output should be:
 "Eleven Thousand Two Hundred Thirty"
*/
#include<stdio.h>
char array[10];

void hundreds(char);
void tens(char);
void teens(char);
void number(char);
main(){
 char ch;
 int i, position, cnt=0;
 printf("Please enter an integer:");
 while((ch=getchar()) != '\n'){
  array[cnt++]=ch;
 }
 position=cnt;
 for(i=0;i<cnt;i++){
  if(position==1)
   number(array[i]);
  if(position==2 && array[i]=='1'){
   i++;
   if(array[i]=='0') printf("Ten ");
   teens(array[i]);
  }
  else if(position==2){
   tens(array[i]);
  }
  else if(position==3)
   hundreds(array[i]);

  position--;
 }
 putchar('\n');
 return 0;
}

void number(char x){
 switch(x){
 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;
 }
}

void tens(char x){
 switch(x){
 //case '1':printf("Ten ");break;
 case '2':printf("Twenty ");break;
 case '3':printf("Thirty ");break;
 case '4':printf("Fourty ");break;
 case '5':printf("Fifty ");break;
 case '6':printf("Sixty ");break;
 case '7':printf("Seventy ");break;
 case '8':printf("Eighty ");break;
 case '9':printf("Ninety ");break;
 }
}

void teens(char x){
 switch(x){
 case '1':printf("Eleven ");break;
 case '2':printf("Twelve ");break;
 case '3':printf("Thirteen ");break;
 case '4':printf("Fourteen ");break;
 case '5':printf("Fifteen ");break;
 case '6':printf("Sixteen ");break;
 case '7':printf("Seventeen ");break;
 case '8':printf("Eighteen ");break;
 case '9':printf("Nineteen ");break;
 }
}

void hundreds(char x){
 if(x !='0'){
  number(x);
  printf("Hundred ");
 }
}

/* using pointer notations
 program to accept a maximum of 35 characters and display
 an appropriate message if the maximum is exceeded.
 The program also prints the string to terminal so that
 each word appears on a separate line.
*/
#include<stdio.h>

main(){
 char word[36];
 char ch;
 char *ptr;
 int i=0,counter=0;
 ptr=word;
 while((ch=getchar()) != '\n'){
  *ptr=ch;
  ptr++;
  counter++;
  /*word[counter++]=ch;*/
  if(counter>36){
   break;
  }
 }
 *ptr='\0';
 if(counter>36){
  printf("Cannot exceed 35 characters!!\n");
 }
 else{/*
  while(i<counter){
   if(word[i]!=' ')
    putchar(word[i]);
   else
    putchar('\n');
   i++;
  }*/
  ptr=word;
  while(*ptr!='\0'){
   if(*ptr == ' ')
    putchar('\n');
   else
    putchar(*ptr);
   ptr++;
  }
 }
 return 0;
}

/* accessing the structure through
 dot and pointer
*/
#include<stdio.h>

struct tag{
 int acct;
 char type;
}cust, *ptr;
main(){
 ptr=&cust;
 cust.acct=101;
 cust.type='A';
 printf("%d %c\n",cust.acct,cust.type);
 printf("%d %c\n",ptr->acct,ptr->type);
 return 0;
}

Click here to go back to 'C' programming
If you have comments or suggestions, email me at nicholas@tm.net.my

This page created with Netscape Navigator Gold (17/9/1999)