UNIT-I Programs
“Java Programs- Basics of
Java”
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
DATE: 22 January 2025
Program: Write Java Program to print Welcome message on screen
class VJTech
{
public static void main(String args[])
{
System.out.println("Welcome to world of Java Programming");
}
}
OUTPUT
Welcome to world of Java Programming
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Program: Write Java Program to print BIO-DATA on screen
class BIOData
{
public static void main(String args[])
{
System.out.println("******BIO-DATA********");
System.out.println("NAME: Vishal Laxman Jadhav");
System.out.println("ADDRESS: Tuljapur");
System.out.println("Mobile No: 9730087674");
System.out.println("Education: MTech in Computer Engg");
System.out.println("Job: Working in IT Industry");
}
}
OUTPUT
******BIO-DATA********
NAME: Vishal Laxman Jadhav
ADDRESS: Tuljapur
Mobile No: 9730087674
Education: MTech in Computer Engg
Job: Working in IT Industry
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Program: Write Java Program to perform addition between two numbers
class Addition
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=5;
c=a+b;
System.out.println("Addition="+c);
}
}
OUTPUT
========
Addition=15
Program: Write Java Program to perform addition between two numbers
using command line arguments
class CommandLineArgs
{
public static void main(String args[])
{
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=a+b;
System.out.println("Addition="+c);
}
}
OUTPUT
========
C:\Javabatch2025\UNIT-I Prorgrams>javac CommandLineArgs.java
C:\Javabatch2025\UNIT-I Prorgrams>java CommandLineArgs 100 200
Addition=300
Program: Write Java Program to check number is EVEN or ODD using
command line arguments
class CommandLineArgs1
{
public static void main(String args[])
{
int no;
no=Integer.parseInt(args[0]);
if(no%2==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
}
OUTPUT
========
C:\Javabatch2025\UNIT-I Prorgrams>javac CommandLineArgs1.java
C:\Javabatch2025\UNIT-I Prorgrams>java CommandLineArgs1 24
Even Number
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Program: Write Java Program to swap two numbers
import java.util.*;
class ScannerDemo
{
public static void main(String args[])
{
int a,b,temp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Number:");
a=sc.nextInt();
System.out.println("Enter Second Number:");
b=sc.nextInt();
temp=a;
a=b;
b=temp;
System.out.println("***After swapping***");
System.out.println("a="+a+"\tb="+b);
}
}
OUTPUT
======
Enter First Number:
120
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Enter Second Number:
420
***After swapping***
a=420 b=120
Program: Write Java Program to implements Math Functions
import java.lang.*;
class MathFunction
{
public static void main(String args[])
{
int a=100,b=200;
System.out.println("Minimum Value:"+Math.min(a,b));
System.out.println("Maximum Value:"+Math.max(a,b));
System.out.println("Square Root of 9:"+Math.sqrt(9));
System.out.println("Pow(2,3):"+Math.pow(2,3));
System.out.println("Exp of 709.78222656=" +Math.exp(709.78222656));
System.out.println("round(200.675) = "+Math.round(200.675));
System.out.println("round(200.675) = "+Math.round(200.50));
System.out.println("round(200.675) = "+Math.round(200.20));
System.out.println("Absolute Value = "+Math.abs(-5944));
}
}
/*
OUTPUT
======
Minimum Value:100
Maximum Value:200
Square Root of 9:3.0
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Pow(2,3):8.0
Exp of 709.78222656 =1.7968190692375724E308
round(200.675) = 201
round(200.675) = 201
round(200.675) = 200
Absolute Value = 5944
Program: Write Java Program to calculate area of circle
import java.util.*;
class AreaOfCircle
{
public static void main(String args[])
{
final float PI=3.14f;
float area,radius;
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of radius:");
PI=1.1;
radius=sc.nextFloat();
area=(PI*radius*radius);
System.out.println("Area of Circle="+area);
}
}
/*
OUTPUT:
=======
Enter value of radius:
2.0
Area of Circle=12.56
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
Write a Java program to implement Implicit Type Casting
import java.util.*;
class ImplicitTypeCasting
{
public static void main(String args[])
{
int a=10;
float b;
b=a; //implicit type casting
System.out.println("Value of a="+a);
System.out.println("Value of b="+b);
}
}
OUTPUT:
=======
Value of a=10
Value of b=10.0
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
//Write a Java program to implement Explicit Type Casting
import java.util.*;
class ExplicitTypeCasting
{
public static void main(String args[])
{
int a=10;
float b;
b=(float)a; //Explicit type casting
System.out.println("Value of a="+a);
System.out.println("Value of b="+b);
}
}
OUTPUT:
=======
Value of a=10
Value of b=10.0
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
//Write a Java program to implement instanceof operator
import java.util.*;
class InstanceOfDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
if(sc instanceof Scanner)
{
System.out.println("sc is an object of Scanner class");
}
}
}
//Write a Java program to implement if statement
import java.util.*;
class IfStatementDemo
{
public static void main(String args[])
{
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first Number:");
a=sc.nextInt();
System.out.println("Enter second Number:");
b=sc.nextInt();
if(a==b)
{
System.out.println("Both Numbers are equal");
}
}
}
OUTPUT:
=======
Enter first Number:
25
Enter second Number:
25
Both Numbers are equal
Write a Java program to implement if-else statement
import java.util.*;
class IfElseStatementDemo
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first Number:");
a=sc.nextInt();
System.out.println("Enter second Number:");
b=sc.nextInt();
if(a==b)
{
System.out.println("Both Numbers are equal");
}
else
{
System.out.println("Both Numbers are not equal");
}
}
}
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
OUTPUT:
=======
Enter first Number:
25
Enter second Number:
25
Both Numbers are equal
//Write Java Program to check positive or negative number
import java.util.*;
class PositiveNegativeNo
{
public static void main(String args[])
{
int no;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any Number:");
no=sc.nextInt();
if(no!=0)
{
if(no>0)
{
System.out.println("Positive Number");
}
else
{
System.out.println("Negative Number");
}
}
else
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
{
System.out.println("Zero is neither positive nor negative");
}
}
}
OUTPUT:
=======
1) Result-1
Enter any Number:
15
Positive Number
2)Result-2
Enter any Number:
-10
Negative Number
3)Result-3
Enter any Number:
0
Zero is neither positive nor negative
Write a Java program to generate student mark grade on the basis of
following conditions.
marks>=75 - Distinction
marks>=60 - First Class
marks>=40 - Pass
marks<40 - Fail
import java.util.*;
class GradeMarks
{
public static void main(String args[])
{
int marks;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Marks:");
marks=sc.nextInt();
if(marks>=75)
{
System.out.println("You got Distinction!!!");
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
}
else if(marks>=60)
{
System.out.println("You got First Class!!!");
}
else if(marks>=40)
{
System.out.println("You are Pass Only!!!");
}
else
{
System.out.println("You are Fail!!!");
}
}
}
/*
OUTPUT:
=======
1) Result-1:
Enter Your Marks:
80
You got Distinction!!!
2)Result-2:
Enter Your Marks:
67
You got First Class!!!
3)Result-3:
Enter Your Marks:
45
You are Pass Only!!!
4) Result-4:
Enter Your Marks:
23
You are Fail!!!
Write Java Program to check whether no is Even or Odd
import java.util.*;
class EvenOddDemo
{
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
public static void main(String args[])
{
int no;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any Number:");
no=sc.nextInt();
if(no%2==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
}
/*
OUTPUT:
=======
Enter first Number:
25
Enter second Number:
25
Both Numbers are equal
Switch case statement (To check alphabet is Vowels or consonant)
class switchcase
{
public static void main(String args[])
{
char ch='p';
switch(ch)
{
case 'a': System.out.println("Alphabet is Vowel");
break;
case 'e': System.out.println("Alphabet is Vowel");
break;
case 'i': System.out.println("Alphabet is Vowel");
break;
case 'o': System.out.println("Alphabet is Vowel");
break;
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
case 'u': System.out.println("Alphabet is Vowel");
break;
default: System.out.println("Alphabet is consonant");
}
}
}
For loop statement
class ForLoopDemo
{
public static void main(String args[])
{
int i;
for(i=1;i<=5;i++)
{
System.out.println("VJTech Academy");
}
}
}
OUTPUT:
=======
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
While loop statement
class WhileLoopDemo
{
public static void main(String args[])
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
{
int i=1;
while(i<=5)
{
System.out.println("VJTech Academy");
i++;
}
}
}
OUTPUT:
=======
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
Do-While loop statement
class DoWhileLoopDemo
{
public static void main(String args[])
{
int i=1;
do
{
System.out.println("VJTech Academy");
i++;
}while(i<=5);
}
}
/*
OUTPUT:
=======
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
VJTech Academy
For Each loop statement
class ForEachLoopDemo
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android
UNIT-I Programs
{
public static void main(String args[])
{
int num[]={10,20,30,40,50};
System.out.println("Array Elements:");
for(int x:num)
{
System.out.println(x);
}
}
}
OUTPUT:
=======
Array Elements:
10
20
30
40
50
Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in
Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android