Saturday, January 31, 2015

Java program to understand the break operation

The java program given below will print the numbers from 0-9, though in the for-loop there is given i<=50. According this condition, the program should print the numbers from 0-50.

But inside the for-loop there is an if statement with the condition i==10, that means when the value of i will 10 then the statement break; will be executed.

That's why after printing the numbers from 0-9 the loop will be stopped, though in the loop I have told to print the numbers from 0-50.
break operation,java program,break operation in java

//java program to understand the break operation
public class breakOp{
 public static void main(String [] args){
  //for loop to print 0-50
  for(int i=0; i<=50; i++){
   //if statement to check the value of i
   if(i==10){
    //stopping the loop if the condition is true
    break;
   }
   //displaying the data stored in i
   System.out.print(i+", ");
  }
 }
}


Java program to multiply two matrices after getting input

The program given below is able to multiply two matrices. After running the program, you will be asked to provide the number of rows of the first matrix and then you will be asked to provide the number of columns.

In the next step you will be asked to provide the data/element of the first matrix and soon the matrix will be displayed. Besides, you will be asked to provide the number of columns for the second matrix.
import java.util.Scanner;

class MatrixMultiplication{
	public static void main(String args[]){
		//declaring integer type data
		int i, j, k, sum=0;

		//creating scaner object
		Scanner in = new Scanner(System.in);

		//getting the number of rows and columns
		System.out.print("Enter the number of rows of first matrix: ");
		int rows1 = in.nextInt();
		System.out.print("Enter the number of columns of first matrix: ");
		int col1 = in.nextInt();

		int first[][] = new int[rows1][col1];

		//getting input for the first matrix
		System.out.println("\n\nEnter the data of first matrix:");
		for(i=0; i<rows1; i++){
			for(j=0; j<col1; j++){
				first[i][j]= in.nextInt();
			}
		}

		//Displaying first matrix
		System.out.println("\n\nThe first matrix is:");
		for(i=0; i<rows1; i++){
			for(j=0; j<col1; j++){
				System.out.print(first[i][j]+"\t");
			}System.out.print("\n\n\n");
		}

		//getting the number of rows and columns of the second matrix
		System.out.print("\n\nEnter the number of rows of second matrix: ");
		int rows2 = in.nextInt();
		System.out.print("Enter the number of columns of second matrix: ");
		int col2 = in.nextInt();

		//checking if the operation is possible or not
		if (col1 != rows2){
			System.out.println("\n\nThe operation is impossible!");
			System.out.println("As the number od rows and columns are not same.");
		} else {
			int second[][] = new int[rows2][col2];
			int multiply[][] = new int[rows1][col2];

		//getting input for the second matrix
		System.out.println("\n\nEnter the data of second matrix");
		for (i=0; i<rows2; i++){
			for (j=0; j<col2; j++){
				second[i][j] = in.nextInt();
			}
		}


		//displaying the second matrix
		System.out.println("\n\nThe second matrix is:");
		for(i=0; i<rows2; i++){
			for(j=0; j<col2; j++){
				System.out.print(second[i][j]+"\t");
			}System.out.print("\n\n\n");
		}

		//multiplying two matrices
		for (i=0; i<rows1; i++){
			for (j=0; j<col2; j++){
				for (k=0; k<rows2; k++){
					sum=sum+first[i][k]*second[k][j];
					}
               multiply[i][j] = sum;
               sum = 0;
               }
        }

		//displaying the product
		System.out.println("\n\nProduct of entered matrices:");
		for (i=0; i<rows1; i++){
			for (j=0; j<col2; j++){
				System.out.print(multiply[i][j]+"\t");
				}
				System.out.print("\n\n\n\n");
			}
		}
   }
}


Java program to add two matrix after getting data from keyboard

The java program given below is able to add two matrices after getting all data from the keyboard. After running the program, you will be asked to provide the number of rows for the first matrix and soon you will be asked to provide the number of columns.

After providing the number of rows and columns then you will asked to provide the data for the matrix. Soon the matrix will be displayed on the screen. Besides, you will be asked to provide the number of rows and columns of the second matrix. Again you will be asked to provide the data for the second matrix.

Again the second matrix will be displayed and the summation of those two matrices will be displayed.
adding two matrices,summation of matrices
import java.util.*;
public class Matrix{
 public static void main(String [] args){
  //creating scanner object
  Scanner input = new Scanner(System.in);

  //getting input for the number of rows
  System.out.print("Enter the number of Rows: ");
  int row = input.nextInt();

  //getting input for the number of columns
  System.out.print("Enter the number of Columns: ");
  int col = input.nextInt();

  //creating double array
  int matrix[][] = new int[30][30];
  int matrix2[][] = new int[30][30];
  int sum[][] = new int[30][30];

  //getting input into the array
  System.out.print ("Enter data for first Matrix:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    matrix[i][j] = input.nextInt();
   }
  }

  //displaying the first matrix
  System.out.println("The first matrix is:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    System.out.print(matrix[i][j]+"    ");
   } System.out.print("\n\n\n");
  }

  //getting input into the array
  System.out.print ("Enter data for second Matrix:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    matrix2[i][j] = input.nextInt();
   }
  }

  //displaying the second matrix
  System.out.println("The second matrix is:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    System.out.print(matrix2[i][j]+"    ");
   } System.out.print("\n\n\n");
  }

  //adding two mattrix
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    sum[i][j] = matrix[i][j] + matrix2[i][j];
   }
  }

  //displaying the summation
  System.out.println("The summation is:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    System.out.print(sum[i][j]+"    ");
   } System.out.print("\n\n\n");
  }
 }
}


Friday, January 30, 2015

Java program to convert a matrix into transpose matrix

The java program given below will create a matrix after getting all data from the user. Later the matrix will be converted to a transpose matrix. Transpose matrix is a matrix where the rows of a matrix become the columns to the new matrix.

After running the program you will be asked to provide the number of rows and columns one by one. Then you will be asked to provide the data for the matrix and soon the matrix will be formed and displayed.

Besides, the matrix will be converted to a transpose matrix and it will be displayed.
transpose matrix,converting matrix
import java.util.*;
public class Matrix{
 public static void main(String [] args){
  //creating scanner object
  Scanner input = new Scanner(System.in);

  //getting input for the number of rows
  System.out.print("Enter the number of Rows: ");
  int row = input.nextInt();

  //getting input for the number of columns
  System.out.print("Enter the number of Columns: ");
  int col = input.nextInt();

  //creating double array
  int matrix[][] = new int[30][30];

  //getting input into the array
  System.out.print ("Enter data for first Matrix:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    matrix[i][j] = input.nextInt();
   }
  }

  //displaying the matrix
  System.out.println("The Matrix is:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    System.out.print(matrix[i][j]+"   ");
   } System.out.print("\n\n\n");
  }

  //converting transpose matrix
  System.out.println("The Transpose Matrix is:\n");
  for(int i=1; i<=col; i++){
   for(int j=1; j<=row; j++){
    System.out.print(matrix[j][i]+"   ");
   } System.out.print ("\n\n\n");
  }
 }
}


Wednesday, January 28, 2015

Java program to print out a matrix

The java program given below will print a matrix after getting the data from the users. When you will run this program, then you will be asked to provide the number of rows of the matrix and later you will be asked to provide the number of columns.

Then you have to provide the data of the matrix one by one. If you want to display a matrix of 2 rows and 2 columns, then you have to provide (2*2) 4 data. The first two data will be for the first row and the next two data will be for the second row.

A double array is used to store several data for the matrix. Not only declaring a double array, we also need to use a for loop inside another for loop to store data in the double array. Similarly, we need to use another set of for loop to display the matrix.
java program,matrix in java
import java.util.*;

public class Matrix{
 public static void main(String [] args){
  //creating scanner object
  Scanner in = new Scanner(System.in);
  
  //showing message to enter the number of rows
  System.out.print("Enter the number of rows: ");
  int rows = in.nextInt();

  //showing message to enter the number of columns
  System.out.print("Enter the number of columns: ");
  int columns = in.nextInt();
  
  //declaring a double array array
  int matrix[][] = new int[30][30];

  //getting input
  System.out.println("\n\nEnter the data...");
  for(int i=1; i<=rows; i++){
   for(int j=1; j<=columns; j++){
    matrix[i][j] = in.nextInt();
   }
  }

  //showing the matrix
  System.out.println("\n\nThe matrix is...");
  for(int i=1; i<=rows; i++){
   for(int j=1; j<=columns; j++){
    System.out.print(matrix[i][j]+"  ");
   } System.out.print("\n\n");
  }
 }
}


