Practice 02: Operators in Java
1.Write a program to implement arithmetic operators.
public class j04ArithmeticOperators {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
2.Write a program to add three numbers and find their sum and mean.
import java.util.Scanner;
public class j05threenumsummean {
public static void main(String[] args) {
int a, b, c;
int sum;
double mean;
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers: ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
sum = a+b+c;
System.out.println("Sum is: "+sum);
mean = sum/3.0;
System.out.println("Mean is: "+mean);
}
}
3.Write a program to read a radius of a circle and find its area and circumference.import java.util.Scanner;
public class j06radiusofcircle {
public static void main(String[] args) {
int r;
double a,c;
System.out.println("Enter radius of circle: ");
Scanner sc = new Scanner(System.in);
r = sc.nextInt();
a = 3.14*r*r;
System.out.println("Area of circle is: "+a);
c = 3.14*2*r;
System.out.println("Circumference of circle is: "+c);
}
}
4.Write a program to read the length and breadth of a rectangle and find it's area and perimeter.import java.util.Scanner;
public class j07rectangle {
public static void main(String[] args) {
int L,B,A,P;
Scanner sc= new Scanner(System.in);
System.out.println("Enter Length: ");
L = sc.nextInt();
System.out.println("Enter Breadth: ");
B =sc.nextInt();
A = L*B;
System.out.println("Area is: "+A);
P = 2*(L+B);
System.out.println("Perimeter is: "+P);
}
}5.Write a program to exchange the values of two variable using third variable.import java.util.Scanner;
public class j08exchangevalue {
public static void main(String[] args) {
int a,b,temp;
Scanner sc =new Scanner(System.in);
System.out.println("Enter Value of a: ");
a = sc.nextInt();
System.out.println("Enter Value of b: ");
b = sc.nextInt();
//Exchanging Values
temp = a;
a = b;
b = temp;
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}

Comments
Post a Comment