Beginner Java Programs Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Reverse a number using a while loop

A
while( num != 0 )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Reverse a number using for loop

A
int num;
 for( ;num != 0; )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Reverse a number using recursion

A

public static void reverseMethod(int number) {

if (number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Check prime number

A

int num=scan.nextInt();

for(int i=2;i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Perform binary search

A
public static void main(String args[])
   {
      int counter, num, item, array[], first, last, middle;
      //To capture user input
      Scanner input = new Scanner(System.in);
      System.out.println("Enter number of elements:");
      num = input.nextInt(); 
      //Creating array to store the all the numbers
      array = new int[num];
      System.out.println("Enter " + num + " integers");
      //Loop to store each numbers in array
      for (counter = 0; counter  last )
          System.out.println(item + " is not found.\n");

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Linear search

A
int counter, num, item, array[];
      //To capture user input
      Scanner input = new Scanner(System.in);
      System.out.println("Enter number of elements:");
      num = input.nextInt(); 
      //Creating array to store the all the numbers
      array = new int[num]; 
      System.out.println("Enter " + num + " integers");
      //Loop to store each numbers in array
      for (counter = 0; counter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Find duplicate Characters in a String

A

public void countDupChars(String str){

    //Create a HashMap 
    Map map = new HashMap(); 
    //Convert the String to char array
    char[] chars = str.toCharArray();
    /* logic: char are inserted as keys and their count
     * as values. If map contains the char already then
     * increase the value by 1
     */
    for(Character ch:chars){
      if(map.containsKey(ch)){
         map.put(ch, map.get(ch)+1);
      } else {
         map.put(ch, 1);
        }
    }
    //Obtaining set of keys
    Set keys = map.keySet();
    /* Display count of chars if it is
     * greater than 1. All duplicate chars would be 
     * having value greater than 1.
     */
    for(Character ch:keys){
      if(map.get(ch) > 1){
        System.out.println("Char "+ch+" "+map.get(ch));
      }
    }
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Convert binary to decimal using Integer.parseInt() method

A
Scanner input = new Scanner( System.in );
       System.out.print("Enter a binary number: ");
       String binaryString =input.nextLine();
       System.out.println("Output: "+Integer.parseInt(binaryString,2));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Convert binary to decimal without using parseInt

A

public int BinaryToDecimal(int binaryNumber){

    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly