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);