Basic Java Programs
Basic Java Concept Assignment -- User Input
1. WAP to swap two numbers without using temp variable.
package assignments;
import java.util.Scanner;
public class Swap2NumbersWithoutTemp {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int number1, number2;
System.out.println("Enter one number: ");
number1=sc.nextInt();
System.out.println("Enter second number: ");
number2= sc.nextInt();
System.out.println("Numbers before swapping: ");
System.out.println(number1);
System.out.println(number2);
// swap with temp variable
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
System.out.println("Numbers after swapping are: ");
System.out.println(number1);
System.out.println(number2);
}
}
Output:
Enter one number:
3
Enter second number:
4
Numbers before swapping:
3
4
Numbers after swapping are:
4
3
2. WAP program to calculate Perimeter of Circle.
package assignments;
import java.util.Scanner;
public class CircumfrenceCircle {
public static void main(String[] args) {
int r;
double circumference;
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius:");
r= sc.nextInt();
sc.close();
circumference = 2 * 3.14 * r;
System.out.println("Circumference of the circle is: " + circumference);
}
}
Output:
Enter radius:
3
Circumference of the circle is: 18.84
3. WAP program to find Area of Circle.
package assignments;
import java.util.Scanner;
public class AreaCircle {
public static void main(String[] args) {
System.out.println("Area of circle");
int radius;
double circumference;
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius:");
radius= sc.nextInt();
sc.close();
circumference = 3.14 * (radius*radius);
System.out.println("Area of the circle is: " + circumference);
}
}
Output:
Area of circle
Enter radius:
2
Area of the circle is: 12.56
4. WAP program to find Area of Rectangle.
package assignments;
import java.util.Scanner;
public class Area5 {
public static void main(String[] args) {
int number1, number2, total;
Scanner sc = new Scanner(System.in);
System.out.println("Input length:");
number1 = sc.nextInt();
System.out.println("Input width:");
number2 = sc.nextInt();
sc.close();
total = number1 * number2;
System.out.println("Area of the rectangle: " + total);
}
}
Output:
Input length:
3
Input width:
2
Area of the rectangle: 6
5. WAP program to find Area of Triangle.
package assignments;
import java.util.Scanner;
public class AreaOfTriangle {
public static void main(String[] args) {
int number1, number2, total;
Scanner sc = new Scanner(System.in);
System.out.println("Input base:");
number1 = sc.nextInt();
System.out.println("Input height:");
number2 = sc.nextInt();
sc.close();
total = number1 * number2/2;
System.out.println("Area of the triangle: " + total);
}
}
Output:
Input base:
2
Input height:
10
Area of the triangle: 10
6. WAP program to calculate Simple Interest.
package assignments;
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
int p, t, r;
double interest;
Scanner sc = new Scanner(System.in);
System.out.println("Enter principle: ");
p = sc.nextInt();
System.out.println("Enter time: ");
t = sc.nextInt();
System.out.println("Enter rate: ");
r = sc.nextInt();
sc.close();
interest = (p * t * r) / 100;
System.out.println("Simple Interest " + interest);
}
}
Output:
Enter principle:
100
Enter time:
1
Enter rate:
1
Simple Interest 1.0
No comments: