Automation QA Testing Course Content

Java Interview programs

 package arrayprograms;


public class JavaInterviewPrograms {

/**

 * write a program to check given number is palindrome or not

 * palindrome:even after reverse the initial number and reversed number is same then 

 * we call that as palindrome

 * int n = 345

 * @param n

 */

private static void checkPalindromeNumber(int n) {

//declare variables

int rev=0,rem,temp;

//store in temp variable

temp = n;

while(n>0) {

//extract each digit from givne number using n%10

rem = n%10;

rev = (rev*10) +rem;

//extract n value

n = n/10;

}

if(rev==temp) {

System.out.println("given number:"+temp+" is palindrome:"+rev);

}else {

System.out.println("given number:"+temp+" is not palindrome:"+rev);

}

}


/**

* write a program to check given number is armstrongnumber or not

* armstrongnumber:Armstrong number is a number that is equal to the sum of cubes of its 

* digits.

*  For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. 

* 153 =1*1*1+3*3*3+5*5*5=1 +27 + 125=153

* @param n

*/

protected static void checkArmstrongNumber(int n) {

//declare variables

int cube=0,rem,temp;

temp =n;

while(n>0) {

//extract each digit from givne number using n%10

rem = n%10;

n = n/10;

cube = cube +(rem*rem*rem);

}

if(cube==temp) {

System.out.println("given number "+temp+" is armstrongnumber:"+cube);

}else {

System.out.println("given number "+temp+" is not armstrongnumber:"+cube);

}

}

/**

* write a program to check given number is prime or not

* primenumber is a number that is greater than 1 and divided by 1 or itsef.

* primenumbers cannot be divided by other numbers than itself or 1

* ex: 2, 3, 5,7,11,13, 17

* @param args

*/

public static void checkPrime(int n) {

int m = n/2,flag =0;

if(n==0||n==1) {

System.out.println(n+" is not a prime number");

}else {

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

if(n%i==0) {

System.out.println(n+" is not a prime number");

flag=1;

break;

}

}

if(flag==0) {

System.out.println(n+" is a prime number");

}

}

}

/**

* write a program to print rightAngletriangle

* *

* * *

* * * *

* * * * *

* * * * * *

*/

protected static void printRightAngleTriangle() {

int rowCnt=6;

//out for llop for rows

for(int i=0;i<rowCnt;i++) {

//inner for loop for cols

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

//print the stars

System.out.print("*");

}

System.out.println();

}

}

/**

* write a program to print pyramid shape

*     *

*    *  *

*   *  *   *

*  *  *   *  *

*/

public static void pyramidPattern() {

//declare number of rows you want in pyramid shape

int rowCnt = 6;

//for rows

for(int i=0;i<rowCnt;i++) {

//inner for loop for space

for(int j=rowCnt-i;j>1;j--) {

System.out.print(" ");

}

//print the star

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

System.out.print(" * ");

}

System.out.println();

} //first for loop ending

}//method ending

public static void main(String[] args) {

checkPalindromeNumber(123);

checkArmstrongNumber(370);

checkPrime(3);

checkPrime(5);

checkPrime(4);

checkPrime(9);

checkPrime(13);

printRightAngleTriangle();

pyramidPattern();

}


}





Java Program to Diamond Shape Star Pattern
Diamond Shape star pattern:

                     *
                    *  *
                  *  *  *
                *  *  *  *
               *  *  *  *  *
             *  *  *   *  * *
           *  *  *  *  *  *  *
             *  *  *  *  *  *
              *  *  *  *  *
               *  *  *  * 
                 *  *  * 
                  *  *
                    *  

// Main driver method

public static void dPatternUsingForLoop()

{

// Declaring and initializing variables

// Variable initialized to the row where max star

// should be there as after that they decreases to

// give diamond pattern

int number = 7;

int m, n;

// Outer loop 1

// prints the first half diamond

for (m = 1; m <= number; m++) {

// Inner loop 1 print whitespaces inbetween

for (n = 1; n <= number - m; n++) {

System.out.print(" ");

}

// Inner loop 2 prints star

for (n = 1; n <= m * 2 - 1; n++) {

System.out.print("*");

}

// Ending line after each row

System.out.println();

}

// Outer loop 2

// Prints the second half diamond

for (m = number - 1; m > 0; m--) {

// Inner loop 1 print whitespaces inbetween

for (n = 1; n <= number - m; n++) {

System.out.print(" ");

}

// Inner loop 2 print star

for (n = 1; n <= m * 2 - 1; n++) {

System.out.print("*");

}

// Ending line after each row

System.out.println();

}

}

---------------------------------------------------------------------------------------------------------------





















































No comments:

Post a Comment

Note: Only a member of this blog may post a comment.