Question

Write a Java program that reads four integers and prints "two pairs" if the input consists...

Write a Java program that reads four integers and prints "two pairs" if the input consists of two matching pairs (in some order) and "not two pairs" otherwise.

For example,

1 2 2 1 two pairs
1 2 2 3 not two pairs
2 2 2 2 two pairs
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

import java.util.*;
public class twopairs
{
public static void main(String[] args)
{
Scanner num = new Scanner(System.in);
System.out.println("Enter four numbers: ");
int p = num.nextInt();
int q = num.nextInt();
int r = num.nextInt();
int s = num.nextInt();

if((p==q && r == s)|| (p==r && q==s) || (r==q && p==s))
{
System.out.println(p+" "+q+" "+r+" "+s+" two pairs" );
}
else
{
System.out.println(p+" "+q+" "+r+" "+s+" not two pairs");
}
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that reads four integers and prints "two pairs" if the input consists...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT