Lab Report Java
Lab Report Java
PROCESS: Here we will see two programs to add two numbers, In the first program we
specify the value of both the numbers in the program itself. The second programs takes both
the numbers (entered by user) and prints the sum.
PROGRAM:
public class AddTwoNumbers {
OUTPUT:
LAB-2
The scanner allows us to capture the user input so that we can get the values of both the
numbers from user. The program then calculates the sum and displays it.
PROGRAM:
import java.util.Scanner;
}}
num1 = sc.nextInt();
num2 = sc.nextInt();
sc.close();
OUTPUT:
LAB-3
class CheckEvenOdd
int num;
num = input.nextInt();
if ( num % 2 == 0 )
else
OUTPUT:
LAB-4
PROGRAM: Java Program to Add two Binary Numbers
PROCESS: In this program we are using Scanner to get the input from user (user
enters the two binary numbers that we need to add) and then we are adding them bit
by bit using while loop and storing the result in an array.
PROGRAM:
import java.util.Scanner;
int i = 0, carry = 0;
b1 = scanner.nextLong();
b2 = scanner.nextLong();
scanner.close();
while (b1 != 0 || b2 != 0)
b1 = b1 / 10;
b2 = b2 / 10;
}
if (carry != 0) {
sum[i++] = carry;
--i;
System.out.print("Output: ");
while (i >= 0) {
System.out.print(sum[i--]);
System.out.print("\n");
}
}
OUTPUT:
LAB-5
PROCESS:
import java.util.Scanner;
int year;
year = scan.nextInt();
scan.close();
if(year % 4 == 0)
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
else
isLeap = true;
}
else {
isLeap = false;
if(isLeap==true)
else
OUTPUT:
LAB-6
TITLE: PROGRAM TO CHECK VOWEL OR CONSONANT USING SWITCH CASE
PROCESS:In this program we are not using break statement with cases
intentionally, so that if user enters any vowel, the program continues to execute all
the subsequent cases until Case 'U' is reached and thats where we are setting up
the value of a boolean variable to true. This way we can identify that the alphabet
entered by user is vowel or not.
PROGRAM:
import java.util.Scanner;
class JavaExample
boolean isVowel=false;;
char ch=scanner.next().charAt(0);
scanner.close();
switch(ch)
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
if(isVowel == true) {
System.out.println(ch+" is a Vowel");
else {
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println(ch+" is a Consonant");
else
}
}
OUTPUT:
LAB-7
TITLE: JAVA PROGRAM TO CALCULATE COMPOUND INTEREST
PROCESS: IN THIS JAVA PROGRAM WE ARE CALCULATING THE
COMPOUND INTEREST
PROGRAM:
OUTPUT:
LAB-8 (A)
TITLE : PROGRAM TO CONVERT CHAR TO STRING
PROCESS: We have following two ways for char to String
conversion.
Method 1: Using toString() method
Method 2: Usng valueOf() method
PROGRAM:
class CharToStringDemo
char ch = 'a';
}
}
OUTPUT:
LAB-8(B)
TITLE: CONVERTING STRING TO CHAR
PROCESS: We can convert a String to char using charAt()
method of String class.
PROGRAM:
class StringToCharDemo
char ch = str.charAt(i);
OUTPUT:
LAB-9(A)
import java.util.Scanner;
class ReverseNumberWhile
int num=0;
num = in.nextInt();
while( num != 0 )
num = num/10;
}
}
}
OUTPUT:
LAB-9(B)
class RecursionReverseDemo
System.out.println(number);
return;
else {
System.out.print(number % 10);
reverseMethod(number/10);
int num=0;
reverseMethod(num);
System.out.println();
OUTPUT:
LAB-9(C)
int num=123456789;
while( num != 0 )
num = num/10;
OUTPUT:
LAB-10
TITLE: JAVA PROGRAM TO CHECK PRIME NUMBER
PROCESS:
PROGRAM:
import java.util.Scanner;
class PrimeCheck
int temp;
boolean isPrime=true;
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
if(isPrime)
else
OUTPUT:
LAB-11
TITLE: JAVA PROGRAM TO GET INPUT FROM USER
PROCESS:
PROGRAM:
import java.util.Scanner;
class GetInputData
int num;
float fnum;
String str;
str = in.nextLine();
num = in.nextInt();
OUTPUT:
LAB-12
TITLE: JAVA PROGRAM TO GET IP ADDRESS
Process:
In this example we are gonna see how to get IP address of a System.
The steps are as follows:
1) Get the local host address by calling getLocalHost() method of
InetAddress class.
2) Get the IP address by calling getHostAddress() method
PROGRAM:
import java.net.InetAddress;
class GetMyIPAddress
InetAddress myIP=InetAddress.getLocalHost();
System.out.println(myIP.getHostAddress());
OUTPUT: