PROGRAMS ON OPERATORS OF JAVA
1.
public class Result
{
public static void main(String[] args)
{
int marks = 70;
String result = (marks > 40) ? "pass" : "fail";
System.out.println(result);
}
}
2.
class Largest
{
public static void main(String args[ ])
{
int a = 15, b= 25, c= 21,max;
max= (a>b)?((a>c)?a:c) : ((b>c)?b:c);
System.out.println("the largest number is :" + max);
}
}
PROGRAMS ON INPUT IN JAVA
1.
import java.util.*;
class Avg_marks
{
public static void main(String args[ ])
{
String name;
int m1,m2,m3;
double avg;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student");
name= sc.nextLine();
System.out.println("enter the marks obtained in 3 subjects:");
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
avg = (m1 + m2 + m3) / 3.0;
System.out.println("the average marks is :" + avg);
}
}
2.
import java.util.*;
class Discount
{
public static void main(String args[ ])
{
double p, s1, s2, d1, d2, d3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the price of the article:");
p = sc.nextDouble( );
d1 = p * 30 / 100.0;
s1 = p - d1;
System.out.println("30 % Discount given by first shop:" + d1);
System.out.println("Amount after discount:"+ s1);
d2 = p * 20 / 100.0;
s2= p - d2;
d3 = s2 * 10 / 100.0;
s2= s2 - d3;
System.out.println("20 % Discount given by second shop: " + d2);
System.out.println("10 % Discount given by second shop: " + d3);
System.out.println("Amount after discount:"+ s2);
}
}
Programs on Mathematical Library methods
1.
import java.util.Scanner;
public class VariousMethods
{
public static void main(String args[ ])
{
Scanner scan = new Scanner( System.in);
System.out.print("Enter first number:");
double firstNumber = scan.nextDouble( );
System.out.print("Enter the second number:");
double secondNumber = scan.nextDouble( );
System.out.println("***" + firstNumber + "***");
System.out.println("Absolute value: " + Math.abs ( firstNumber));
System.out.println("Rounded value: " + Math.round ( firstNumber));
System.out.println("Ceiling: " + Math.ceil ( firstNumber));
System.out.println("Floor: " + Math.floor ( firstNumber));
System.out.println("***" + secondNumber + "***");
System.out.println("Absolute value: " + Math.abs ( secondNumber));
System.out.println("Rounded value: " + Math.round ( secondNumber));
System.out.println("Ceiling: " + Math.ceil ( secondNumber));
System.out.println("Floor: " + Math.floor ( secondNumber));
System.out.println("*** Min/Max ***");
System.out.println("Minimum value:" + Math.min ( firstNumber, secondNumber));
System.out.println("Maximum value:" + Math.max ( firstNumber, secondNumber));
}
}
WRITE OUTPUT
2.
public class Number
{
public static void main(String args[])
{
int p = 64;
int q = 2;
double r1 = Math.pow(p, q);
System.out.println("p to the power of q = " + r1);
double r2 = Math.sqrt(p);
System.out.println("Square root of p = " + r2);
}
}
Programs on Conditional Constructs
1.
import java.util.Scanner;
public class ArkenstoneConsulting
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of hours: ");
int hrs = in.nextInt();
System.out.print("Enter total sales: ");
int sales = in.nextInt();
double wage = hrs * 500;
double c = 0;
if (sales < 100)
c = 0;
else if (sales < 1000)
c = 1;
else if (sales < 10000)
c = 2;
else if (sales < 25000)
c = 3;
else
c = 3.5;
double comm = c * sales / 100.0;
wage += comm;
System.out.println("Wage = " + wage);
}
}
Write output
2.
import java.util.Scanner;
public class Number
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Composite Number");
System.out.println("Type 2 for Smallest Digit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch)
{
case 1:System.out.print("Enter Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
c++;
}
if (c > 2)
System.out.println("Composite Number");
else
System.out.println("Not a Composite Number");
break;
case 2: System.out.print("Enter Number: ");
int num = in.nextInt();
int s = 10;
while (num != 0)
{
int d = num % 10;
if (d < s)
s = d;
num /= 10;
}
System.out.println("Smallest digit is " + s);
break;
default:System.out.println("Wrong choice");
}
}
}
WRITE OUTPUT
3.
import java.util.Scanner;
public class PythagoreanTriplet
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter 1st number: ");
int a = in.nextInt();
System.out.print("Enter 2nd number: ");
int b = in.nextInt();
System.out.print("Enter 3rd number: ");
int c = in.nextInt();
if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
System.out.println("Pythagorean Triplets");
else
System.out.println("Not Pythagorean Triplets");
}
}
WRITE OUTPUT
4.
import java.util.Scanner;
public class MayurTpt
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter parcel weight: ");
double wt = in.nextDouble();
double amt = 0;
if (wt <= 10)
amt = 30 * wt;
else if (wt <= 30)
amt = 300 + ((wt - 10) * 20);
else
amt = 300 + 400 + ((wt - 30) * 15);
System.out.println("Parcel Charge = " + amt);
}
}
Programs on iterative constructs