Hello world program in java
class Hello {
public static void main(String args[]) {
System.out.println("Hello World !");
}
}
Sum of two number in java
class Sum {
public static void main(String args[]) {
int num1,num2,sum;
num1=5;
num2=6;
sum = num1+num2;
System.out.println("Sum is "+ sum);
}
}
Sum of two number in java input by user
import java.util.Scanner;
class SumUserChoice {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
int num1,num2,sum;
System.out.println("Enter the first number : ");
num1 = readin.nextInt();
System.out.println("Enter the second number : ");
num2 = readin.nextInt();
sum = num1+num2;
System.out.println("Sum is "+sum);
}
}
Comparision of two integer input bu user program in java
import java.util.Scanner;
class NumberComparision {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
int num1,num2;
System.out.println("Enter the first number:");
num1 = readin.nextInt();
System.out.println("Enter the second number:");
num2 = readin.nextInt();
if(num1>num2) {
System.out.println(num1+" is greater than "+ num2);
}
else {
System.out.println(num1+ " is less than "+ num2);
}
}
}
Program to find entered number is even or odd in java
import java.util.Scanner;
class EvenOrOdd {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
int num;
System.out.print("Enter the number to check number is even or odd :");
num = readin.nextInt();
if(num%2==0) {
System.out.println(num+" is a even number .");
} else {
System.out.println(num+" is a odd number .");
}
}
}
Program to find the factorial of a integer number in java
import java.util.Scanner;
class Factorial {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the number to find the factorial :");
int num = readin.nextInt();
for(int i=num;i>0;i--)
{
int fact = fact * i;
}
System.out.println("Factorial of "+num+" is "+fact);
}
}
Check the number is palindrome or not program in java
import java.util.Scanner;
class Palindrome {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the number to check the number is palindrome or not :");
int num = readin.nextInt();
int no=num;
while(num>0)
{
int re=num%10;
int palin=re+(palin*10);
num=num/10;
}
if(no == palin) {
System.out.println(no+" is a palindrome number.");
}
else {
printf(no+" is not a palindrome number.");
}
}
}
Check the number is armstrong or not program in java
import java.util.Scanner;
class Armstrong {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
int no,num,mul=1,sum=0,i,re;
System.out.println("Enter the number to check the number is armstrong or not :");
num = readin.nextInt();
no=num;
while(num>0) {
re = num%10;
for(i=0;i<3;i++)
{
mul=mul*re;
}
sum=sum+mul;
num=num/10;
mul=1;
}
if(no == sum) {
System.out.println(no+" is a Armstrong number .");
} else {
System.out.println(no+" is not a Armstrong number .");
}
}
}
Number reverse program in java
import java.util.Scanner;
class ReverseNumber {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the number :");
int num = readin.nextInt();
while(int no>0) {
int re=no%10;
int rev=re+(rev*10);
no=no/10;
}
System.out.println("Reverse of "+num+" is "+rev);
}
}
Swapping of two number using 3rd variable program in java
import java.util.Scanner;
class SwappingThirdVar {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = readin.nextInt();
System.out.println("Enter the second number");
int num2 = readin.nextInt();
int temp = num1;
num1 = num2;
num2 = temp;
System.out.println(" num1 is "+num1+"\n num2 is"+num2);
}
}
Swapping of two number without using 3rd variable program in java
import java.util.Scanner;
class SwappingWithoutUsingThirdVar {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = readin.nextInt();
System.out.println("Enter the second number");
int num2 = readin.nextInt();
num1=num1+num2;
num2 = num1-num2;
num1 = num1-num2;
System.out.println(" num1 is "+num1+"\n num2 is"+num2);
}
}
Fabonacci series program in java
import java.util.Scanner;
class FabonacciSeries {
public static void main(String args[]) {
Scanner raedin = new Scanner(System.in);
System.out.println("Enter the range :");
int range = readin.nextInt();
Systme.out.println("Fabonnaci sereis is :");
int num1 = 0,num2 = 1;
for(int i = 0;i<range;i++) {
System.out.print(num1+" ");
num1=num2;
int fab=num1+num2;
num2=fab
}
}
}
Program to find average of 3 numbers in java
import java.util.Scanner;
class Average {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.print("Enter the numbers :");
int num1 = readin.nextInt();
int num2 = readin.nextInt();
int num3 = readin.nextInt();
int avg = (num1+num2+num3)/3;
System.out.println("Average of numbers is "+avg);
}
}
Print factors of a number in java
import java.util.Scanner;
class Factors {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the number :");
int num = readin.nextInt();
System.out.println("Factors of "+num+"are");
for(int i=1 ;i<=num ; i++)
{
if(num%i==0)
{
System.out.print(i+" ");
}
}
}
}
Power of integers program in java
import java.util.Scanner;
class PowerOfInteger {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in);
System.out.println("Enter the integer number :");
int num = readin.nextInt();
System.out.println("Enter the power number :");
int pow = readin.nextInt();
for(int i=0;i<pow;i++)
{
int res=res*num;
}
System.out.println("Result is "+pow);
}
}
Area of triangle program in java
import java.util.Scanner;
class Area {
public static void main(String args[]) {
Scanner readin = new Scanner(System.in)
System.out.println("Enter the height of triangle :");
int height = readin.nextInt();
System.out.println("Enter the base of triangle :");
int base = readin.nextInt();
ar = (base*height)/2;
System.out.println("Area of triangle is :"+ar);
}
}