[Go to site: main page, start]

0% found this document useful (0 votes)
8 views6 pages

Java Programs for Beginners

The document contains multiple Java programs demonstrating basic programming concepts such as printing text, performing arithmetic operations, swapping numbers, checking even or odd, and finding the largest number among three inputs. Each program includes necessary imports, class definitions, and methods to execute the desired functionality. User input is utilized in some programs to enhance interactivity.

Uploaded by

freeup435
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Java Programs for Beginners

The document contains multiple Java programs demonstrating basic programming concepts such as printing text, performing arithmetic operations, swapping numbers, checking even or odd, and finding the largest number among three inputs. Each program includes necessary imports, class definitions, and methods to execute the desired functionality. User input is utilized in some programs to enhance interactivity.

Uploaded by

freeup435
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program1: Write a Java Program to print Hello World!

import [Link].*;
class HelloProgram
{
public static void main(String args[])
{
[Link]("Hello World!");
}
}

Program2: Write a Java Program to calculate the addition of two numbers.


import [Link].*;
class Addition
{
public static void main(String args[])
{
int a=10,b=20,c=0;
[Link]("Value of a="+a);
[Link]("Value of b="+b);
c=a+b;
[Link]("Addition of Two number="+c);
//[Link]("Addition of Two number="+(a+b));
}
}
Program3: Write a Java Program to calculate the addition of two numbers (Read values from user).
import [Link].*;
import [Link].*;
class AddProgram
{
public static void main(String args[])
{
try
{
int a=0,b=0,c=0;
Scanner oS=new Scanner([Link]);
[Link]("Enter First Number:");
a=[Link]();//Read value of first number in a
[Link]("Enter Second Number:");
b=[Link]();//Read value of second number in b
[Link]("Value of a="+a);
[Link]("Value of b="+b);
c=a+b;
[Link]("Addition of Two number="+c);
//[Link]("Addition of Two number="+(a+b));
}
catch(Exception e)
{
[Link](e);
}
}
}
Program4: Write a Java Program to swap two numbers.
import [Link].*;
class SwapNo
{
public static void main(String args[])
{
int x=10,y=20,temp=0;
[Link]("Before Swapping x="+x+"& y="+y);

temp=x;
x=y;
y=temp;
[Link]("After Swapping x="+x+"& y="+y);

}
}
Program5: Write a Java Program to swap two numbers (Read values from user).
import [Link].*;
import [Link].*;
class NumberSwap
{
public static void main(String args[])
{
try
{
int x=10,y=20,temp=0;
Scanner oS=new Scanner([Link]);
[Link]("Enter First Number:");
x=[Link]();//Read value of first number in x
[Link]("Enter Second Number:");
y=[Link]();//Read value of second number in y
[Link]("Before Swapping x="+x+"& y="+y);
temp=x;
x=y;
y=temp;
[Link]("After Swapping x="+x+"& y="+y);
}
catch(Exception e)
{
[Link](e);
}
}
}
Program6: Write a Java Program to check given number Even or Odd.
import [Link].*;
import [Link].*;
class EvenOdd
{
public static void main(String args[])
{
try
{
int iNo=0;
Scanner oS=new Scanner([Link]);
[Link]("Enter Any integer Number:");
iNo=[Link]();
if(iNo%2==0)
{
[Link]("The given number "+iNo+" is Even Number.");
}
else
{
[Link]("The given number "+iNo+" is Odd Number.");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
Program7: Write a Java Program to find the largest number from three numbers.
import [Link].*;
import [Link].*;
class LargestNo
{
public static void main(String args[])
{
try
{
int iNum1=0,iNum2=0,iNum3=0;
Scanner oS=new Scanner([Link]);
[Link]("Enter First integer Number:");
iNum1=[Link]();
[Link]("Enter Second integer Number:");
iNum2=[Link]();
[Link]("Enter Third integer Number:");
iNum3=[Link]();
if(iNum1>iNum2 && iNum1>iNum3)
{
[Link]("The Largest Number is "+iNum1);
}
else if(iNum2>iNum1 && iNum2>iNum3)
{
[Link]("The Largest Number is "+iNum2);
}
else
{
[Link]("The Largest Number is "+iNum3);
}
}
catch(Exception e)
{
[Link](e);
}
}
}

You might also like