Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Number triangle pyramid program in Java.





import java.util.Scanner;
public class NumberTraingle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter number of rows : ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = num - r; sp > 0; sp--) {
    System.out.print(" ");
   }
   for (int c = 1; c <= r; c++) {
    System.out.print(r);
   }
   for (int k = 2; k <= r; k++) {
    System.out.print(r);
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 5

         1
       1 2 3
     1 2 3 4 5
   1 2 3 4 5 6 7
 1 2 3 4 5 6 7 8 9

BUILD SUCCESSFUL (total time: 3 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my Blog.


Java program to find perfect numbers



Perfect Number:  a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

package demo;
import java.util.Scanner;
public class PerfectNumber
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int n, i = 1, sum = 0;
  System.out.print("Enter a number: ");
  n = sc.nextInt();
  while (i < n) {
   if (n % i == 0) {
    sum = sum + i;
   }
   i++;
  }
  if (sum == n) {
   System.out.print(i + " is a perfect number");
  } else {
   System.out.print(i + " is not a perfect number");
  }
 }
}

Output: Java program to find perfect numbers

Enter a number: 6
6 is a perfect number
BUILD SUCCESSFUL (total time: 36 seconds)



Java program for addition of two binary numbers.



package demo;
import java.util.Scanner;
public class BinaryAddition 
{
 public static void main(String[] args)
 {
  long binary1, binary2;
  int i = 0, remainder = 0;
  int[] sum = new int[20];
  Scanner sc = new Scanner(System.in);

  System.out.print("Enter any first binary number: ");
  binary1 = sc.nextLong();
  System.out.print("Enter any second binary number: ");
  binary2 = sc.nextLong();

  while (binary1 != 0 || binary2 != 0) 
  {
   sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
   remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
   binary1 = binary1 / 10;
   binary2 = binary2 / 10;
  }
  if (remainder != 0) {
   sum[i++] = remainder;
  }
  --i;
  System.out.print("Sum of two binary numbers: ");
  while (i >= 0) {
   System.out.print(sum[i--]);
  }
 }
}

Output:
Enter any first binary number: 111111
Enter any second binary number: 101010
Sum of two binary numbers: 1101001
BUILD SUCCESSFUL (total time: 8 seconds)


Java program for binary to hexadecimal conversion.



package demo;
import java.util.Scanner;
public class BinaryToHexadecimal
{
 public static void main(String[] args)
 {
  int[] hex = new int[1000];
  int i = 1, j = 0, rem, dec = 0, bin;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter a Binary Number: ");
  bin = sc.nextInt();
  while (bin > 0) {
   rem = bin % 2;
   dec = dec + rem * i;
   i = i * 2;
   bin = bin / 10;
  }
  /* At this state our input number is converted into Decimal Number */
  i = 0;
  while (dec != 0) {
   hex[i] = dec % 16;
   dec = dec / 16;
   i++;
  }
  System.out.print("Equivalent HexaDecimal value: ");
  for (j = i - 1; j >= 0; j--)
  {
   if (hex[j] > 9) 
   {
    System.out.print((char)(hex[j] + 55));
   } else
   {
    System.out.print(hex[j]);
   }
  }
 }
}

Output:
Enter a Binary Number: 11111100
Equivalent HexaDecimal value: FC
BUILD SUCCESSFUL (total time: 6 seconds)


Java program for binary to decimal conversion.



import java.util.Scanner;
public class BinaryToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long binaryNumber, octalNumber = 0, j = 1, remainder;
  System.out.print("Enter any number any binary number: ");
  binaryNumber = sc.nextLong();

  while (binaryNumber != 0) 
  {
   remainder = binaryNumber % 10;
   octalNumber = octalNumber + remainder * j;
   j = j * 2;
   binaryNumber = binaryNumber / 10;
  }
  System.out.println("Octal Number: " + octalNumber);
 }
}

Output:
Enter any number any binary number: 1100110
Octal Number: 102
BUILD SUCCESSFUL (total time: 5 seconds)



Java program for octal to decimal.



package demo;
import java.util.Scanner;
public class OctalToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long octal, decimal = 0;
  int i = 0;
  System.out.print("Enter any octal number: ");
  octal = sc.nextLong();
  while (octal != 0) 
  {
   decimal = (long)(decimal + (octal % 10) * Math.pow(8, i++));
   octal = octal / 10;
  }
  System.out.print("Equivalent decimal value: " + decimal);
 }
}

Output:
Enter any octal number: 1000
Equivalent decimal value: 512
BUILD SUCCESSFUL (total time: 3 seconds)


Java program for octal to binary conversion.



