Java Program of Kids School Example

Question:

In a school, the Kindergarten students are supposed to repeat a word 5 times. For example, if the word is “dancing”, they will have to say dancing dancingdancingdancingdancing.

Write a program to accept the string(word) as input from the user and also get the X and the Y position of the character as input from the user. If the Xth and Yth position characters are same then print it as “Same” else “Not Same”.

[Example: if the input is dancing, and if the x is 3 and y is 10 then the output will be “Same”. The students keep on repeating the word for 5 times, the 3rd position character is “n” and the 10th position character is also “n” so the output is the same]

Sample input 1:

Dancing

3

10

Sample output 1:

Same

Sample input 2:

love

2

12

Sample output 2:

Not Same

Code:

Main.java

import java.util.Scanner;

public class kid_school {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int pos1 = sc.nextInt();
        int pos2 = sc.nextInt();

        String a="";
        for (int i=1;i<=5;i++){
            a+=str;
        }
        System.out.println(a);
        if(a.charAt(pos1)==a.charAt(pos2)){
            System.out.println("Same");
        }
        else{
            System.out.println("Not Same");
        }

    }
}

Output:

Dance
3
13
Same

Recommended:

Leave a Comment

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