Tuesday, January 27, 2015

Array to store string type data and displaying them using for loop

The java program given below will store several string type data into an array and later the stored data will be displayed using a for loop. When you will run this program then you will be asked to provide a limit (if you provide the limit 3, then you will be able to store 3 string type data). After providing the limit, you will be asked to store the string type data (for example: your friends name, name of several programming languages etc.).

After providing the data, soon they will be displayed.
string type array, array in java, string type array in java
import java.util.*;

public class CharArray{
 public static void main(String [] args){
  //creating scanner object
  Scanner in = new Scanner(System.in);
  
  //declaring string type array
  String friends[] = new String[50];
  
  //message to enter the limit
  System.out.print("Enter the limit: ");
  
  //storing the limit into the variable j
  int j = in.nextInt();

  //for loop to store data into the array
  for(int i=0; i<=j; i++){
   friends[i] = in.nextLine();
  }

  //for loop to display the stored data
  for(int i=0; i<=j; i++){
   System.out.print(friends[i]+"\n");
  }
 }
}


Monday, January 26, 2015

Uses of array in java

The java program given below will store integer type data into an array and later the data will be displayed. First of all I have declared an integer type array month by the line int month[] = new int[30];

Later I have created a scanner object to get input from keyboard using the line Scanner in = new Scanner(System.in);

By the 12th line System.out.print("Enter the limit: "); a message will be displayed to enter a limit. There in the array we can store 31 integer type data in total, but by providing the limit we can control it. If we provide the limit 5, then only 5 integer type data will be stored and displayed.
array, integer type array, array in java
By the 13th line provided limit will be stored into the integer type variable j. Later there you will found two for loops, first one is to store the data and the second one is to display them.
import java.util.*;

public class ArrayTest{
 public static void main(String [] args){
  //declaring integer type array
  int month[] = new int[30];
  
  //creating a scanner object
  Scanner in = new Scanner(System.in);
  
  //getting input from keyboard
  System.out.print("Enter the limit: ");
  int j = in.nextInt();
  
  //for loop to get input into the array
  for (int i=0; i<=j;i++){
   month[i]=in.nextInt();
  }
  
  //for loop for displaying data from array
  for(int i=0; i<=j;i++){
   System.out.print(month[i]+", ");
  }
 }
}


Sunday, January 25, 2015

The numbers divisible by 5 between 1-100

The java program given below will check the numbers between 1-100 whether it is divisible by 5 or not. After execution there will be displayed those numbers only what are divisible by 5.

This program is almost same to the last two programs. A while-do loop is being used to check the numbers one by one divisible by 5 or not. Mainly the if statement is checking the numbers of the loop before printing.
if statement,do-while loop,divisible by 5
public class Divi5{
 public static void main(String arg []){
  //a simple message
  System.out.println("The numbers divisible by 5 are:");

  //initializing integer type variable i
  int i=1;

  //do...while loop
  do{
   //if statement to check the numbers for remainder
   if(i%5==0){
   //displaying the expected numbers
   System.out.print(i+", ");
  }
   i++;
  } while (i<=100);//condition for do...while loop
  //for two line break
  System.out.println("\n\n");
 }
}


Java program to print odd numbers between 1-100

The java program given below will print the odd numbers between 1-100. I have used a do...while loop to print the odd numbers one by one. An if statement is also there inside the do...while loop to check whether the number is odd or even.

All the numbers is being checked by the condition if(i%2==1). That means all the numbers are being divided by 2 to check if any remainder found.

We know if remainder 1 found after dividing a number by 2, then it is obviously an odd number. So the program is written to print those numbers only.

In the condition there is written while (i<=100);, that means the loop will continue 100 times. If we change 100 to 50, then all the odd numbers between 1-50 will be printed.
odd numbers,java program,do-while-loop
public class OddNum{
 public static void main(String arg []){
  //a simple message
  System.out.println("The odd numbers are:");

  //initializing integer type variable i
  int i=1;

  //do...while loop
  do{
   //if statement to check the numbers for remainder
   if(i%2==1){
   //displaying the expected numbers
   System.out.print(i+", ");
  }
   i++;
  } while (i<=100);//condition for do...while loop
  //for two line break
  System.out.println("\n\n");
 }
}


Friday, January 23, 2015

Printing the even numbers between 1 to 100 in java program


The java program given below will print all the even numbers between 1 to 100. To do so I have used do...while loop. Inside the loop I have used an if statement to check if the number is even or not.
printing even numbers,do while loop
public class EvenNum{
 public static void main(String arg []){
  int i=1;
  do{
   if(i%2==0){
   System.out.print(i+", ");
  }
   i++;
  } while (i<=100);
 }
}


Thursday, January 22, 2015

Java program to check a number whether it is Armstrong or not


This program will check a number if it is Armstrong number. For example: 153 is an Armstrong number where 13+53+3= 1+125+27 = 153. Thus 407 = 43 + 03 + 73 = 64 + 0 + 343 = 407 is also an Armstrong number.
armstrong number,java program,checking armstrong number
import java.util.Scanner;

public class ArmstrongNumber{
 public static void main(String args []){
  //declaring integer type variables
  int num, temp, r=0, sum=0;

  //getting input from keyboard
  System.out.print("Enter a number: ");
  Scanner in = new Scanner(System.in);
  num = in.nextInt();

   //keeping the number into temp variable
  temp = num;

  //while loop to check whether the number is armstrong or not
  while(temp != 0){
   r = temp%10;
   sum = sum + r*r*r;
   temp = temp/10;
  }

  //if...else condition to print the result
  if(num==sum){
   System.out.println(num+" is an Armstrong");
  } else{
   System.out.println(num+" is not an Armstrong");
  }

 }
}


Java program to check a number whether it is prime or not

This java program will check a number whether it is prime or not. 3, 5, 7,13 are some prime numbers are divisible by 1 and the same number.

After running the program you will be asked to provide a number to check. Later the program will check the number by the do...while loop.

First of all it will be checked if there is any remainder after divided by 2. If there is no any remainder, then it is obviously not a prime number. The loop will be stopped. But if any remainder found, then that number will be divided by the next number 3.
java program, checking prime number,while do loop
import java.util.Scanner;

public class PrimeNumber{
	public static void main(String [] args){
		//declaring integer type variables
		int number, i=2, result=0;

		//message to provide a number
		System.out.print("Enter a number: ");
		
		//getting input from keyboard
		Scanner in = new Scanner(System.in);
		number = in.nextInt();

		//do while loop to check the logic
		do{
			if(number%i==0){
				result=1;
			}

			i++;
		} while (i<number);

		//if else statement to display the result
		if(result==1){
			System.out.println(number +" is not a prime number");
		} else {
			System.out.println(number +" is a prime number");
		}
	}
}


Java program to check a year whether it is leap year or not

The java program given below is a program that is able to check a year whether it is a leap year or not. This java program is written simply but work effectively. I have used normal if...else condition to check a year.

After running the program, a message will be displayed to enter a year. After entering the year it will be stored in an integer type variable year.

Later we have to divide the year by 100 and need to check if there is any remainder or not. If there is no any remainder after dividing by 100, then we need to divide it by 400 again. If there is no any remainder even after dividing by 400, then we can declare it as a leap year.
java leap year,leap year in java,java program to check leap year
import java.util.Scanner;

public class LeapYear{
 public static void main(String [] args){
  //declaring integer type variable
  int year;

  //displaying message to input a year
  System.out.print("Enter a year: ");

  //getting input from keyboard
  Scanner in = new Scanner(System.in);
  year = in.nextInt();

  //checking whether it is leap year or not
  if(year%100==0 || year%4==0 && year%400==0){
   System.out.println(year + " is a Leap Year");
  } else {
   System.out.println(year + " is not a Leap Year");
  }
 }
}


Wednesday, January 21, 2015

Input a letter and check whether it is vowel or not

This java program will check a character if it is vowel or not. I have used the switch case statement to check the character/letter whether it is vowel or consonant.

After running this java program, you will be asked to provide a letter. When you will input a letter using your keyboard, the the letter you have recently input will be stored into a variable called letter here.

