Basic Java Programs

In this Post we will learn fundamental Java Programs for Beginners and final year students,these programs will improve your programming skills. Let's start now

1. Java program to display a message

// Hello World example

class FirstProgram
{
public static void main(String args[])
{
System.out.print("Hello coding4world");

}
}

Output:

Hello coding4world

2. Java Program to find area of circle

class circle
{
public static void main("String args[])
{
double rad;
final double PI=3.1415;
rad=10.0;
double area=PI*rad*rad;
System.out.print("Area of circle is="+area);
}
}

Output:
Area of circle is= 314.15

3. Java Program to find area of triangle

class Triangle
{
public static void main(String args[])
{
double area,p,q,r,s;

p=3;
q=4;
r=5;
s=(p+q+r)/2;
area=Math.sqrt(s*(s-p)*(s-q)*(s-r));
System.out.print("Area of triangle is=+area);
}
}

output:

Area of trianlge is=6.0

4. Java Program to find Area of  Rectangle

import java.util.Scanner;
class AreaOfRectangle
{
public static void main(String args[])
{
int length;
int breadth;
int area;
scanner sc=new Scanner(System.in);
System.out.print("Enter the length of Rectangle");
length=sc.nextInt();
System.out.print("\nEnter the Breadth of Rectangle");
breadth=sc.nextInt();
 area=length*breadth;
System.out.print("\n Area of Rectangle:"+area);
}
}

Output:
Enter the length of Rectangle: 5
Enter the breadth of Rectangle: 6
Area of Rectangle : 30

5. Java program to find the volume of Spehere

import java.util.Scanner;
class Spehere
{
public static void main(String args[]);

{
float radius;

float volume;
Scanner sc= new Scanner(System.in);
System.out.print("enter radius of the sphere");
radius=sc.nextFloat();
volume=(float)((4*3.14*radius*radius* radius)/3;
System.out.print(\n valume of sphere is: +volume);
}
}

Output:

Enter radius of the sphere 2
Volume of sphere is: 33.493

6. Java program to covert Celsius To Fahrenheit

import java.util.Scanner;
class CelsiusToFahrenheit
{
public static void main(String args[])
{

float c;
float f;
Scanner sc=new Scanner(System.in);
System.out.print("Enter temp in centigrade:");
c=sc.nextFloat();
f=(float)((1.8*c)+32.0);
System.out.print("Temperature in Fahrenheit="+f);
}
}


Output:
Enter temp in centigrade: 100
Temperature in Fahrenheit=212

7. Java Program to find LCM

import java.util.Scanner;
class LCM
{
public static void main(String args[])

{
int a,b;
int big,small;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of a and b");
a=sc.nextInt();
b=sc.nextInt();
if(a>b)
{
big=a;

small=b;
}
else

{
big=b;
small=a;
}
for(int i=1;i<=big;i++)
{
if(((big*i)%small)==0)
{
int lcm=big*i;

System.out.print("The LCM is"+(lcm));
break;
}
}

}
}

Output:
Enter the value of a and b:
10
15
The LCM is 30

8. Java Program to find Hexagon

import java.util.Scanner;
class Hexagon
{
public static void main(String args[])
{
int side;

float area;
Scanner sc=new Scanner(System.in);
System.out.print("enter the Length of side:");
side=sc.nextInt();
area=(float)(3*Math.squre(3)*side*side)/2;
System.out.print("\n Area of Regular Hexagon:"+area);
}
}
Output:
Enter the length of side: 3
Area of Regular Hexagon: 23.38

9.Java Program to find Volume of Hemisphere

import java.util.Scanner;
class Hemisphere
{
public static void main(String args[]);

{
float radius;

float volume;
System.out.print("\n enter radius of the hemisphere");
radius=sc.nextFloat();
volume=(float)((2*3.14*radius)/3);
System.out.print("\n volume of hemisphere is:"+volume);
}
}
Output:
Enter radius of the hemisphere: 6

Volume of hemispere is: 12.64

10. Java program to find Volume of cone

import java.util.Scanner;
class Cone
{
public static void main(String args[])

{
float r;

float h;
float volume;
Scanner sc=new Scanner(System.in);
System.out.print("Enter radius of the cone:");
r=sc.nextFloat();
System.out.print("enter height of the Cone:");
h=sc.nextFloat();
volume=(float)((3.14*r*r*h)/3);
System.out.print("\n volume of cone is:"+volume);
}
}

Output:
Enter radius of the cone: 5

Enter height of the cone: 8
Volume of the cone is: 209.33






No comments:

Post a Comment

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...