0% found this document useful (0 votes)
120 views

Write A Program in Java To Make Such A Pattern Like A Pyramid With A Number Which Will Repeat The Numbers in The Same Row

The document contains 10 code examples in Java that demonstrate various programming concepts: 1) A program to print a number pyramid pattern based on user input. 2) A program to print Floyd's triangle pattern. 3) A program to display a rhombus pattern of numbers. 4) A program to count the digits in a positive integer number. 5) A program to calculate the factorial of a number. 6) A program to check if a number is prime. 7) A program to check if a number is an Armstrong number. 8) A program to count letters, numbers, and other characters in a string. 9) A program to round a floating point number to a

Uploaded by

Nishan Hamal
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)
120 views

Write A Program in Java To Make Such A Pattern Like A Pyramid With A Number Which Will Repeat The Numbers in The Same Row

The document contains 10 code examples in Java that demonstrate various programming concepts: 1) A program to print a number pyramid pattern based on user input. 2) A program to print Floyd's triangle pattern. 3) A program to display a rhombus pattern of numbers. 4) A program to count the digits in a positive integer number. 5) A program to calculate the factorial of a number. 6) A program to check if a number is prime. 7) A program to check if a number is an Armstrong number. 8) A program to count letters, numbers, and other characters in a string. 9) A program to round a floating point number to a

Uploaded by

Nishan Hamal
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
You are on page 1/ 6

1.

Write a program in Java to make such a pattern like a pyramid with a number which will
repeat the numbers in the same row.

import java.util.Scanner;
public class PyramidPattern {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");


int number = sc.nextInt();

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


for (int k = number - i; k > 0; k--) {
System.out.print(" ");
}

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


System.out.print(i + " ");
}
System.out.println();
}
}

2. Write a program in Java to print the Floyd’s Triangle.


import java.util.Scanner;
public class FloydsTriangle {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows: ");


int number = sc.nextInt();

int value = 1;

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


for (int j = 1; j <= i; j++) {
System.out.print(value);
if (value == 1) {
value--;
} else {
value++;
}
}
System.out.println();
}
}

3. Write a Java program to display the number rhombus structure.


import java.util.Scanner;
public class Rhombus {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int space = 1;

System.out.print("Enter the number of rows: ");


int number = sc.nextInt();
space = number - 1;
for (int i = 1; i <= number; i++) {
for (int k = 1; k <= space; k++) {
System.out.print(" ");
}
space--;
for (int j = i; j > 0; j--) {
System.out.print(j);
}
for (int k = 2; k <= i; k++) {
System.out.print(k);
}
System.out.println();
}
space = 1;
for (int i = number - 1; i >= 1; i--) {
for (int k = 1; k <= space; k++) {
System.out.print(" ");
}
space++;
for (int j = i; j > 0; j--) {
System.out.print(j);
}
for (int k = 2; k <= i; k++) {
System.out.print(k);
}
System.out.println();
}
}

}
4. Write a Java program that reads a positive integer and count the number of digits the
number (less than ten billion) has.
import java.util.Scanner;
public class IntegerCount {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int count = 0;
System.out.println("Enter the numbers: ");
int number = sc.nextInt();
int numbers = number;

while (number != 0) {
//int digit = number % 10;
number /= 10;
count++;
}

System.out.println("The number of digits in the given number " + numbers + "


is: " + count);
}

5. Write a program to calculate the factorial number input by user.


import java.util.Scanner;
public class Main {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int count = 0;
System.out.println("Enter the number to calculate factorial: ");
int number = sc.nextInt();
int numbers = number;

for (int i = number - 1; i >= 1; i--) {


number *= i;
}

System.out.println("The factorial of the given number " + numbers + " is: " +
number);
}

}
6. Write a program to check whether a number input by user is prime or not.
import java.util.Scanner;
public class PrimeChecker {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int number, reverse = 0;

System.out.println("Enter the digits to check: ");


number = sc.nextInt();

boolean checker = false;

for (int i = 2; i <= number / 2; i++) {


int temp = number % i;
if (temp == 0) {
checker = true;
break;
}
}

if (!checker) {
System.out.println("The number is prime.");
} else {
System.out.println("The number is not prime.");
}
}

7. Write a program to check whether a number input by user is Armstrong or not.


import java.util.Scanner;
public class Armstrong {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int number, sum = 0;

System.out.println("Enter the digits to check: ");


number = sc.nextInt();

int temp = number;

while (number != 0) {
int digit = number % 10;
sum += Math.pow(digit, 3);
number /= 10;
}

number = temp;

if (number == sum) {
System.out.println("The number " + number + " is armstrong.");
} else {
System.out.println("The number " + number + " is not armstrong.");
}
}

8. Write a program to count the number of upper case, lower case, digit and other
character from a string that is taken as input from user.
import java.util.Scanner;
public class Counter {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter a string: ");


String input = sc.next();

int upper = 0, lower = 0, numbers= 0, others = 0;

for (int i = 0; i <= input.length() - 1; i++) {


if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') {
upper++;
} else if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') {
lower++;
} else if (input.charAt(i) >= '0' && input.charAt(i) <= '9') {
numbers++;
} else {
others++;
}
}

System.out.println("The number of uppercase letters is: " + upper);


System.out.println("The number of lowercase letters is: " + lower);
System.out.println("The number of numbers is: " + numbers);
System.out.println("The number of other characeters is: " + others);
}

}
9. Write a program that takes two input from user: a floating-point digit and a precision, a
and round off and display the number to that precision.

import java.text.DecimalFormat;
import java.util.Scanner;
public class RoundOff {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

String zeroes = "0.";


System.out.println("Enter a floating point number: ");
double number = sc.nextDouble();

System.out.println("Enter a precision that you want: ");


int precision = sc.nextInt();

for (int i = precision; i >= 1; i--) {


zeroes += "0";
}

DecimalFormat df = new DecimalFormat(zeroes);

System.out.println("The floating point number " + number


+ " with given precision " + precision + " is: " + df.format(number));
}

10. Write a program that displays the amount displayed by user in Nepali money format.
11. Write a generalize program for Q20 by asking the format of money from user (e for
English and n for Nepali) and displaying formatted output accordingly.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy