Posts

Java program to display triangle 0 10 101 0101

1 2 3 4 5 6 7 8 9 10 11 1 /* Display Triangle as follow 0 1 0 1 0 1 0 1 0 1 */ class Output2 { public static void main ( String args [ ] ) { for ( int i = 1 ; i <= 4 ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( ( ( i + j ) % 2 ) + " " ) ; } System . out . print ( " \n " ) ; } }

Java program to display triangle 1 24 369 481216

/* Display Triangle as follow 1 2 4 3 6 9 4 8 12 16 ... N (indicates no. of Rows) */ class Output3{ public static void main(String args[]){ int n = Integer.parseInt(args[0]); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ System.out.print((i*j)+" "); } System.out.print("\n"); } } }

Java program to prints all real solutions to the quadratic equation ax2+bx+c = 0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 import java.io.* ; class Quadratic { public static void main ( String args [ ] ) throws IOException { double x1,x2,disc,a,b,c ; InputStreamReader obj = new InputStreamReader ( System . in ) ; BufferedReader br = new BufferedReader ( obj ) ; System . out . println ( "enter a,b,c values" ) ; a = Double . parseDouble ( br. readLine ( ) ) ; b = Double . parseDouble ( br. readLine ( ) ) ; c = Double . parseDouble ( br. readLine ( ) ) ; disc = ( b * b ) - ( 4 * a * c ) ; if ( disc == 0 ) { System . out . println ( "roots are real and equal " ) ; x1 = x2 =- b / ( 2 * a ) ; System . out . println ( "roots are " + x1 + "," + x2 ) ; } else if ( disc > 0 ) { System . out . println ( "roots are real and unequal" ) ; x1 = ( - b + Math . sqrt ( disc ) ) / ( 2 * a ) ; x2 = ( - b + Math . sqrt ( disc ...

Java program to print Fibonacci sequence Recursive & Non Recursive

1 2 3 4 5 6 7 8 9 10 11 12 13 14 /*Non Recursive Solution*/ import java.util.Scanner ; class Fib { public static void main ( String args [ ] ) { Scanner input = new Scanner ( System . in ) ; int i,a = 1 ,b = 1 ,c = 0 ,t ; System . out . println ( "Enter value of t:" ) ; t = input. nextInt ( ) ; System . out . print ( a ) ; System . out . print ( " " + b ) ; for ( i = 0 ; i < t - 2 ; i ++ ) { c = a + b ; a = b ; b = c ; System . out . print ( " " + c ) ; } System . out . println ( ) ; System . out . print ( t + "th value of the series is: " + c ) ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* Recursive Solution*/ import java.io.* ; import java.lang.* ; class Demo { int fib ( int n ) { if ( n == 1 ) return ( 1 ) ; else if ( n == 2 ) return ( 1 ) ; else return ( fib ( n - 1 ) + fib ( n - 2 ) ) ; } } class RecF...

Java Program to find the IP address of the Machine

1 2 3 4 5 6 7 8 9 10 11 12 13   import java.util.* ; import java.lang.* ; import java.net.* ;   public class GetOwnIP { public static void main ( String args [ ] ) { try { InetAddress ownIP = InetAddress . getLocalHost ( ) ; System . out . println ( "IP of my system is := " + ownIP. getHostAddress ( ) ) ; } catch ( Exception e ) { System . out . println ( "Exception caught =" + e. getMessage ( ) ) ; } } }

Program to calculate Compound Interest in Java

import java.util.Scanner; public class CompoundInterest {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);       double principal = 0;       double rate = 0;       double time = 0;       double compoundInterest = 0;         System.out.print("Enter the Principal amount : ");       principal = input.nextDouble();       System.out.print("Enter the Rate : ");       rate = input.nextDouble();       System.out.print("Enter the Time : ");       time = input.nextDouble();       compoundInterest = principal * Math.pow((1 + rate/100),time);         System.out.println("...

Difference between while for and do while loop in java

The main difference between for loop, while loop, and do while loop is While loop checks for the condition first. so it may not even enter into the loop, if the condition is false. do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false. for loop is similar to while loop except that initialization statement, usually the counter variable initialization a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement The following sample code explains all 3 different loops: Source Code import   java . lang .*; import   java . util .*; import   java . io .*; class   LoopsDifference {     public   static   void   main ( String []  args )    {            int   MAX  = 5; ...