prowessapps.in List of Basic Programs

 WAP to display Hello World!

class Hello {
 public static void main(String[] a)
 {
  System.out.print("Hello World!");
 }
}
OUTPUT :
Hello World!

 WAP to add two numbers.

import java.util.Scanner;
class Demo {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  int x = s.nextInt();
  System.out.print("Enter Num2: ");
  int y = s.nextInt();
  int z = x+y;
  System.out.println("Sum = "+z);
 }
}
OUTPUT :
Enter Num1: 5
Enter Num2: 6
Sum = 11 

 WAP to swap two values of 2 no.

import java.util.Scanner;
class Demo {
 public static void main(String[] a)
 {
  int x,y,temp;
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  x = s.nextInt();
  System.out.print("Enter Num2: ");
  y = s.nextInt();
  System.out.print("Before Swapping");
  System.out.println(" X="+x+" Y="+y);
  temp = x;
  x = y;
  y = temp;
  System.out.print("After Swapping ");
  System.out.println(" X="+x+" Y="+y);
 }
} 
OUTPUT :
Enter Num1: 10
Enter Num2: 20
Before Swapping X=10 Y=20
After Swapping  X=20 Y=10 

 WAP to swap two values without using third variable.

import java.util.Scanner;
class Demo {
 public static void main(String[] a)
 {
  int x,y,temp;
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  x = s.nextInt();
  System.out.print("Enter Num2: ");
  y = s.nextInt();
  System.out.print("Before Swapping");
  System.out.println(" X="+x+" Y="+y);
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.print("After Swapping ");
  System.out.println(" X="+x+" Y="+y);
 }
}  
OUTPUT :
Enter Num1: 10
Enter Num2: 20
Before Swapping X=10 Y=20
After Swapping  X=20 Y=10

 WAP to calculate the area of a circle.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Radius:");
  int r = s.nextInt();
  double ar = 3.14*r*r;
  System.out.println("Area = "+ar);
 }
}
OUTPUT :
Enter Radius:7
Area = 153.86

 WAP to find the cube of a number.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num: ");
  int x = s.nextInt();
  int y = x*x*x;
  System.out.print("Cube = "+y);
 }
}
OUTPUT :
Enter Num: 5
Cube = 125 


 WAP to find square root of a number.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 { 
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Number: ");
  int x = s.nextInt();
  double y = Math.sqrt(x);
  System.out.print("SQUARE ROOT = "+y);
 }
}
OUTPUT :
Enter Number: 5
SQUARE ROOT = 2.23606797749979

 WAP to find x to the power y (xy).

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Base: ");
  int x = s.nextInt();
  System.out.print("Enter Power: ");
  int y = s.nextInt();
  int r =(int) Math.pow(x,y);
  System.out.println("Result = "+r);
 }
}
OUTPUT :
Enter Base: 5
Enter Power: 3
Result = 125 

 WAP to convert temp from Fahrenheit to Celsius.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter temp in (F): ");
  int f  = s.nextInt();
  double c = (5.0/9.0)*(f-32);
  System.out.print("Temp in (C) = "+c);
 }
}
OUTPUT :
Enter temp in (F): 41
Temp in (C) = 5.0

 WAP to input time in millisecond and show in minuts and second.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Time in millis:");
  int tms = s.nextInt();
  int ts = tms/1000;
  int tm = ts/60;
  ts = ts%60;
  System.out.print("Time- "+tm+":"+ts);
 }
}
OUTPUT :
Enter Time in millis:75000
Time- 1:15

 WAP to find greatest among two numbers using ternary number.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  int x = s.nextInt();
  System.out.print("Enter Num2: ");
  int y = s.nextInt();
  int r = (x>y)?x:y;
  System.out.print("Largest = "+r);
 }
}
OUTPUT :
Enter Num1: 10
Enter Num2: 30
Largest = 30 

 WAP to find difference between two numbers, diff must be Positive.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter Num1: ");
  int x = s.nextInt();
  System.out.print("Enter Num2: ");
  int y = s.nextInt();
  int r = (x>y)?(x-y):(y-x);
  System.out.print("Diff. = "+r);
 }
}
OUTPUT :
Enter Num1: 12
Enter Num2: 15
Diff. = 3




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved