Suppose we want to find the average salary of three employee(A,B,C) without disclosing there individual salary.In order to do this see the following algorithm:
ALGORITHM :
- First A will add a random number to it's salary and then tell the total sum to B.
- B will add its salary to the sum told by A and tell the new sum to C.
- C will also add its salary to the sum told by B and tell the new sum to A.
- A will now subtract its random number from the sum.
- Now we have a new number which is the total of all three employee salary.
- We will take average by dividing total buy 3.
PROGRAM :
int main(){
int a=10,b=20,c=30,avg=0;
int x=1;
//printf("Enter the first number:::");
//scanf("%d",&a);
//printf("Enter the second number:::");
//scanf("%d",&b);
//printf("Enter the first number:::");
//scanf("%d",&c);
a=x+a;
b=a+b;
c=b+c;
a=c-x;
avg=a/3;
printf("total sum ::: %d",a);
printf("\naverage ::: %d",avg);
return 1;
}
OUTPUT :
total sum ::: 60
average ::: 20
0 Comment(s)