Posts

Character Pattern

  Sample Input 1: 5 Sample Output 1: A BC CDE DEFG EFGHI import java.util.Scanner; public class CharacterPattern { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); for(int i=1;i<=n;i++) { char c=(char)('A'+ i -1); for(int j=1;j<=i;j++) { System.out.print(c); c++; } System.out.println(); } } }

Alphabets Pattern

  Sample Input 1: 7 Sample Output 1: A BB CCC DDDD EEEEE FFFFFF GGGGGGG import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=1; i<=n;i++) { for(int j=1 ;j<=i;j++) { char c = (char)('A' + i -1); System.out.print(c); } System.out.println(); } } }

Square Root

  Given a number N, find its square root. You need to find and print only the integral part of square root of N. Sample Input 1 : 10 Sample Output 1 : 3 import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); int op=0; while(op*op<=n) { op++; } op--; System.out.println(op); } }

Decimal to Binary

  Given a decimal number (integer N), convert it into binary and print. Sample Input 1 : 12 Sample Output 1 : 1100 import java.util.Scanner; public class DecToBin { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int binary[] = new int[100]; int index = 0; if(n==0) { System.out.println("0"); } while(n > 0) { binary[index++] = n%2; n = n/2; } for(int i = index-1;i >= 0;i--) { System.out.print(binary[i]); } } }

Binary to Decimal

  Given a binary number as an integer N, convert it into decimal and print. Sample Input 1 : 1100 Sample Output 1 : 12 import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i=0; int s=0; while(n!=0) { int d=n%10; int b= d* (int)Math.pow(2,i); s=s+b; n=n/10; i++; } System.out.println(s); } }

Reverse of a Number

  Write a program to generate the reverse of a given number N. Print the corresponding reverse number. Sample Input 1 : 1234 Sample Output 1 : 4321 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int r=0; while(n>0) { int d= n%10; r=r*10+d; n=n/10; } System.out.println(r); } }

Terms of AP

  Write a program to print first x terms of the series 3N + 2 which are not multiples of 4. Sample Input 1 : 10 Sample Output 1 : 5 11 14 17 23 26 29 35 38 41 import java.util.*; public class TermsOfAP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum=0; int p=n; for (int i=1; i<=p; i++) { sum=3*i+2; if (sum % 4 != 0) { System.out.print(sum+ " "); } else { //Increasing p in case sum is divisible by 4 so that we get n terms p++; } } } }