package demo;
import java.util.Scanner;
public class OctalToBinary {
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int[] octalvalues = {0, 1, 10, 11, 100, 101, 110, 111};
  long octal, tempOctal, binary, place;
  int rem;
  /*
   * Reads Octal number from user
   */
  System.out.print("Enter any Octal number: ");
  octal = sc.nextLong();
  tempOctal = octal;
  binary = 0;
  place = 1;
  /*
   * Finds Binary of the octal number
   */
  while (tempOctal != 0)
  {
   rem = (int)(tempOctal % 10);
   binary = octalvalues[rem] * place + binary;
   tempOctal /= 10;
   place *= 1000;
  }
  System.out.println("Octal number = " + octal);
  System.out.print("Binary number = " + binary);
 }
}

Output:
Enter any Octal number: 1250
Octal number = 1250
Binary number = 1010101000
BUILD SUCCESSFUL (total time: 3 seconds)


Java program to convert decimal to octal number.



package demo;
import java.util.Scanner;
public class DecimalToOctal 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int[] octalNumber = new int[100];
  int i = 1;
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   octalNumber[i++] = (quotient % 8);
   quotient = quotient / 8;
  }
  System.out.print("Equivalent octal value of decimal number " + ( decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) {
   System.out.print(octalNumber[j]);
  }
 }
}

Output:
Enter any decimal number: 100
Equivalent octal value of decimal number 100: 144
BUILD SUCCESSFUL (total time: 4 seconds)


Java program to convert decimal to binary.



package demo;
import java.util.Scanner;
public class DecimalToBinary
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int[] binaryNumber = new int[100];
  int i = 1;
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   binaryNumber[i++] = (quotient % 2);
   quotient = quotient / 2;
  }
  System.out.print("Equivalent binary value of decimal number " + (decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) 
  {
   System.out.print(binaryNumber[j]);
  }
 }
}

Output:
Enter any decimal number: 100
Equivalent binary value of decimal number 100 =  1100100
BUILD SUCCESSFUL (total time: 3 seconds)


Algorithum
Step 1: Divide the original decimal number by 2
Step 2: Divide the quotient by 2
Step 3: Repeat the step 2 until we get quotient equal to zero.


Java program to find out the sum of given H.P. Series.




Harmonic progression (H.P.):
Harmonic progression is a progression formed by taking the reciprocals of an arithmetic progression.


Example of H.P. series:
1/3, 1/6, 1/9, …
If you take the reciprocal of each term from the above HP, the sequence will become
3, 6, 9, ...
which is an AP with a common difference of 3.



import java.util.Scanner;
public class Series
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  int n;
  float sum, t;
  System.out.println("1+1/2+1/3+......+1/n");
  System.out.println("Enter the value of n");
  n = sc.nextInt();
  sum = 0;
  for (float i = 1; i <= n; i++) 
  {
   t = 1 / i;
   sum = sum + t;
  }
  System.out.println(sum);
 }
}

Output:
1+1/2+1/3+......+1/n
Enter the value of n
5
2.283334
BUILD SUCCESSFUL (total time: 6 seconds)



Java program to find out the sum of A.P. series



Arithmetic progression (A.P.):
A series of numbers in which difference of any two consecutive numbers is always a same number that is constant. This constant is called as common difference.

Example of A.P. series:
 3 + 8 + 13 + 18 + 23 + 28 + 33


package demo;
import java.util.Scanner;
public class Series {
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  int a, d, n, tn;
  int sum = 0;

  System.out.print("Enter the first number of the A.P. series: ");
  a = sc.nextInt();

  System.out.print("Enter the total numbers in the A.P. series: ");
  n = sc.nextInt();

  System.out.print("Enter the common difference of A.P. series: ");
  d = sc.nextInt();

  sum = (n * (2 * a + (n - 1) * d)) / 2;
  tn = a + (n - 1) * d;
  System.out.print("Sum of the series A.P.: ");

  for (int i = a; i <= tn; i = i + d) 
  {
   if (i != tn) 
   {
    System.out.print(i + " + ");
   } else 
   {
    System.out.print(i + " = " + sum + " ");
   }
  }
 }
}

Output:

Enter the first number of the A.P. series: 3
Enter the total numbers in the A.P. series: 7
Enter the common difference of A.P. series: 5
Sum of the series A.P.: 3 + 8 + 13 + 18 + 23 + 28 + 33 = 126 
BUILD SUCCESSFUL (total time: 17 seconds)


Print Sum of Series 1/1^2 + 1/2^2 + 1/3^2 …………1/n^2 in Java.



