Google

Wednesday, February 3, 2010

CSC202 : Exercise on Functions

1. Write a program to input two numbers and print the sum,
product and difference of the two numbers.

2. Rewrite your program in Q. 1 above in modular form.


Solution for Q. 2:

#include "stdio.h"

//---- Function Prototypes -----
int add(int s, int t);
int multiply(int s, int t);
int diff(int s, int t);

void main()
{
int a, b, sum, product, difference;
printf("enter 2 integers: ");
scanf("%d%d",&a,&b);

sum = add(a, b);
product = multiply(a,b);
difference = diff(a,b);

printf("sum = %d\nproduct= %d\ndifference = %d\n",sum,product,difference);
}

//----- Function Definitions -----
int add(int s, int t)
{
int total;
total = s + t;
return total;
}

int multiply(int s, int t)
{
return s*t;
}

int diff(int s, int t)
{
return s - t;
}