The Next Number


General Statement:
A relatively simple task for humans to do is to identify the next number in an arithmetic sequence of numbers. For example, given the sequence of numbers 3, 6, and 9, we know that the next number in the sequence is 12 since each number is followed by a number increasing it by 3. Alternatively, given the sequence of numbers 3, 6, and 12, we know that the next number in the sequence is 24 since each number is followed by a number that doubles it.

Your task is to write a program that will deduce the next integer in a given sequence of three integers where the same arithmetic operation is applied to each integer to produce the next. The only arithmetic operations that will be used are addition, subtraction, multiplication, or division.

Input: The first line of the input contains an integer n that represents the number of data collections that follow where each data collection is on a single line. Each input line contains three integers with each pair of integers separated by a single space. The integers represent a sequence as described in the General Statement.

Output: Your program should produce n lines of output (one for each data collection). The output for each data collection should be the next integer in the corresponding input sequence.

The output is to be formatted exactly like that for the sample output given below.

Assumptions: The value of n will be between 1 and 100, inclusive.
There will be exactly one answer to each input sequence.
The answer will always be an integer (even if division is used).

Sample Input:
3
7 21 35
-10 20 -40
64 16 4

Sample Output:
49
80
1

#include <stdio.h>

int main ()
{
    int n;
    scanf ("%d", &n);

    while ( n-- ) {
        double x, y, z;

        scanf ("%lf %lf %lf", &x, &y, &z);

        if ( y - x == z - y )
            printf ("%0.lf\n", z + y - x);

        else
            printf ("%0.lf\n", z / (x / y));
    }

    return 0;
}

3 thoughts on “The Next Number

  1. Test Input:		
    9
    -2000 -1000 -500
    24 12 0
    -2 0 0
    1 5 25
    4 4 4
    25 -25 25
    7 -7 -21
    5 500 50000
    -54 18 -6
    
    Test Output:		
    -250
    -12
    0
    125
    4
    -25
    -35
    5000000
    2
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s