WAP to input a number find 4 digit or not. if 4 digit number then find the number if AABB formed or not ex:5588, 2255.
Answers
Answered by
9
Let's understand the question..
- We need to accept 4-digit number from the user
- Then we have to check if the number forms AABB or not
But, How to check if the number is AABB or not?
>> Simple, Just break the number into two equal halves... and check if in each half the digits are same or not..
This logic can be well understood by an example..
- Let's say the number is 2233
- Divide this number into two equal halves... 22 33
- Now we will see if the digit of the first half is same or not.. 22 here the first and last digit is same.
- Same in the second half
- If condition comes true in both the half.. The number forms AABB
Now here comes the programming...
package Programmer;
import java.util.*;
public class Number {
public static void main (String ar [])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); //Accepting input from the user
int D1=n/100;
int D2=n%100;
if((D1%10==D1/10)&&(D2%10==D2/10))
System.out.println("Number forms AABB");
}
}
•Output attached
Attachments:
Similar questions
English,
2 months ago
Math,
2 months ago
Social Sciences,
2 months ago
English,
4 months ago
Math,
4 months ago
History,
10 months ago
Computer Science,
10 months ago
Computer Science,
10 months ago