Java program to add two numbers
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); } }
Output of program:
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); } }
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.
Download Odd or even program class file.
Output of program:
Another method to check odd or even, for explanation see: c program to check odd or even. Code:
There are other methods for checking odd/even one such method is using bitwise operator.
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."); } }
Output of program:
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"); } }
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.
Download Fahrenheit to Celsius program class file.
Output of program:
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.
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); } }
Output of program:
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