Later this stored letter will be compared in the cases.
switch case,vowel or not,checking a letter

import java.util.Scanner;

public class VowelConsonant{
 public static void main(String [] args){
  //delaring a string type variable
  String letter;

  //getting input from keyboard
  Scanner in = new Scanner(System.in);
  System.out.print("Enter a letter: ");
  letter = in.nextLine();

  //comparing the letter with the cases if match found
  switch(letter){
   case "a":
   case "A":
   System.out.println("This is a vowel");
   break;

   case "e":
   case "E":
   System.out.println("This is a vowel");
   break;

   case "i":
   case "I":
   System.out.println("This is a vowel");
   break;

   case "o":
   case "O":
   System.out.println("This is a vowel");
   break;

   case "u":
   case "U":
   System.out.println("This is a vowel");
   break;

   //if no matches, declaring as consonant
   default:
   System.out.println("This letter is consonant");
   break;
  }
 }
}


Java program to check the number whether it is odd or even


The java program given below will check a number whether it is odd or even. After running the program, you will be asked to provide a number. Later that number will be checked to find out whether it is odd or even. Finally a message will be displayed to inform you if the number is odd or not.
odd or even number,java program,whether odd or even

import java.util.Scanner;

public class OddEven{
 public static void main(String [] args){
  //integer type variables declaration
  int number, remainder;

  //getting input a number from keyboard
  Scanner in = new Scanner(System.in);
  System.out.print("Enter the number you want to check: ");
  number = in.nextInt();

  //checking for remainder
  remainder = number%2;

  //if else statement to make a decision
  if(remainder==1){
   System.out.println("The number is Odd");
  } else {
   System.out.println("The number is Even");
  }
 }
}


Finding out remainder after getting data from keyboard

The java program given below is a program that will find out the remainder of the mathematical operation division. When we divide a number by another then we get it. The fifteenth line remainder = num1%num2; is being used here to calculate the remainder.

After executing the program, you will be asked to provide the largest number first that will be stored into the variable num1. Soon you will be asked to provide the smallest number that will be stored into the variable num2.

Later the mathematical operation will be performed in the fifteenth line and the remainder will be stored into the variable remainder. Finally the result will be displayed by the line System.out.println("The remainder is: "+remainder);
import java.util.Scanner;
public class RemainderIn{
 public static void main(String [] args){
 //declaring three integer type variables
 int num1, num2, remainder;
 //getting input from keyboard
 Scanner in = new Scanner(System.in);
 System.out.print("Enter the largest number: ");
 num1 = in.nextInt();
 System.out.print("Enter the smallest number: ");
 num2 = in.nextInt();
 //calculating the remainder
 remainder = num1%num2;
 //displaying the result
 System.out.println("The remainder is: "+remainder);
 }
}


Tuesday, January 20, 2015

Java switch case to make a choice


Switch case in java programming allows us to make a decision. The program given below is such a program.

After running the program it will ask you to press a number between 1 and 2. If you press 1 then you will be able to perform the mathematical operation addition. After pressing 1, you will be asked to provide two numbers to perform the selected mathematical operation.

Similarly if you press 2, then you will be able to perform the mathematical operation Subtraction. After pressing 2, you will be asked to provide 2 numbers for subtraction.

Thus we may perform more operations using cases. Stitch case is being used also in c programming and in web designing.
java program, switch case programming,decision making program
import java.util.Scanner;
public class SwitchCase{
 public static void main(String [] args){
  //showing message for users
  System.out.println("Enter your Choice:\nPress 1 for Addition\nPress 2 for Subtraction");
  //getting input from keyboard
  int choice;
  Scanner in = new Scanner(System.in);
  choice = in.nextInt();

  //declaring four double type varriables for mathematical operations
  double num1,num2,addition,subtraction;

  //message to store the first number
  System.out.print("\n\nEnter a Number: ");
  //storing double type data into the variable num1
  num1 = in.nextDouble();

  System.out.print("Enter another Number: ");
  num2 = in.nextDouble();

  //switch case start
  switch(choice){
   //this section will be executed if choice=1 found
   case 1:
   addition=num1+num2;
   System.out.println("Addition is: "+addition);
   break;
   
   //this section will be executed if choice=2 found
   case 2:
   subtraction=num1-num2;
   System.out.println("Subtraction is: "+subtraction);
   break;

   default:
   break;
  } //ending switch case
 }
}

