0% found this document useful (0 votes)
0 views4 pages

Java Programs 1 To 4

The document contains Java programs that demonstrate various functionalities, including checking for Automorphic and Armstrong numbers, converting words to Pig Latin, identifying Buzz and Perfect numbers, and handling two-dimensional arrays for input and display. Each program includes a main method for user interaction and specific methods for the respective checks or conversions. The code is structured to take user input and provide output based on the calculations performed.

Uploaded by

kummardeepak1961
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)
0 views4 pages

Java Programs 1 To 4

The document contains Java programs that demonstrate various functionalities, including checking for Automorphic and Armstrong numbers, converting words to Pig Latin, identifying Buzz and Perfect numbers, and handling two-dimensional arrays for input and display. Each program includes a main method for user interaction and specific methods for the respective checks or conversions. The code is structured to take user input and provide output based on the calculations performed.

Uploaded by

kummardeepak1961
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/ 4

Java Programs (1 to 12)

1. Automorphic and Armstrong Number

import java.util.Scanner;

public class NumberCheck {


public void checkAutomorphic(int num) {
int square = num * num;
String numStr = String.valueOf(num);
String squareStr = String.valueOf(square);
if (squareStr.endsWith(numStr)) {
System.out.println(num + " is an Automorphic number.");
} else {
System.out.println(num + " is NOT an Automorphic number.");
}
}

public void checkArmstrong(int num) {


int sum = 0, temp = num, digits = String.valueOf(num).length();
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
if (sum == num)
System.out.println(num + " is an Armstrong number.");
else
System.out.println(num + " is NOT an Armstrong number.");
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
NumberCheck obj = new NumberCheck();
System.out.print("Enter a number: ");
int num = sc.nextInt();
obj.checkAutomorphic(num);
obj.checkArmstrong(num);
}
}

2. Pig Latin Converter

import java.util.Scanner;
public class PigLatin {
public void convert(String word) {
word = word.toLowerCase();
char firstChar = word.charAt(0);
if ("aeiou".indexOf(firstChar) != -1) {
System.out.println(word + "way");
} else {
String pigLatin = word.substring(1) + firstChar + "ay";
System.out.println(pigLatin);
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
PigLatin obj = new PigLatin();
System.out.print("Enter a word: ");
String word = sc.next();
obj.convert(word);
}
}

3. Buzz and Perfect Number

import java.util.Scanner;

public class SpecialNumbers {


public void checkBuzz(int num) {
if (num % 10 == 7 || num % 7 == 0)
System.out.println(num + " is a Buzz number.");
else
System.out.println(num + " is NOT a Buzz number.");
}

public void checkPerfect(int num) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0)
sum += i;
}
if (sum == num)
System.out.println(num + " is a Perfect number.");
else
System.out.println(num + " is NOT a Perfect number.");
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
SpecialNumbers obj = new SpecialNumbers();
System.out.print("Enter a number: ");
int num = sc.nextInt();
obj.checkBuzz(num);
obj.checkPerfect(num);
}
}

4. Two Dimensional Array Input & Display

import java.util.Scanner;

public class TwoDArray {


private int[][] matrix;
private int rows, cols;

public void inputMatrix() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
rows = sc.nextInt();
System.out.print("Enter columns: ");
cols = sc.nextInt();
matrix = new int[rows][cols];

System.out.println("Enter elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("[" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
}
}

public void displayMatrix() {


System.out.println("Matrix is:");
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + "\t");
}
System.out.println();
}
}

public static void main(String[] args) {


TwoDArray obj = new TwoDArray();
obj.inputMatrix();
obj.displayMatrix();
}
}

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