Java Assignment 2
Java Assignment 2
}
}
Output:
//2. Print whether number is even or odd
import java.util.Scanner;
public class posOrNeg {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a number : ");
int a=input.nextInt();
if (a<0)
System.out.println("Number is negative");
else if (a>0)
System.out.println("Number is positive");
else
System.out.println("Number is zero");
}
}
Outputs:
//3. Print smallest of three numbers
import java.util.Scanner;
public class smallestOfThree {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter number 1 : ");
int a=input.nextInt();
System.out.print("Enter number 2 : ");
int b=input.nextInt();
System.out.print("Enter number 3 : ");
int c=input.nextInt();
if ((a<b)&&(a<c))
System.out.println(a+" is the smallest
number");
else if ((b<a)&&(b<c))
System.out.println(b+" is the smallest
number");
else
System.out.println(c+" is the smallest
number");
}
}
Output:
//4. Print whether two floats are equal
import java.util.Scanner;
public class decimalThree {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a float : ");
float a=input.nextFloat();
System.out.print("Enter a float : ");
float b=input.nextFloat();
float c=(a%1000)/1000;
float d=(b%1000)/1000;
if (c==d)
System.out.println("Numbers are same ");
else
System.out.println("Numbers are not
same");
}
}
Output:
//5.Print whether number is positive or negative or 0
import java.util.Scanner;
public class posOrNeg {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a number : ");
int a=input.nextInt();
if (a<0)
System.out.println("Number is negative");
else if (a>0)
System.out.println("Number is positive");
else
System.out.println("Number is zero");
}
}
Outputs:
//6. Print day given the number
import java.util.Scanner;
public class weekDays{
public static void main(String[] args){
int num;
System.out.println("Enter a number between 1
and 7:");
Scanner Sc=new Scanner(System.in);
num=Sc.nextInt();
switch (num){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
Output :
//7. Print numbers of days in a given year and a
given month
import java.util.Scanner;
public class numOfDays {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter year : ");
int a=input.nextInt();
System.out.print("Enter month number : ");
int b=input.nextInt();
if (a%4==0){
for (int i=1;i<13;i++){
if
(((i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i==10)||(i
==12))&&(i==b))
System.out.println("31 days");
else if
(((i==4)||(i==6)||(i==9)||(i==11))&&(i==b))
System.out.println("30 days");
else if ((i==2)&&(i==b))
System.out.println("29 days");
}
} else if (a%4!=0){
for (int i=1;i<13;i++){
if
(((i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i==10)||(i
==12))&&(i==b))
System.out.println("31 days");
else if
(((i==4)||(i==6)||(i==9)||(i==11))&&(i==b))
System.out.println("30 days");
else if ((i==2)&&(i==b))
System.out.println("28 days");
}
}
}
}
Outputs:
//8. Print discounted bill
import java.util.Scanner;
public class discountBill{
public static void main(String[] args){
float
prod1,prod2,prod3,prod4,prod5,bill,disc,amt;
System.out.println("Enter the price of 5
products:");
Scanner Sc=new Scanner(System.in);
prod1=Sc.nextFloat();
prod2=Sc.nextFloat();
prod3=Sc.nextFloat();
prod4=Sc.nextFloat();
prod5=Sc.nextFloat();
amt=prod1+prod2+prod3+prod4+prod5;
if ((amt>5000) && (amt<10000)){
disc=amt*0.10f;
bill=amt-amt*0.10f;
System.out.println("10%"+"\n"+disc+"\n"+bill);
}
else if ((amt>10000) && (amt<50000)){
disc=amt*0.3f;
bill=amt-amt*0.3f;
System.out.println("30%"+"\n"+disc+"\n"+bill);
}
else if (amt>50000){
disc=amt*0.5f;
bill=amt-amt*0.5f;
System.out.println("50%"+"\n"+disc+"\n"+bill);
}
else
{
System.out.println(amt);
}
}
}
Output: