[Solved] Team Event with Java, C++, Python

Team Event: Super Quiz Bee is a famous quiz Competition that tests students on a wide variety of academic subjects. This week’s competition was a Team event and students who register for the event will be given a unique registration code N. The participants are teamed into 10 teams and the team to which a participant is assigned to depends on the absolute difference between the first and last digit in the registration code.

The event organizers wanted your help in writing an automated program that will ease their job of assigning teams to the participants. If the registration number given is less than 10, then the program should display “Invalid Input”.

Input Format:
The only line of input contains an integer N.

Output Format:
Output the absolute difference between the first and last digit of N.
Refer sample input and output for formatting specifications.

Sample Input 1:
345

Sample Output 1:
2

Sample Input 2:
9

Sample Output 2:
Invalid Input

Solution

import java.util.*;
import java.math.*;
class Main
{
    static int first(int n)
            
            {
               int digits = (int)(Math.log10(n));
               
               n= (int)(n/(int)(Math.pow(10,digits)));
     
               return n ;
            }
            
    static int last(int n)
    {
       
        return (n % 10);
    }   
    
    public static void main(String jr [])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n<10)
        {
            System.out.println("Invalid Input");
            
        }
        
        else if(n==10)
        {
            //sauravhathi
           System.out.println(first(n)-last(n)); 
        }
        
        else 
        {  
          if(last(n)<first(n))
          System.out.println(first(n)-last(n));
          else
          System.out.println(last(n)-first(n));
        }
    }
    
}
include<stdio.h>
include<stdlib.h>
int main()
{
    int n, s, i[10], a, g
    scanf("%d", & n)
    if(n/10 == 0)
    printf("Invalid Input")
    else
    {
        for(a=0
            n != 0
            a++)
        {
            s = n % 10

            n = n/10

            i[a] = s
        }

        g = abs(i[0]-i[a-1])
        printf("%d", g)
    }
    return(0)
}
n = int(input())
i = []
if n/10 == 0:
    print("Invalid Input")
else:
    for a in range(n):
        s = n % 10
        n = n/10
        i[a] = s
    g = abs(i[0]-i[a-1])
    print(g)

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 *