The Block Game | CodeChef Solution

Hello coders, today we are going to solve The Block Game CodeChef Solution whose Problem Code is PALL01.

The Block Game

Task

The citizens of Byteland regularly play a game. They have blocks each denoting some integer from 0 to 9. These are arranged together in a random manner without seeing to form different numbers keeping in mind that the first block is never a 0. Once they form a number they read in the reverse order to check if the number and its reverse is the same. If both are same then the player wins. We call such numbers palindrome.

Ash happens to see this game and wants to simulate the same in the computer. As the first step he wants to take an input from the user and check if the number is a palindrome and declare if the user wins or not. 

Input Format

The first line of the input contains T, the number of test cases. This is followed by T lines containing an integer N.

Output Format

For each input output “wins” if the number is a palindrome and “loses” if not, in a new line.

Constraints

  • 1<=T<=20
  • 1<=N<=20000

Sample Input

3
331
666
343

Sample Output

loses
wins
wins

Solution – The Block Game | CodeChef Solution

C

#include <stdio.h>

int main(void)
{
    int t,n,i,r,rev;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n);
        int n1=n;
        rev=0;
        while(n!=0)
        {
            r=n%10;
            rev=rev*10+r;
            n=n/10;
        }
        if(rev==n1){
           printf("wins\n");
        }
        else{
           printf("loses\n");
        }
    }
	return 0;
}

C++

#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
            int n , rem =0, a;
            cin>>n;
            a = n;
            while(a>0){
                rem = rem*10 + a%10;
                a = a/10;
            }

            if(rem == n){
                cout<<"wins"<<endl;
            }else{

                cout<<"loses"<<endl;

           }
	}
	return 0;
}

Python

#Solution Provided by Sloth Coders
for i in range(int(input())):
    n = input()
    if n == n[::-1]:
        print("wins")
    else:
        print("loses")

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
    	int t = sc.nextInt();
    	for(int i=0; i<t; i++) {
    		int n = sc.nextInt();
    		int temp = n;
    		int rev = 0;
    		while(temp>0) {
    			int rem = temp%10;
    			rev = rev*10 + rem;
    			temp = temp/10;
    		}
    		if(rev==n) {
    			System.out.println("wins");
    		}
    		else {
    			System.out.println("loses");
    		}
	    }
	sc.close();
	}
}

Disclaimer: The above Problem (The Block Game) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.