Computer Applications
Write a program to print the following patterns.
(i)
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
(ii)
J
I H
G F E
D C B A
Java
Java Nested for Loops
9 Likes
Answer
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1 ");
for (int k = 5; k >= 1; k--) {
for (int l = k; l <= 5; l++) {
System.out.print(l + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2 ");
char ch = 'J';
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch--;
}
System.out.println();
}
}
}
Output

Answered By
5 Likes
Related Questions
Write a program to input a number. Check and display whether it is a Niven number or not. (A number is said to be Niven which is divisible by the sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.Write a program to accept 10 different decimal numbers (double data type) in a Single Dimensional Array (say, A). Truncate the fractional part of each number of the array A and store their integer part in another array (say, B).
Define a class StringMinMax to find the smallest and the largest word present in the string.
E.g.
Input:
Hello this is wow worldOutput:
Smallest word: is
Largest word: HelloA class Employee contains the following member:
Class name : Employee
Data members/Instance variables
String ename : To store the name of employee
int ecode : To store the employee code
double basicpay : To store the basic pay of employeeMember functions/Methods
Employee( - - - - ) : An argumented constructor to assign name, employee code and basic salary to data members/instance variables
double salCalc( ) : To compute and return the total salary of an employee
void display( ) : To display ename, ecode, basicpay and calculated salaryThe salary is calculated according to the following rules :
Salary = Basic pay + HRA + DA + TA
where, HRA = 20% of basic pay
DA = 25% of basic pay
TA = 10% of basic payif the ecode <= 100, then a special allowance (20% of salary) will be added and the maximum amount for special allowance will be 2500.
if the ecode > 100 then the special allowance will be 1000.
Hence, the total salary for the employee will calculated as :
Total Salary = Salary + Special Allowance
Specify the class Employee giving the details of the constructor, double salCalc() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.