0% found this document useful (0 votes)
10 views5 pages

Java Programming Patterns and Salaries

This document contains 5 programming questions involving Java code to generate patterns, calculate salaries, and check if a number is special. Question 1 involves code to calculate employee's gross salary based on their basic salary. Question 2 has code for 3 different patterns - a diagonal pattern with @ and #, descending numbers pattern, and a pattern with #* repeated. Question 3 checks if a given number is a special number by calculating the factorial of its digits and comparing the sum to the original number. Question 4 displays one of two patterns based on user choice - an alphabet pattern counting down, or a string pattern with increasing characters. Question 5 has code for two different patterns - printing an increasing substring of a word,

Uploaded by

anishbarge23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Java Programming Patterns and Salaries

This document contains 5 programming questions involving Java code to generate patterns, calculate salaries, and check if a number is special. Question 1 involves code to calculate employee's gross salary based on their basic salary. Question 2 has code for 3 different patterns - a diagonal pattern with @ and #, descending numbers pattern, and a pattern with #* repeated. Question 3 checks if a given number is a special number by calculating the factorial of its digits and comparing the sum to the original number. Question 4 displays one of two patterns based on user choice - an alphabet pattern counting down, or a string pattern with increasing characters. Question 5 has code for two different patterns - printing an increasing substring of a word,

Uploaded by

anishbarge23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Work Sheet - 7

General - Key

Question 1:

import [Link].*;
public class salary6a
{
public static void main() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
double bp,da=0.0,hra=0.0,pf=0.0,np=0.0,gp=0.0;
[Link]("Enter Basic Salary");
bp=[Link]([Link]());
da=bp*25/100;
hra=bp*15/100;
pf=bp*8.33/100;
np=bp+da+hra;
gp=np-pf;
[Link]("Gross salary Rs: "+ gp);
}
}

Question 2a:

public class pattq10a


{
public void display()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==0)
[Link]("#");
else
[Link]("@");
}
[Link]();
}
}
}

Question 2b:
public class pattq10b
{
public void display()
{
for(int i=5;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
}
Question 2c:

public class pattq10c


{
public void display()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=5;j++)
{
[Link]("#* ");
}
[Link]();
}
}
}

Question 3:

import [Link].*;
public class Q3
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number:");
int n = [Link]();
int copy = n , d=0, fact=1, sum=0;
while(copy>0)
{
d=copy%10;
copy=copy/10;
for(int i=1;i<=d;i++)
{
fact = fact*i;
}
sum = sum+fact;
fact=1;
}
if(sum==n)
[Link](n+ " is a Special Number");
else
[Link](n+ " is Not a special Number");
}
}

Question 4:

import [Link].*;
public class Display_Pattern4
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int ch;
[Link]("Enter 1. to display Pattern 1");
[Link]("Enter 2. to display Pattern 2");
[Link]("Enter your choice 1 or 2");
ch=[Link]();
switch(ch)
{
case 1:
{
char chr;
[Link]("Pattern 1:");
for(int i=69;i>=65;i--)
{
for(int j=65;j<=i;j++)
{
chr=(char)(j);
[Link](chr);
}
[Link]();
}
}
break;
case 2:
{
String str;
int len;
char chr;
[Link]("Pattern 2:");
[Link]("Enter a string in upper case");
str=[Link]();
len=[Link]();
for(int i=0;i<len;i++)
{
for(int j=0;j<=i;j++)
{
chr=[Link](i);
[Link](chr);
}
[Link]();
}
}
break;
default:
[Link]("You entered wrong choice");
}
}
}

Question 5:
a)
import [Link].*;
public class Pattern
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a,b;
String st;
[Link]("Enter the word to print in a pattern");
st=[Link]();
b=[Link]();
[Link]("The Pattren:");
for(a=0;a<=b;a++)
{
[Link]([Link](0,a));
}
}
}
b)
import [Link].*;
public class Pattern2
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a,b,p,k=1;
String st;
[Link]("Enter any word to print in the pattern");
st=[Link]();
b=[Link]();
[Link]("The Pattern:");
for(a=b;a>=0;a--)
{
for(p=1;p<=k;p++)
{
[Link](" ");
}
[Link]([Link](0,a));
k++;
}
}
}

Common questions

Powered by AI

The gross salary is calculated by first determining the components based on the basic salary. Dearness Allowance (DA) is 25% of the basic salary, House Rent Allowance (HRA) is 15%, and Provident Fund (PF) is 8.33%. The net pay (np) is calculated as the sum of basic salary, DA, and HRA. Gross pay (gp) is then the net pay minus PF.

'Pattq10b' uses decrementing loops to produce a numerical sequence in triangular form. The outer loop decrements from 5 to 1 which controls the number of rows, while the inner loop decrements from the current row number to 1 to print each row's contents. This results in consecutive lines with a decreasing sequence of integers starting from the current row number down to 1.

The 'pattq10a' outputs a pattern consisting of '@' and '#' in a pyramid shape based on the condition of odd and even indices, while 'pattq10c' prints a block consisting of the static pattern "#* " repeated uniformly across three rows. The key difference is the conditional dynamic generation in 'pattq10a' versus the static, repeated structure in 'pattq10c'.

BufferedReader in the salary calculation program is used to read text from the console. It is utilized here to take user input for the basic salary, which is then processed to calculate allowances and deductions. It efficiently reads characters, arrays, and lines, making it suitable for reading input streams in Java.

The program 'pattq10a' outputs a pattern of '@' and '#' characters in a pyramid-like form. It uses nested loops to determine the structure of the pattern, where the outer loop controls the number of rows, and the inner loop controls the characters printed per row. The conditional statement (if-else) checks whether the column index (j) is even or odd to print '@' for odd indices and '#' for even indices.

The 'Pattern' class outputs a pattern that incrementally shows substrings of a user-input word. It uses a loop to print substring(0,a) where 'a' increments in each iteration, resulting in consecutive substrings from an empty string up to the full word. This demonstrates the use of the Java String method 'substring' to gradually reveal the string's characters from left to right.

The program 'Display_Pattern4' uses a reverse alphabetical printing approach with ASCII character manipulation. It initializes the character variable using ASCII values and prints characters from 'E' to 'A' in decreasing length rows. The outer loop controls the row count while the inner loop calculates which character to print using ASCII conversion. This method leverages character arithmetic for pattern generation, making ASCII manipulation crucial for controlling output contents.

The 'Pattern2' program creates a staggered text output by decreasing the substring length from 'b' to 0 and incrementing leading spaces for each line. This uses two nested loops: the outer loop changes the substring end index, and the inner loop precedes each substring with an increasing number of spaces determined by counter variable 'k'. 'k' controls indentation for each line, making the text appear progressively more indented.

The 'Display_Pattern4' program uses a switch statement to provide different pattern outputs based on user input. The user is prompted to select between pattern 1 or 2 by entering a choice. The program executes the corresponding case block which contains logic for displaying either an alphabetical decreasing pattern or a user-inputted uppercase character pattern. Wrong choices result in an error message output.

A 'Special Number' in the program 'Q3' is determined by comparing the input number and the sum of the factorial of its digits. The program reads the number, then iterates over each digit to calculate its factorial. The sum of these factorials is computed, and the number is considered special if this sum equals the original number (n). This uses a while loop to separate each digit, and a for loop to calculate the factorial.

You might also like