Java Programs 2
Java Programs 2
/*
This program shows how to calculate
area of circle using it's radius.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
int radius = 0;
System.out.println("Please enter radius of a circle");
try
{
//get the radius from console
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
/*
* Area of a circle is
* pi * r * r
* where r is a radius of a circle.
*/
/*
Output of Calculate Circle Area using Java Example would be
14
Please enter radius of a circle
19
Area of a circle is 1134.1149479459152
*/
Program12 - Factorial of a number using recursion
/*
This program shows how to calculate
Factorial of a number using recursion function.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Output of this Java example would be
15
Program13 – pyramid of numbers using for loops
/*
This Java example shows how to generate a pyramid of numbers for given
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
System.out.println("Enter Number:");
System.out.println("Enter X:");
int y = 0;
16
for(int j=1; j <= i ; j++){
System.out.print(y + "\t");
y = y + x;
System.out.println("");
/*
Enter Number:
Enter X:
1 2
3 4 5
6 7 8 9
10 11 12 13 14
----------------------------------------------
17
Enter Number:
Enter X:
2 4
6 8 10
12 14 16 18
20 22 24 26 28
----------------------------------------------
Enter Number:
Enter X:
3 6
9 12 15
18 21 24 27
30 33 36 39 42
*/
18
Program14 – To Find Maximum of Two Numbers.
/*
*/
class Maxoftwo{
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
if(i > j)
else
*/
class Minoftwo{
int i = Integer.parseInt(args[0]);
19
int j = Integer.parseInt(args[1]);
20
Program 16
/* Write a program that will read a float type value from the keyboard and
print the following output.
->Given Number.
*/
class ValueFormat{
class RandomDemo{
for(int i=1;i<=5;i++){
System.out.println((int)(Math.random()*100));
21
}
switch(marks/10){
case 10:
case 9:
case 8:
System.out.println("Excellent");
break;
case 7:
System.out.println("Very Good");
break;
case 6:
System.out.println("Good");
break;
case 5:
System.out.println("Work Hard");
break;
case 4:
System.out.println("Poor");
break;
case 3:
22
case 2:
case 1:
case 0:
System.out.println("Very Poor");
break;
default:
while(temp>0){
temp--;
temp = num;
23
result = 1;
temp--;
int result=0;
for(int i=100;i<=200;i++){
if(i%7==0)
result+=i;
Input - 5
24
Output - 1 2 3 4 5 */
class Join{
for(int i=1;i<=num;i++){
System.out.println(result);
System.out.println("*****MULTIPLICATION TABLE*****");
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
System.out.print("\n");
25