Question
Using java
Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern 2: make note of what is uppercase and what is lowercase. You must use for loops to solve this problem. Program must be dynamic and work for any of the valid input values. You CANNOT hardcode the output. HINT You will need to use nested loops for this one. Each pattern is its own set of nested loops.
Sample Outputs
media%2F099%2F09927fde-cb9b-4916-bae7-70
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;

import java.util.*;

class pattern

{

public static void main(String args[])

{

int p1,p2;//for pattern1 and pattern 2

int m,n;

Scanner s=new Scanner(System.in);

int flag1=1,k;

System.out.print("how many rows for pattern1 ? : ");

p1=s.nextInt();

if(p1>0 && p1<1000) flag1=0;

while(flag1==1)

{

System.out.print("how many rows for pattern1 ? : ");

p1=s.nextInt();

if(p1>0 && p1<1000) flag1=0;

}

m=p1;

System.out.println();

for(int i=1;i<=m;i++)

{

k=m;

for(int j=1;j<=i;j++)

{

System.out.print(k-- +" ");

}

System.out.println();

}

int flag2=1;

System.out.print("how many rows for pattern2 ? : ");

p2=s.nextInt();

if(p2>0 && p2<27) flag2=0;

while(flag2==1)

{

System.out.print("how many rows for pattern2 ? : ");

p2=s.nextInt();

if(p2>0 && p2<27) flag2=0;

}

n=p2;

System.out.println();

for(int i=n;i>=1;i--)

{

for(int j=1;j<=i;j++)

{

if(j%2==0)

{

System.out.print((char)(j+64+32) +" ");

}

else

{

System.out.print((char)(j+64) +" ");

}

}

System.out.println();

}

}

}

C: \Users\SARATHCHNDRANDesktop\HomeworkLib>javac pattern.java C: \Users\SARATHCHNDRANDesktop\HomeworkLib>java pattern how many rows for p

Add a comment
Know the answer?
Add Answer to:
Using java Sample Outputs Write a program which asks the user for an integer and then...
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
  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • Exercise 1: Write a C++ program that asks the user to input an integer n followed...

    Exercise 1: Write a C++ program that asks the user to input an integer n followed by n other characters that will be stored in an array. We suppose here that the user will input only lowercase characters between ‘a’ and ‘z’. We also suppose that the user will input different characters. The program will then sort these characters according to an increasing order of their alphabetical order. In this exercise, feel free to use selection sort, insertion sort, or...

  • ANSWER IN C PLEASE (: ew History Bookmarks Window Help 13.16. Loops E ry> ECE 270...

    ANSWER IN C PLEASE (: ew History Bookmarks Window Help 13.16. Loops E ry> ECE 270 home > 13.16 Loops-Ex4 zyBooks catalog TO.TU LOOo Write a program that prints a pattern like the ones shown below Ask the user to input the letter that goes in the middle of the last row in the pyramid (uppercase or lowercase). The pattern should extend to the character entered. In the Example 1, the pattern is the result from an input value of...

  • Using LC3 Editor. This program accepts an integer typed in by the user, verifies that the...

    Using LC3 Editor. This program accepts an integer typed in by the user, verifies that the number is valid, and if it is valid prints the binary version of the number to the display as on the following page. Call the program part2.asm. As you can see the program rejects any input which doesn't start with a + or - sign and prints "The input is invalid." to the display. Also any integer values less than -511 or greater than...

  • Write a C++ program that asks the user to input a decimal integer (i) and an...

    Write a C++ program that asks the user to input a decimal integer (i) and an integer radix from 2 to 16 (r) and outputs the value of iin radix r(base r). (HINT: use repeated division by base r) NOTE: show output screenshot

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • In Python: Write a well-documented (commented) program that asks the user for a 9-digit integer, computes...

    In Python: Write a well-documented (commented) program that asks the user for a 9-digit integer, computes its checksum, and then prints the corresponding ISBN number. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith...

  • In Python Write a well-documented (commented) program that asks the user for a 9-digit integer, computes...

    In Python Write a well-documented (commented) program that asks the user for a 9-digit integer, computes its checksum, and then prints the corresponding ISBN number. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith...

  • Write a program that prints the following pattern to the screen. (no blank lines between lines)...

    Write a program that prints the following pattern to the screen. (no blank lines between lines) You must use nested for loops. 1 212 32123 4321234 543212345 c++

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