- Write a Java program to find the area and perimeter of a circle.
class Area
{
public static void main(String
args[])
{
double pi, radius, area,perimeter;
radius = 10;
pi = 3.1416;
area = (pi*(radius*radius));
perimeter =(2*pi*radius);
System.out.println("Area of circle is " + area);
System.out.println("perimeter of the circle is " +perimeter);
}
}
- Write a program to check whether a number is odd or even.
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("plz enter an integer to check weather odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}
- Write a program that computes the number of miles that light will travel in a specified number of days..
class LightSpeed
{
public static void main(String args[])
{
int lightspeed;
long days;
long seconds;
long distance;
// Intializing the speed of light in miles per second
lightspeed = 186282;
days = 1000;
// Intialize the number of days here
seconds = (days*(24 * 60 * 60));
// Days is converted to seconds
distance = lightspeed * seconds;
// calculating the total distance that light covers
System.out.print("In " + days +" days light will
travel about " + distance + " miles.");
}}
- Write a Java Program to compute the factorial of a number.
class Factorial
{
public static void main(String args[])
{
int number=10, count, fact = 1;
System.out.println("Defalut no is :" +number);
for ( count = 1 ; count <= number ; count++ )
{
fact = fact*count;
}
System.out.println("Factorial of "+number+" is = "+fact);
}
- Write a program to swap two numbers (use a function to swap).
class SwapNumbers
{
public static void main(String args[])
{
int
x_int=10, y_int=20, temp_int;
System.out.println("Before
Swapping\nx = "+x_int+"\ny = "+y_int);
temp_int
= x_int;
x_int = y_int;
y_int = temp_int;
System.out.println("After Swapping\nx = "+x_int+"\ny = "+y_int);
}
}
- Write a program to add two numbers passed as the command line argument to the program. If only one number is passed as the command line argument, find the square root of that number.
import java.lang.*;
class Add {
public static void main(String[] args) {
int sum = 0;
int sqr = 0;
int i =0;
if (args.length ==1)
{
sqr = Integer.parseInt(args[i]);
{
System.out.println("square root is"+Math.sqrt(sqr));
}}
else{
for ( i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);}
System.out.println("The sum of the arguments passed is " + sum);
}}}
- Write a program to find the largest among three numbers
import java.util.Scanner;
public class Largest_no
{
public static void main(String[] args)
{
int no1, no2, no3, largest;
Scanner s =
new Scanner(System.in);
System.out.println("Enter all three numbers:");
no1 = s.nextInt();
no2 = s.nextInt();
no3 = s.nextInt();
largest = no3 > (no1 > no2 ? no1 : no2) ? no3 : ((no1 > no2) ? no1 : no2);
System.out.println("Largest Number is :"+largest);
}
}
- Write a program to find all the numbers divisible by 7 between 1 and 100.
class Divisble_7
{
public
static void main(String arg[])
{
int i;
System.out.println("No. divisible by 7 in between 1 and 100");
for(i=1;i<=100;i++)
{
if(i%7==0)
{
System.out.println(i );
}}}}
No comments:
Post a Comment