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

Java Programs for Number Operations and Patterns

Uploaded by

M Yes
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 views7 pages

Java Programs for Number Operations and Patterns

Uploaded by

M Yes
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

Assignment-3

Name: Yogesh Mourya

Reg. No.: 22BCE10462

1. Program to count positive, negative, and zeros

import [Link];

public class CountNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int positiveCount = 0, negativeCount = 0, zeroCount = 0;
char choice;

do {
[Link]("Enter a number: ");
int num = [Link]();

if (num > 0) {
positiveCount++;
} else if (num < 0) {
negativeCount++;
} else {
zeroCount++;
}

[Link]("Do you want to continue? (y/n): ");


choice = [Link]().charAt(0);
} while (choice == 'y' || choice == 'Y');

[Link]("Positive numbers: " + positiveCount);


[Link]("Negative numbers: " + negativeCount);
[Link]("Zeros: " + zeroCount);
[Link]();
}
}
2. Program to print Fibonacci Series

import [Link];

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link]();
int a = 0, b = 1, next;

[Link]("Fibonacci Series: " + a + " " + b);

for (int i = 2; i < n; i++) {


next = a + b;
[Link](" " + next);
a = b;
b = next;
}
[Link]();
}
}

3. Program to print all Armstrong numbers between 1 to 1000

public class ArmstrongNumbers {


public static void main(String[] args) {
[Link]("Armstrong numbers between 1 and 1000:");

for (int num = 1; num <= 1000; num++) {


int number = num, originalNumber, remainder, result = 0;
originalNumber = number;

while (originalNumber != 0) {
remainder = originalNumber % 10;
result += [Link](remainder, 3);
originalNumber /= 10;
}

if (result == number) {
[Link](number + " ");
}
}
}
}

4. Program to print the multiplication table of a given number

import [Link];

public class MultiplicationTable {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a positive integer: ");
int num = [Link]();

[Link]("Multiplication table of " + num + ":");

for (int i = 1; i <= 10; i++) {


[Link](num + " x " + i + " = " + (num * i));
}
[Link]();
}
}

5. Pattern solutions

Pattern1: Right Half Pyramid Pattern

// Java Program to print

// Pyramid pattern

import [Link].*;

public class GeeksForGeeks {

// Function to demonstrate pattern

public static void printPattern(int n)

int i, j;

// outer loop to handle rows


for (i = 1; i <= n; i++) {

// inner loop to handle columns

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

[Link]("*");

// printing new line for each row

[Link]();

// Driver Function

public static void main(String args[])

int n = 6;

printPattern(n);

Pattern2: Left Half Pyramid Pattern

// Java Program to print pattern

// Left Half Pyramid pattern

import [Link].*;

public class GeeksForGeeks {

// Function to demonstrate pattern

public static void printPattern(int n)


{

int i, j;

// outer loop to handle rows

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

// inner loop to print spaces.

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

[Link](" ");

// inner loop to print stars.

for (j = 0; j <= n - i; j++) {

[Link]("*");

// printing new line for each row

[Link]();

// Driver Function

public static void main(String args[])

int n = 6;

printPattern(n);
}

Pattern3: Full Pyramid Pattern

// Java Program to print

// Triangular Pattern

import [Link].*;

public class GeeksForGeeks {

// Function to demonstrate pattern

public static void printPattern(int n)

int i, j;

// outer loop to handle rows

for (i = 0; i < n; i++) {

// inner loop to print spaces.

for (j = n - i; j > 1; j--) {

[Link](" ");

// inner loop to print stars.

for (j = 0; j <= i; j++) {

[Link]("* ");

}
// printing new line for each row

[Link]();

// Driver Function

public static void main(String args[])

int n = 6;

printPattern(n);

Common questions

Powered by AI

The program iterates through each number from 1 to 1000. For each number, it separates its digits, cubes them, and accumulates the sum of these cubes. If the accumulated sum equals the original number, it qualifies as an Armstrong number. The program prints such numbers as it identifies them .

The multiplication table program prompts the user to enter a positive integer. It then generates a multiplication table for that integer up to a factor of 10, iterating and calculating each product using a for loop. The results are printed in the format 'num x i = product' for each iteration .

The full pyramid pattern necessitates printing spaces on both sides of each row of asterisks to center-align the stars horizontally. It utilizes two primary loops: one for leading spaces, which decrease per row, and one for stars, which increment with each row and include an extra space for separation of consecutive stars. This implementation creates a symmetrical triangle spanning multiple lines .

Enhancements could include input validation to ensure the user enters a positive integer and error handling to manage non-numeric input gracefully by using try-catch blocks. Additionally, prompting users with clear instructions and messages on valid input criteria could improve interaction. Using a loop to re-prompt invalid inputs would further enhance user experience .

In the Fibonacci series program, the user inputs the number of terms to generate. The program initializes the first two terms of the series as 0 and 1. It then iteratively computes subsequent terms by summing the preceding two numbers within a for loop, printing each term as it is computed. This loop continues until the number of terms specified by the user is reached .

The provided program uses a do-while loop to repeatedly prompt the user for an integer, determine its sign (positive, negative, or zero), and update respective counters (positiveCount, negativeCount, zeroCount) accordingly based on conditional if-else statements. The loop continues to execute as long as the user indicates a desire to continue by entering 'y' or 'Y'. At the end, the program prints the total counts for each category .

Do-while loops are advantageous in user-interactive programs because they execute the program's body at least once, ensuring the user has the opportunity to provide initial input before any condition checks terminate the loop. This is beneficial in scenarios like the counting program where user participation drives the program's continuation .

The Armstrong numbers program has computational inefficiencies due to its repeated digit extraction and power calculations for each number from 1 to 1000. Optimization techniques, such as memoization of power values or using a fixed power value assignment for numbers with equal digits, could improve performance. For larger ranges, the current approach would not scale efficiently due to its exponential time complexity concerning the number of digits .

The right half pyramid pattern is generated using two nested loops. The outer loop iterates over the rows, while the inner loop iterates over the columns. For each row, the program prints a number of asterisks (*) equal to the current row number, producing an incremental right-facing triangular pattern. Each row concludes with a newline .

To convert a right half pyramid into a left half pyramid, the program must account for leading spaces on each row. This involves adding an inner loop that prints decreasing amounts of space characters before the asterisks for each row. The added loop would execute for the number of spaces equal to the difference between the total number of rows minus the current row number .

You might also like