Computer Concept & Programming In C - Unit IV - 4

Q.7 Draw flow chart and write a program in C to print the multiplication of two matrices A and B of size N x N.        (2009-10)
Ans. The program for matrix multiplication is already told before. Put r1= c1 = r2 = c2 = N.
The flow chart for matrix multiplication is shown as follows :



Q.8 A 5-digit positive integer is entered through the keyboard. Write a 'C' function to calculate sum of the 5-digit number
1. Without using recursion
2. Using recursion       */                                          (AKTU. 2008 - 09)
Ans.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define size 5
int sum(char *string);            /* Without using recursion     */
int rec_sum(char *string, int);   /* Using recursion             */
int total = 0;                    /* for storing the value of sum of digits in the recursive
    routine  */
void main()
{
  int i, flag = 0,choice;
  char number[size];
  clrscr();

  /* initializing the array elements to 0  */

  printf("\nEnter the number\n");
  gets(number);

  /*Checking for a valid number*/
  for(i = 0; i < size; i++)
    if(!(isdigit(number[i])))
      {
flag = 1;
break;
      }
  if(flag == 1)
    printf("\nInvalid 5 digit number");
  else
    {
      printf("\n Press 1 to sum digits of the number");
      printf("\n without using recursion");
      printf("\n Press 2 to sum digits of the number");
      printf("\n using recursion");
      scanf("%d",&choice);
      switch(choice)
      {
case 1 :
 printf("\nThe sum of the 5 digits of the number without recursion is:\n");
 printf("%d",sum(number));
 break;
case 2:
 printf("\nThe sum of the 5 digits of the number with recursion is:\n");
 printf("%d",rec_sum(number, size));
 break;

default:
 printf("\nWrong Choice");
 break;
      }
}
printf("\nPress any key to exit.");
getch();
}
int sum(char a[])
{
  int s = 0,i;
  for(i = 0; i < size; i++)
     s += a[i] -'0';
  return s;
}

int rec_sum(char a[], int j)
{
 j--;
 total += a[j] -'0';
 if(j > 0)
  rec_sum( a, j);
 else
 return total;
}