Saturday, April 4, 2015

Java Programs - Adding two Numbers,odd or Even Number, convert Fahrenheit - Class 5

Java program to add two numbers

Java program to add two numbers :- Given below is the code of java program that adds two numbers which are entered by the user.

Java programming source code

import java.util.Scanner;
 
class AddNumbers
{
   public static void main(String args[])
   {
      int x, y, z;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = x + y;
      System.out.println("Sum of entered integers = "+z);
   }
}
Download Add numbers program class file.
Output of program:
add numbers
Above code can add only numbers in range of integers(4 bytes), if you wish to add very large numbers then you can use BigInteger class. Code to add very large numbers:
import java.util.Scanner;
import java.math.BigInteger;
 
class AddingLargeNumbers {
  public static void main(String[] args) {
    String number1, number2;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Enter first large number");
    number1 = in.nextLine();
 
    System.out.println("Enter second large number");
    number2 = in.nextLine();
 
    BigInteger first  = new BigInteger(number1);
    BigInteger second = new BigInteger(number2);
    BigInteger sum;
 
    sum = first.add(second);
 
    System.out.println("Result of addition = " + sum);
  }
}
In our code we create two objects of BigInteger class in java.math package. Input should be digit strings otherwise an exception will be raised, also you cannot simply use '+' operator to add objects of BigInteger class, you have to use add method for addition of two objects.
Output of program:
Enter first large number
11111111111111
Enter second large number
99999999999999
Result of addition = 111111111111110
 
 

Java program to find odd or even

This java program finds if a number is odd or even. If the number is divisible by 2 then it will be even, otherwise it is odd. We use modulus operator to find remainder in our program.

Java programming source code

import java.util.Scanner;
 
class OddOrEven
{
   public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
 
      if ( x % 2 == 0 )
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
}
Download Odd or even program class file.
Output of program:
odd even
Another method to check odd or even, for explanation see: c program to check odd or even. Code:
import java.util.Scanner;
 
class EvenOdd
{
  public static void main(String args[])
  {
    int c;
    System.out.println("Input an integer");
    Scanner in = new Scanner(System.in);
    c = in.nextInt();
 
    if ( (c/2)*2 == c )
      System.out.println("Even");
    else
      System.out.println("Odd");
  }
}
There are other methods for checking odd/even one such method is using bitwise operator.
 
 

Java program to convert Fahrenheit to Celsius

Java program to convert Fahrenheit to Celsius: This code does temperature conversion from Fahrenheit scale to Celsius scale.

Java programming code

import java.util.*;
 
class FahrenheitToCelsius {
  public static void main(String[] args) {
    float temperatue;
    Scanner in = new Scanner(System.in);      
 
    System.out.println("Enter temperatue in Fahrenheit");
    temperatue = in.nextInt();
 
    temperatue = ((temperatue - 32)*5)/9;
 
    System.out.println("Temperatue in Celsius = " + temperatue);
  }
}
Download Fahrenheit to Celsius program class file.
Output of program:
Fahrenheit to Celsius Java program output
For Celsius to Fahrenheit conversion use
T = 9*T/5 + 32
where T is temperature on Celsius scale. Create and test Fahrenheit to Celsius program yourself for practice.
 

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...