Programming Basic Question
These are the most basic Programming questions you can't afford to miss.
Programming Problems:
1) WAP to test if a given number is even (use % first and then bitwise & operator to test).
2) Accept 2 ints, print the multiplication tables of the smaller number till the second number times (if 5,2 and given, print 2 X 1 till 2 X 5) => Print directly inside the method.
3) WAP to test if a given number is divisible by 3. Do this for both +ve and -ve ints.
4) WAP to test if a given number is prime.
5) WAP to generate all primes within a positive int number given. Can you reuse the method coded for 4th problem?
6) Given 3 numbers, return the correct average of them.
7) WAP to test if a given int number is a positive power of 2
Slightly more complex:
8) WAM to test if all digits in a given number are in decreasing order (allow -ves).
9) WAM to test if all digits in a given number are in increasing order (allow -ves).
10) WAM to test if all digits in a given number are the same (allow -ves).
11) WAM to return the sum of all the digits in a given number (allow -ves).
12) WAM to return the if a given digit is present in a given number.
13) WAP to identify if an int number is a palindrome! Generate all palindromes from 10 till 1 million and print to monitor.
14) WAP to test whether 2 numbers given as inputs contain the same digits, for ex: 121, 112, 211 contain the same digits!
15) WAP to test GCD of 2 int numbers given as parameter.
16) Given 3 numbers, return the second biggest.
Programming Problems Solution:
1) WAP to test if a given number is even (use % first and then bitwise & operator to test).
public class Even { // main method public static void main(String[] args) { int number = 28; //using & System.out.println(number+(isEvenBitWise(number)?" is even using &":" is not even using &")); //using % System.out.println(number+(isEven(number)?" is even using %\n":" is not even using %\n")); int number1 = 29; //using & System.out.println(number1+(isEvenBitWise(number1)?" is even using &":" is not even using &")); //using % System.out.println(number1+(isEven(number1)?" is even using %\n":" is not even using %\n")); } //method for checking Even using % oprator public static boolean isEven(int num) { if(num < 0) num = -num; if(num % 2 == 0) { return true; } return false; } //method for checking Even using Bitwise & oprator public static boolean isEvenBitWise(int num) { if(num < 0) num = -num; if((num & 1) == 0) { return true; } return false; } }
2) Accept 2 ints, print the multiplication tables of the smaller number till the second number times (if 5,2 and given, print 2 X 1 till 2 X 5) => Print directly inside the method.
public class Q2 { //main method public static void main(String[] args) { tableTillLargeNumber(5, 2); } // method for 'multiplication tables of the smaller number till the second number times ' public static void tableTillLargeNumber(int a, int b) { if(a>=b) { for(int i = 1; i <= a; i++) System.out.println(b+" * "+i+" = "+(b*i)); } else { for(int i = 1; i <= b; i++) System.out.println(a+" * "+i+" = "+(a*i)); } } }
3) WAP to test if a given number is divisible by 3. Do this for both +ve and -ve ints.
public class Q3 { public static void main(String[] args) { int n = 393; System.out.println(n+(isDevisibleByThree(n)?" is divisible by 3":" is not divisible by 3")); int n1 = 394; System.out.println(n1+(isDevisibleByThree(n1)?" is divisible by 3":" is not divisible by 3")); int n2 = -39; System.out.println(n2+(isDevisibleByThree(n2)?" is divisible by 3":" is not divisible by 3")); } //isDevisibleByThree method public static boolean isDevisibleByThree(int num) { if (num < 0) num = -num; if(num % 3 == 0) return true; return false; } }
4) WAP to test if a given number is prime.
public class Prime { //main method public static void main(String[] args) { int n = 237; System.out.println("Is "+n+" prime? "+(isPrime(n))); int n1 = -1>>>1; System.out.println("Is "+n1+" prime? "+(isPrime(n1))); int n2 = -1; System.out.println("Is "+n2+" prime? "+(isPrime(n2))); } //method isPrime public static String isPrime(int n) { if(n < 0) return "invalid"; if(n == 2) return "yes"; if(n == 1 || n == 0 || (n & 1) == 0) return "no"; for(int i = 3; i < n/2; i += 2) { if(n%i == 0) return "no"; } return "yes"; } }
5) WAP to generate all primes within a positive int number given. Can you reuse the method coded for 4th problem?
public class Q5 { //main method public static void main(String[] args) { int num = 300; for( int i =0; i <= 300; i++) { if(Prime.isPrime(i) == "yes") // using is prime method from Prime Class System.out.print(i+", "); } } }
6) Given 3 numbers, return the correct average of them.
public class Q6 { public static void main(String[] args) { int n1 = 27, n2 = 25, n3= 22; System.out.println(average(n1,n2,n3)); } // method average public static double average(double n1, double n2, double n3) { return ((n1+n2+n3)/3.0); } }
7) WAP to test if a given int number is a positive power of 2
public class Q7 { public static void main(String[] args) { int num = 128; System.out.println("Is "+num+" Positive Power of 2? "+(isPositivePower(num))); int num2 = 127; System.out.println("Is "+num2+" Positive Power of 2? "+(isPositivePower(num2))); } //method is power of 2 public static String isPositivePower(int n) { if(n <= 0) return "invalid"; if(n == 1) return "no"; for(int i = 2 ; i <= n/2; i*=2) { if(2 * i == n) return "yes"; } return "no"; } }
Slightly more complex Solution:
8) WAM to test if all digits in a given number are in decreasing order (allow -ves).
public class Q8 { //main method public static void main(String[] args) { int n = 9862; System.out.println("is "+n+" digit in decreasing order: "+(isDigitInDecreasingOrder( n))); int n1 = 5862; System.out.println("is "+n1+" digit in decreasing order: "+(isDigitInDecreasingOrder( n1))); int n2 = -123; System.out.println("is "+n2+" digit in decreasing order: "+(isDigitInDecreasingOrder( n2))); int n3 = -323; System.out.println("is "+n3+" digit in decreasing order: "+(isDigitInDecreasingOrder( n3))); int n4 = -789; System.out.println("is "+n4+" digit in decreasing order: "+(isDigitInDecreasingOrder( n4))); int n5 = 789; System.out.println("is "+n5+" digit in decreasing order: "+(isDigitInDecreasingOrder( n5))); } //Method to check decreasing digit public static String isDigitInDecreasingOrder(int n) { int digit = -9; while(n != 0) { int digit1 = n%10; if(digit1 >= digit) { digit = digit1; n = n/10; continue; } break; } if(n==0) return "yes"; else return "no"; } }
9) WAM to test if all digits in a given number are in increasing order (allow -ves).
public class Q9 { //main method public static void main(String[] args) { int n = 2689; System.out.println("is "+n+" digit in increasing order: "+(isDigitInIncreasingOrder( n))); int n1 = 8689; System.out.println("is "+n1+" digit in increasing order: "+(isDigitInIncreasingOrder( n1))); } //Method to check Increasing digit public static String isDigitInIncreasingOrder(int n) { int digit = 9; while(n != 0) { int digit1 = n%10; if(digit1 <= digit) { digit = digit1; n = n/10; continue; } break; } if(n==0) return "yes"; else return "no"; } }
10) WAM to test if all digits in a given number are the same (allow -ves).
public class Q10 { //main method public static void main(String[] args) { int n = 999; System.out.println("is all digit of "+n+" is same: "+(isDigitSame(n))); int n1 = -999; System.out.println("is all digit of "+n1+" is same: "+(isDigitSame(n1))); int n2 = 989; System.out.println("is all digit of "+n2+" is same: "+(isDigitSame(n2))); int n3 = 9090; System.out.println("is all digit of "+n3+" is same: "+(isDigitSame(n3))); } //Method to check same digit public static String isDigitSame(int n) { while(n != 0) { int digit1 = n%10; n = n/10; int digit2 = n%10; if(digit1 == digit2) { n = n/10; continue; } break; } if(n==0) return "yes"; else return "no"; } }
11) WAM to return the sum of all the digits in a given number (allow -ves).
public class Q11 { //main method public static void main(String[] args) { int n = 998; System.out.println("Sum of the digit of "+n+" is: "+(sumOfDigits(n))); int n1 = -998; System.out.println("Sum of the digit of "+n1+" is: "+(sumOfDigits(n1))); int n2 = (-1>>>1); System.out.println("Sum of the digit of "+n2+" is: "+(sumOfDigits(n2))); } //Method to check sum of digit public static int sumOfDigits(int n) { int sum = 0; while(n != 0) { int digit = n%10; sum += digit; n = n/10; } return sum; } }
12) WAM to return the if a given digit is present in a given number.
public class Q12 { //main method public static void main(String[] args) { int num = 998; int key = 9; System.out.println("is "+key+" found in "+num+" : "+(isDigitFound(num, key))); int num1 = 99874; int key1 = 1; System.out.println("is "+key1+" found in "+num1+" : "+(isDigitFound(num1, key1))); } //Method to find digit public static String isDigitFound(int num, int keyDigit) { while(num != 0) { int digit = num%10; if(digit == keyDigit) return "yes"; num = num/10; } return "no"; } }
13) WAP to identify if an int number is a palindrome! Generate all palindromes from 10 till 1 million and print to monitor.
public class Q13 { //main method public static void main(String[] args) { int n = 9999; System.out.println(n+" is pallindrom? "+(isPallindrom(n))); int n1 = 100001; System.out.println(n1+" is pallindrom? "+(isPallindrom(n1))); int n2 = 9909; System.out.println(n2+" is pallindrom? "+(isPallindrom(n2))); // generating all pallindrom from 10 to 1 million System.out.println("pallindrom from 10 to 1 million"); for(int i = 10; i < 1000000; i++) { if(isPallindrom(i) == "yes") System.out.print(i+", "); } } //Method to check Pallindrom public static String isPallindrom(int num) { int rev = 0; int numCopy = num;//we have to create copy to compare with reverse num while(num != 0) { int rem = num % 10; rev = rev * 10 + rem; num = num/10; } if(rev == numCopy) return "yes"; return "no"; } }
14) WAP to test whether 2 numbers given as inputs contain the same digits, for ex: 121, 112, 211 contain the same digits!
import java.util.Arrays; public class Q14 { public static void main(String[] args) { System.out.println("121 & 211: " + match(121,211)); System.out.println("112 & 121: " + match(112,121)); System.out.println("231 & 313: " + match(231,313)); System.out.println("876 & 886: " + match(876,866)); } //method understood than used from internet public static boolean match(int a, int b) { String s1 = String.valueOf(a); char[] c1 = s1.toCharArray(); Arrays.sort(c1); String s2 = String.valueOf(b); char[] c2 = s2.toCharArray(); Arrays.sort(c2); return(Arrays.equals(c1,c2)); } }
15) WAP to test GCD of 2 int numbers given as parameter.
public class Q15 { public static void main(String[] args) { int a = 324; int b = 36; System.out.println("GCD of "+a+" & "+b+" is "+(findGcd(a, b))); int a1 = 32; int b1 = 28; System.out.println("GCD of "+a1+" & "+b1+" is "+(findGcd(a1, b1))); int a2 = 19; int b2 = 27; System.out.println("GCD of "+a2+" & "+b2+" is "+(findGcd(a2, b2))); } //GCD method public static int findGcd(int x, int y) { int rem = 0, dividend, divisor; //this is euclid's algo dividend = (x > y) ? x : y; // a is greater number divisor = (x < y) ? x : y; // b is smaller number rem = divisor; while(dividend % divisor != 0) { rem = dividend % divisor; dividend = divisor; divisor = rem; } return rem; } }
16) Given 3 numbers, return the second biggest.
public class Q16 { //main method public static void main(String[] args) { int a = 309; int b = 207; int c = 200; System.out.println("Second biggest value in ( "+a+", "+b+", "+c+" ) is "+(secondBiggestNum(a, b, c))); } //method Second Biggest public static int secondBiggestNum(int a, int b, int c) { if(((a>b && a>c) && (b<a && b<c))||((b>a && b>c)&&(a<b && a<c))) return c; if(((b>a && b>c) && (c<a && c<b)) ||((c>a && c>b) && (b<a && b<c))) return a; return b; } }
No comments:
For Query and doubts!