import java.util.Scanner;
public class Series 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  double sum = 0;
  for (int i = 1; i <= n; i++)
   sum = sum + (double) 1 / Math.pow(i, 2);
  System.out.println("Sum of series = " + sum);
 }
}

Output:
Enter the no of terms 
5
Sum of series = 1.4636111111111112
BUILD SUCCESSFUL (total time: 2 seconds)



Print Sum of series 1+ (1+2) + (1+2+3)……. (1+2+3+…….n) in Java



import java.util.Scanner;
public class Series  
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  int sum = 0, i = 0;
  for (int j = 1; j <= n; j++) 
  {
   for (i = 1; i <= j; i++) 
   {
    sum = sum + i;
   }
   i = 1;
  }
  System.out.println("Sum of series = " + sum);
 }
}

Output:
Enter the no of terms 
4
Sum of series = 20
BUILD SUCCESSFUL (total time: 2 seconds)


Print sum of series 1/1^3 – 1/2^3 + 1/3^3…….1/n^3 in Java.



import java.util.Scanner;
public class  Series 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  double sum = 0;
  System.out.println("Enter the no of terms ");
  int n = sc.nextInt();
  for (int i = 1; i <= n; i++) 
  {
   if (i % 2 == 0) {
    sum = sum - (double) 1 / (i * i * i);
   } else {
    sum = sum + (double) 1 / (i * i * i);
   }
  }
  System.out.println(" Sum of the series : " + sum);
 }
}

Output:

Enter the no of terms 
5
Sum of the series : 0.904412037037037
BUILD SUCCESSFUL (total time: 3 seconds)


Print Series 1, 11, 111, 1111……..n terms in Java.



import java.util.Scanner;
public class Series  
{
 public static void main(String args[])
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                                          // s for terms of series, c for n terms
  for (c = 1; c <= n; c++)                          // To generate n terms
  {
   s = s * 10 + 1;
   System.out.print(s + " ");
  }                                                           //for  ends
 }
}


Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111...... 
BUILD SUCCESSFUL (total time: 3 seconds)


Print Series 1, 12, 123, 1234, …………n in Java.



import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                       // s for terms of series, c for counter to generate n terms
  for (c = 1; c <= n; c++) {
   s = s * 10 + c;
   System.out.print(s + " ");
  }
 }
}


Output:
Enter the number of terms: 6
1,  12,  123,  1234,  12345,  123456....... 
BUILD SUCCESSFUL (total time: 3 seconds)


Print Series 1, -3, 5, -7, …………n terms in Java.



import java.util.Scanner;
public class OddSeries 
{
 public static void main(String args[]) 
  {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int i = 1, c, f = 1;                            // i for odd nos, c for counter, f for flag
  for (c = 1; c <= n; c++) {
   if (f % 2 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
   i += 2;
   f++;
  }                                                  //Loop ends
 }
}


Output:


Enter the number of terms: 6

1  -3  5  -7  9  -11...... 
BUILD SUCCESSFUL (total time: 6 seconds)



Print Series 2, -4, 6, -8,………n terms in Java



import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  int c, i = 2, n;                                          // c for counter, i for even nos.
  System.out.print("Enter the number of terms: ");
  n = sc.nextInt();
  System.out.print("\n");
  for (c = 1; c <= n; c++, i += 2)               //to generate n terms of the series
  {
   if (i % 4 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
  }
 }
}

Output:

Enter the number of terms: 10
2 -4 6 -8 10 -12 14 -16 18 -20 
BUILD SUCCESSFUL (total time: 6 seconds)



Java program to print Tribonacci Series.



A tribonacci series is one in which the sum next term is the sum of previous three terms
Example 0 1 2 3 6 11 20………….


import java.util.Scanner;
public class Tribonacci 
 {
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 2, d = 0, i;
  System.out.print(a + " , " + b + " , " + c);
  for (i = 4; i <= n; i++) {
   d = a + b + c;
   System.out.print(", " + d);
   a = b;
   b = c;
   c = d;
  }
 }
}

Output:

0 , 1 , 2, 3, 6, 11, 20, 37, 68, 125
BUILD SUCCESSFUL (total time: 1 second)


Java program to print fibonacci series.



A fibonacci series is one in which the next term is the sum of previous two terms
Example. 0 1 1 2 3 5 8……



import java.util.Scanner;
public class Fibonacci 
{
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 0, i;
  System.out.print(a + " , " + b);
  for (i = 3; i <= n; i++) {
   c = a + b;
   System.out.print(", " + c);
   a = b;
   b = c;
  }
 }
}

Output:

0 , 1, 1, 2, 3, 5, 8, 13, 21, 34
BUILD SUCCESSFUL (total time: 0 seconds)