Advertisement

Saturday, January 17, 2015

Getting input several integer and double type data for addition and multiplication

The java program given below will perform two mathematical operations addition and multiplication. The operations will be performed using four integer type data and four double type data.

When you will run the program then you will be asked to provide four integer type data one by one and soon you will get the summation & product of those four numbers.

Similarly you will be asked to provide four double type data and later you will get the result.
java programming,input double type data,several mathematical operations

import java.util.Scanner;
 
public class AddMulin{
 public static void main(String [] args){
  //declaring integer type variables
  int n1=0, n2=0, n3=0, n4=0, r1=0, r2=0;
 
  //getting input from keyboard
  Scanner in = new Scanner(System.in);
  System.out.print("First Integer Number=");
  n1 = in.nextInt();
 
  System.out.print("Second Integer Number=");
  n2 = in.nextInt();
 
  System.out.print("Third Integer Number=");
  n3 = in.nextInt();
 
  System.out.print("Fourth Integer Number=");
  n4 = in.nextInt();
 
  //performing mathematical operation addition
  r1=n1+n2+n3+n4;
  //showing the result
  System.out.println("\nSum of 4 integer numbers is:"+r1);
  //performing mathematical operation multiplication
  r2=n1*n2*n3*n4;
  //showing the result
  System.out.println("Product of 4 integer numbers is:"+r2);
 
  //declaring double type variables
  double num1=0, num2=0, num3=0, num4=0, r3=0, r4=0;
  
  //getting input from keyboard
  System.out.print("\n\nFirst Double Number=");
  num1 = in.nextDouble();
 
  System.out.print("Second Double Number=");
  num2 = in.nextDouble();
 
  System.out.print("Third Double Number=");
  num3 = in.nextDouble();
 
  System.out.print("Fourth Double Number=");
  num4 = in.nextDouble();
 
  //performing mathematical operation addition
  r3=num1+num2+num3+num4;
  //showing the result stored in variable r3
  System.out.println("\nSum of 4 double numbers is:"+r3);
  //performing mathematical operation multiplication
  r4=num1*num2*num3*num4;
  //showing the result stored in variable r4
  System.out.println("Product of 4 double numbers is:"+r4);
 }
}


Saturday, January 10, 2015

Finding distance of two points using java program

The java program given below will calculate the distance of two points using the mathematical rule c2=a2+b2. Basically we have to found the value of c from the equation. So we need to use the Square Root mathematical operation using Math.sqrt.

That's why we have to import a class using the line import java.lang.*;. Otherwise the method Math.sqrt will not function and obviously will return error message while compiling the program.

As we have to find out c from the equation c2=a2+b2, so we need to know the value of a and b. The program given below won't get any value from the keyboard. The values are predefined for all the variables a, b and c. c is initialized with the value 0, so that there shouldn't stored any garbage value. 
java program,study java program,mathematical operation
import java.lang.*;

public class Dis2Point{
   public static void main(String [] args){
      double a=5, b=2, c=0;
      c=Math.sqrt(a*a+b*b);
      System.out.println("Distance is="+c);
   }
}


Finding the remainder using java program


The java program given below will find out the remainder after dividing 5 by 2. The remainder will be calculated using the sixth line r=a%b; and the result will be stored into the variable r. Later the result will be displayed using the eighth line System.out.println("Remainder is="+r);

public class Remainder{
  public static void main(String [] args){
  //declaring variables
  int a=5, b=2, r=0;
  //operation to find out the remainder
  r=a%b;
  //printing the remainder stored in the variable r
  System.out.println("Remainder is="+r);
 }
}

Let's do the same program in different way

Finding velocity from v2=u2+2as using java

import java.lang.*;

public class velocity2{
public static void main(String [] args){
double u=5, a=3.5, s=2, v=0;
v=Math.sqrt(u*u+2*a*s);
System.out.println("Velocity is="+v);
}
}

Finding velocity from v=u+at using java

public class velocity{
public static void main(String [] args){
double u=5, a=3.5, t=2, v=0;
v=u+a*t;
System.out.println("Velocity is="+v);
}
}

