[Solved] Lucky Pairs with Java, C++, Python

Richie and Riya are participating in a game called “Lucky pairs” at the Annual Game Fair in their Company. As per the rules of the contest, two members form a team and Richie initially has the number A and Riya has the number B.
There are a total of N turns in the game, and Richie and Riya alternatively take turns. In each turn the player whose turn it is, multiplies his or her number by 2. Richie has the first turn. Suppose after the entire N turns, Richie’s number has become C and Riya’s number has become D. The final score of the team will be the sum of the scores (C+D) of both the players after N turns.

Write a program to facilitate the quiz organizers to find the final scores of the teams.

Input Format:
The only line of input contains 3 integers AB, and N.

Output Format:
Output a single line which contains the integer that gives the final score of the team which will be the sum of the scores of both the players after N turns.
Refer sample input and output for formatting specifications.

Sample Input 1:
1 2 1

Sample Output 1:
4

Sample Input 2:
3 2 3

Sample Output 2:
16

Solution

import java.util.*;
class Main
{
    public static void main(String jr[])
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int n = sc.nextInt();
        for(int i=0;i<n;i++)
        {
            //sauravhathi
            if((i%2)==0)
            a=a*2;
            else
            b=b*2;
        }
        System.out.println(a+b); 
           
         
        
        
    }
}
include<stdio.h>
int main()
{
    int A, B, N, G, i
    scanf("%d%d%d", & A, & B, & N)
    for(i=0
        i < N
        i++)
    {
        if((i % 2) == 0)
        A = A*2
        else
        B = B*2
    }
    G = A+B
    printf("%d", G)
    return(0)
}
a = list(map(int, input().split()))
for i in range(a[2]):
    if i % 2 == 0:
        a[0] *= 2
    else:
        a[1] *= 2
print(a[0] + a[1])

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *