Learn Programming through Logical Thinking Series – 3

Learn Programming through Logical Thinking Series – 3

This blog explains about Learn Programming through Logical Thinking Series – 3 and is given below : 

_______________________________________________________________________________

                                        This is Learn Programming through Logical Thinking Series – 3.  In this post, we are going to discuss about a problem related to Ages.

Question:

Five years ago the average of A,B,C,D was 45 years.  With E joining them now the average of all five becomes 49 years.  What is the age of E?

SOLUTION :

 The average age of  A , B ,C & D before 5 years = 45 * 4 = 90 years                                                     

After 5 years their average age of A , B , C & D will be  50 years. 

So the average age of A , B , C ,D & E = 200 + x = 49 * 5

                                                                                     = 245 

                        =>    X = 245 – 200 = 45 years

                                      The age of E  = 45 years 

Now, shall we focus on deriving algorithm for resolving this problem?

Algorithm

  1. Get the average of 5 years before
  2. Multiply average with 4 to get the year
  3. Multiply 5&4 and add he value with total years
  4. Multiply 49 &5 get the E present age
  5. Now subtract 200 from E present age

PROGRAM

package com.java.learning;

public class AgeProblem{

public static void main(String[] args)

{

int avgOfFiveInYears =45;

int totalAge=avgOfFiveInYears*4;

int preAge=5*4;

int presentAge=totalAge+preAge;

int ePreAge=49*5;

int ePresentAge=epreAge-presentAge;

System.out.print(epresentAge);

}

}

Read These Also:

  1. Learn Programming Through Logical Thinking Series – 1

  2. Learn Programming Through Logical Thinking Series – 2