Java program to find out F using F=ma

public class Fma{
public static void main(String [] args){
double f=100, m=30, a=3.5;
f=m*a;
System.out.println("Force F="+f);
}
}

Friday, January 9, 2015

Area and volume of various geometrical figures

public class AreaVol{
 public static void main(String [] args){
  double lr=3, wr=2, hr=0.5, ar=0, vr=0;
  ar=lr*wr;
  System.out.println("Area of rectangle="+ar);
  vr=lr*wr*hr;
  System.out.println("Volume of rectangle="+vr);

  double bt=2, ht=3.2, at=0;
  at=bt*(ht/2);
  System.out.println("\nArea of triangle="+at);

  double pi=3.1416, rc=3.7, ac=0;
  ac=pi*rc*rc;
  System.out.println("\nArea of circle="+ac);

  double rade1=3, rade2=3.75, ae=0;
  ae=pi*rade1*rade2;
  System.out.println("\nArea of Ellipse="+ae);

  double sideSq=3.6, aSq=0;
  aSq=sideSq*sideSq;
  System.out.println("\nArea of Square="+aSq);

  double hTr=2.6,base1Tr=3.4,base2Tr=4.8,aTr=0;
  aTr=hTr*((base1Tr+base2Tr)/2);
  System.out.println("\nArea of trapezoid="+aTr);

  double diag1r=3, diag2r=9, adiag=0;
  adiag=(diag1r*diag2r)/2;
  System.out.println("\nArea of Rhombus="+adiag);

  double rSphr=5.3, aSphr=0, vSphr=0;
  aSphr=4*pi*rSphr*rSphr;
  System.out.println("\nArea of Sphere="+aSphr);
  vSphr=(4*pi*rSphr*rSphr*rSphr)/3;
  System.out.println("Volume of Sphere="+vSphr);

  double rCyl=2.3, hCyl=5.6, aCyl=0, vCyl=0;
  aCyl=2*pi*rCyl*hCyl+2*pi*rCyl*rCyl;
  System.out.println("\nArea of Sphere="+aCyl);
  vCyl=pi*rCyl*rCyl*hCyl;
  System.out.println("Volume of Cylinder="+vCyl);
  }
}

Square root function in Java

import java.lang.*;

public class xValue{
public static void main(String [] args){
double a=3, b=2, c=11, x=0;
x=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println("The value of x is:"+x);
}
}

Finding result from equation using java

public class Equ{
public static void main(String [] args){
float a=9, b=2, c=3, x=6, y=5, z=1, m=0, p=0;
m=a*x-b*y+c/z;
System.out.println("The value of m is:"+m);

p=a*a+2*a*b-b*b;
System.out.println("The value of p is:"+p);
}
}

Java program to print difference and quotient of 2 integer and double type numbers

public class SubDiv{
public static void main(String [] args){
int n1=9, n2=6, r1=0, r2=0;
r1=n1-n2;
System.out.println("Difference between 2 integer numbers is:"+r1);
r2=n1/n2;
System.out.println("Quotient of 2 integer numbers is:"+r2);

double num1=9, num2=6, r3=0, r4=0;
r3=num1-num2;
System.out.println("Difference between 2 double numbers is:"+r3);
r4=num1/num2;
System.out.println("Quotient of 4 double numbers is:"+r4);
}
}

Java program to print sum and product of 4 integer and double type numbers

public class AddMul{
public static void main(String [] args){
int n1=5, n2=6, n3=4, n4=9, r1=0, r2=0;
r1=n1+n2+n3+n4;
System.out.println("Sum of 4 integer numbers is:"+r1);
r2=n1*n2*n3*n4;
System.out.println("Product of 4 integer numbers is:"+r2);

double num1=5, num2=6, num3=4, num4=9, r3=0, r4=0;
r3=num1+num2+num3+num4;
System.out.println("Sum of 4 double numbers is:"+r3);
r4=num1*num2*num3*num4;
System.out.println("Product of 4 double numbers is:"+r4);
}
}

Thursday, January 8, 2015

Java program to get input from keyboard and to display that


The java program given below will ask to provide a number, that will be stored into a variable and later the stored data will be displayed. This program will process the integer type data as I have used the line a = in.nextInt();
java input,inputing integer number,input and output java

Java program to get an integer number as input from keyboard and to display that
import java.util.Scanner;

public class InputOutput{
    public static void main(String [] args){
        int a;

        System.out.println("Provide an integer value for a:");
        Scanner in = new Scanner(System.in);
        a = in.nextInt();
        System.out.println("Stored int value in a ="+a);
    }
}



Wednesday, January 7, 2015

Java program to calculate the average of five numbers


The program given below is a Java program that will add five numbers, will calculate the average and later the result will be displayed. Five float type variables n1, n2, n3, n4 and n5 are used to store five numbers and another variable avg to store the result. This java program won't get any input from the keyboard. That's why the variables are initialized with some values.
java program,calculating average of five numbers

The following Java program will calculate the average of five numbers
public class Avg {
public static void main(String [] args) {
float n1=5;
float n2=7;
float n3=9;
float n4=1;
float n5=6;
float avg=0;

avg=(n1+n2+n3+n4+n5)/5;

System.out.println("The average is="+avg);
}
}


Tuesday, January 6, 2015

Java program to perform several arithmetic operations

The Java program given below is to perform several arithmetic operations. I have used six float type variables n1, n2, sum, sub, mul and div. n1 and n2 is used to store two data that will be used for several arithmetic operations. The program will not get any input from the keyboard, that's why the variables n1 and n2 are initialized by 7 and 3 respectively.

The next four 4 variables sum, sub, mul and div are initialized by 0. Later the line sum=n1+n2; is used to find out the sum of the numbers stored into the variables n1 and n2. Similarly, the line sub=n1-n2; is to find out the difference, the line mul=n1*n2; is to find out the product and the line div=n1/n2; is to find out the quotient. These four lines will perform the mathematical operations and will store the results into the variables sum, sub, mul and div.

Finally the stored result will be displayed by the next four lines. For example the line System.out.println("The Sum is="+sum); will display the result of addition stored into the variable sum.
java program,mathematical operations,arithmetic operations
Java program to Add, to Subtract, to Multiply and to Divide two numbers...
public class ArithOp
{
public static void main(String [] args)
{
float n1=7;
float n2=3;
float sum=0;
float sub=0;
float mul=0;
float div=0;

sum=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n1/n2;

System.out.println("The Sum is="+sum);
System.out.println("The Difference is="+sub);
System.out.println("The Product is="+mul);
System.out.println("The Quotient is="+div);
} }


Monday, January 5, 2015

Java program to add two integer numbers

The Java program given below will add two integer numbers and will store the result into a variable. Later the result will be displayed with the appropriate message. Actually I have used three integer type variables a, b, sum. The variables a, b, sum are initialized with the values 5, 7 and 0 sequentially.

Later, I have added a and b, stored the result into the variable sum using the line sum=a+b;

Finally, I have showed the result using the line System.out.println("The addition is="+sum); We have to use the line System.out.println("any message"); to print any message. sum in the line System.out.println("The addition is="+sum); is used to print the data stored into it. And + (plus sign) is used to concatenate the stored result with the message.
Java program,addition in java,summation in java
Java program to add two integer numbers...
public class Addition
{
public static void main(String [] args)
{
int a=5;
int b=7;
int sum=0;

sum=a+b;
System.out.println("The addition is="+sum);
} }


Java program to print multiline


The java program given below will print multiline after running. This is a very simple java program like the HelloWorld program. In HelloWorld program we printed a single meassage Hello World! using the line System.out.println("Hello World!"); and in this program we have to use the same line for five times.

But we may do the same thing by the second program. In the second program, I have used the print command once but five several messages with /n.
java programming,object oriented programming,multiline in java
The first program to print multiline...
public class MultiLine
{
public static void main(String [] args)
{
System.out.println("1. This is the first line");
System.out.println("2. This is the second line");
System.out.println("3. This is the third line");
System.out.println("4. This is the fourth line");
System.out.println("5. This is the fifth line");
}
}

The second program to print multiline...
public class MultiLine
{
public static void main(String [] args)
{
System.out.println("1. First line\n2. Second line\n3. Third line\n4. Fourth line\n5. Fifth line");
}
}