The document contains Java code snippets for various algorithms and programs including:
1) Creating a 3x3 matrix and printing it.
2) Transposing a 3x3 matrix and printing it before and after transposition.
3) Calculating the areas of basic shapes like circle, square, rectangle, and triangle.
4) Generating the Fibonacci series up to a given number.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
144 views
Java Interview Programs 3
The document contains Java code snippets for various algorithms and programs including:
1) Creating a 3x3 matrix and printing it.
2) Transposing a 3x3 matrix and printing it before and after transposition.
3) Calculating the areas of basic shapes like circle, square, rectangle, and triangle.
4) Generating the Fibonacci series up to a given number.
Write a program to derive Factorial of a given number
public class Factorial {
public static void main(String[] args) {
int num= 5; int fact= 1; System.out.println("Factorial of " + num );
for (int i= 1; i<=num; i++) { fact=fact*i; } System.out.println(fact);
}
}
Write a program to verify if a given word is a palindrome (Palindrome:characters appears same in both the directions eg: MADAM - Palindrome)
class Palindrome { public static void main(String args[]) { String original, reverse=""; Scanner input = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome"); original = input.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) { reverse = reverse + original.charAt(i); }
if (original.equals(reverse)) { System.out.println("Entered string is a palindrome."); } else { System.out.println("Entered string is not a palindrome."); } input.close();
} }
Write a program to print Prime Numbers in the given range.
import java.util.Scanner;
class PrimeNumber {
public static void main(String[] args) throws Exception{
int i;
Scanner input = new Scanner(System.in);
System.out.println("Enter number:");
int num = Integer.parseInt(input.nextLine());
System.out.println("Prime number: ");
for (i=1; i < num; i++ ){
int j;
for (j=2; j<i; j++){ int n = i%j; if (n==0){ break; }
}
if(i == j){ System.out.print(" "+i); } } } }
Write a program to print Prime Numbers in the given range.
import java.util.Scanner;
public class PascalsTriangle {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
System.out.println("Please enter the size of the triangle you want");
for (int i = 1; i < array.length; i++) { for (int j = 1; j < array[i].length; j++) { array[i][j] = array[i-1][j-1] + array[i-1][j]; } }
return array; }
private static void printArray(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { if(array[i][j] != 0) System.out.print(array[i][j] + " "); } System.out.println(); }
}
}
Write a program to take a String as input and reverse it.
public class StringReverse {
public static void main(String[] args) { String original, reverse = ""; Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse"); original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) { reverse = reverse + original.charAt(i); } System.out.println("Reverse of entered string is: "+reverse);
}
}
Write a program to reverse a number
public class NumberReverse {
public static void main(String[] args) { int original=12345; StringBuffer reverse=new StringBuffer(); String str= Integer.toString(original); int length = str.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) { reverse = reverse.append(str.charAt(i)); } System.out.println("Reverse of the number " + reverse.toString());
}
}
Write a program to print Floyds Triangle
public class FloydsTriangle {
public static void main(String args[]) { int i, j, n; for( i = 1; i <= 5; i++) { for( j = i, n = 1; n <= i; n++, j++) { System.out.print(j%2 + " "); } System.out.println(" "); } } }
Write a program to print highest of 3 numbers
class LargestOfThreeNumbers { public static void main(String args[]) { int x, y, z; System.out.println("Enter three integers "); Scanner in = new Scanner(System.in);
x = in.nextInt(); y = in.nextInt(); z = in.nextInt();
if ( x > y && x > z ) System.out.println("First number is largest."); else if ( y > x && y > z ) System.out.println("Second number is largest."); else if ( z > x && z > y ) System.out.println("Third number is largest."); else System.out.println("Entered numbers are not distinct."); } }
/*An Armstrong number is a number such that the sum ! of its digits raised to the third power is equal to the number ! itself. For example, 371 is an Armstrong number, since ! 3**3 + 7**3 + 1**3 = 371.*/
Write a program to find in a given number is Armstrong number
class ArmstrongNumber { public static void main(String args[]) { int n, sum = 0, temp, r;
Scanner in = new Scanner(System.in); System.out.println("Enter a number to check if it is an armstrong number"); n = in.nextInt();
temp = n;
while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; }
if ( n == sum ) System.out.println("Entered number is an armstrong number."); else System.out.println("Entered number is not an armstrong number."); } }
Write a program to print Armstrong number in a given range like 100 to 1000
public class ArmstrongNumberInRange {
public static void main(String[] args) {
for(int num=100;num<=1000;num++) { int sum = 0, r=0, temp=num;
while( temp > 0 ) { r = temp%10; sum = sum + (r*r*r); temp =temp/10;
}
if ( num == sum ) System.out.println("Number " + num + " is an armstrong number"); }
Sorting in descending order, however, is only possible either by writing your own sorting code, or converting your array to Integer objects then importing from the Collections library
Write a program for number sorting using bubble sort for desceding order public class IntegerSorting {
public static void main(String[] args) {
int temp;
int num[]={5,8,2,1,9};
for(int i=0; i < num.length; i++ ) {
for(int j=i+1; j < num.length; j++ ) { // to get ascending order change it >
if ( num[i] < num[j] ) { temp = num[ i ]; num[ i ] = num[ j ]; num[ j ] = temp; } } }
for(int k=0; k < num.length; k++ ) { System.out.println(num[k]); }
}
}
Please solve this also 1 Write a program to print odd numbers b/w 1-100 2.Write a program to print even numbers b/w 1-100 3.Write a program to print sum of 100 numbers 4. Write a program to print product of first 10 numbers 5Write a Java program to compare two numbers 6. Write a Java Program to list all even numbers between two numbers Write a program to print the below Triangle
1 23 456 78910 7 Write a program to 10 -1 in reverse order
8 Write a program to print
1 22 333 4444 55555
9 Write a program to find if two integers are both even or both odd none
10 Write a program to print all odd numbers from 10 -50
11Write a program to find the sum of all the numbers from 10-50 that are divisible by 3
Write a program to get the following
input str1="Water,str2="Bottle" o/p-WatBottleer
public class StringManipulation1 {
public static void main(String[] args) { String str1="Water"; String str2="Bottle";
//str1.substring(str1.length()-2, str1.length()); //o/p er System.out.println(str2.replaceFirst("", str1.substring(0, 3))+ str1.substring(str1.length()-2, str1.length()));
}
}
Write a Program to print average of the integer array elements and also to print the mean base on odd or even number of elements in the array
public class ArrayAverage {
public static void main(String[] args) {
int[] numbers = new int[]{10,20,15,25,16,60,100,5,7};
//to print the average of array elements int sum = 0;
for(int i=0; i < numbers.length ; i++) sum = sum + numbers[i];
double average = sum / numbers.length;
System.out.println("Average value of array elements is : " + average);
//to give you the mean based on odd or even elements // in the array if (numbers.length % 2==0) { int num1pos=numbers.length/2; int num2pos=num1pos +1; double mean=(numbers[num1pos- 1]+numbers[num2pos-1])/2; System.out.println(mean);
} else { int num1pos=numbers.length/2; System.out.println(numbers[num1pos]); }
} }
Write a program to divide a number without using / operator
public class DivideWithOutOperator {
public static void main(String[] args) { int number = 26; int divisor = 5; int result = 0;
while((number-divisor)>=0){ result++; number = number - divisor; }
System.out.println(result); }
}
Write a program to multiply 2 numbers without using number without using * multiplication operator
public class MultiplyWithoutOperator {
public static void main(String[] args) { int number1 = 10; int number2 = 5; int result = 0;
Write a program to reverse a String and also Sort the string characters alphabetically.
public class ReverseAndSort {
public static void main(String[] args) { String str="Hello Chennai"; StringBuffer str1 = new StringBuffer(str); System.out.println(str1.reverse()); //to put it in a string str=str1.reverse().toString(); System.out.println(str);
Write a program to print a the following Triangle 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
public class TriangleOne {
public static void main(String[] args) throws IOException { System.out.println("Enter the number of rows"); Scanner in = new Scanner(System.in);
int numRow = in.nextInt(); for (int i = 1; i <= numRow; i++) { // Prints the blank spaces for (int j = 1; j <= numRow - i; j++) { System.out.print(" "); } // Prints the value of the number for (int k = 1; k <= i; k++) { System.out.print("1 "); } System.out.println(); } } }
Write a program to print a the following Triangle
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
public class RowNumberIncrementTriangle {
public static void main(String[] args) throws IOException { System.out.println("Enter the number of rows"); Scanner in = new Scanner(System.in);
int numRow = in.nextInt(); for (int i = 1; i <= numRow; i++) { // Prints the blank spaces for (int j = 1; j <= numRow - i; j++) { System.out.print(" "); } // Prints the value of the number for (int k = 1; k <= i; k++) { System.out.print(i +" "); } System.out.println(); } } }
Write a program to print a the following Triangle
1 32 654 10987
public class FlippedTriangle { public static void main(String[] args) { int rows=4; int cntr=1; int start; int val; for(int i=1;i<=rows;i++) {
Write a program to print the perfect numbers b/w 1-500 Ex: The number 6 is said to be a perfect number because it is equal to the sum of all its exact divisors (other than itself). 6 = 1 + 2 + 3
If the reverse square root of the reverse of square of a number is the number itself then it is Adam Number. 12 and 21 Take 12 square of 12 = 144 reverse of square of 12 = 441 square root of the reverse of square of 12 = 21 The reverse square root of the reverse of square of 12 = 12, then number itself. Such number is called Adam Number.
class AdamsNumber {
public static void main(String[] args) {
AdamsNumber an = new AdamsNumber(); int i, n, rn; int sn, rsn, rrsn; System.out.println("List of Adam Numbers under 1000"); for (i = 10; i < 1000; i++) { n = i; rn = an.ReverseNumber(i); if (n == rn) continue; sn = n * n; rsn = rn * rn; rrsn = an.ReverseNumber(rsn); if (rrsn == sn) { System.out.println(n); } } }
int CountNumberOfDigits(int n) { int numdgits = 0; do { n = n / 10; numdgits++; } while (n > 0); return numdgits; }
int ReverseNumber(int n) { int i = 0, result = 0; int numdigits = CountNumberOfDigits(n); for (i = 0; i < numdigits; i++) { result *= 10; result += n % 10; n = n / 10; } return result; } }