Name-Megh Patel
div- B
Sub-Spm
Assignment-2
1. Check if a number input by user is a multiple of 27 or not.
package javaapp1;
import java.util.Scanner;
public class multiple27 {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter number n");
n=sc.nextInt();
if(n%27==0)
{
System.out.println("The Number Input by user is a multiple of 27");
}
else{System.out.println("the number input by user is not a multiple of 27");}
}
}
2. Depending on the age input by user, display if the user is a minor (0-17 yrs), adult
(18-59 yrs) or a senior citizen (60+ yrs).
package javaapp1;
import java.util.Scanner;
public class age {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int age;
System.out.println("Enter your age");
age=sc.nextInt();
if(age<=17)
{
System.out.println("You are a minor");
}
else if(age<=59)
{
Name-Megh Patel
div- B
Sub-Spm
System.out.println("You are an adult");
}
else
{
System.out.println("You are a senior");
}
}
}
3. Input all three angles of a triangle and display if it is a valid triangle or not.
(H
int :sum of all angles of a triangle= 180°)
package javaapp1;
import java.util.Scanner;
public class triangle {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a1,a2,a3;
System.out.println("Enter the first agnle of triangle");
a1=sc.nextInt();
System.out.println("Enter the second agnle of triangle");
a2=sc.nextInt();
System.out.println("Enter the third agnle of triangle");
a3=sc.nextInt();
if(a1+a2+a3==180)
{
System.out.println("It is a valid triangle");
}
else
{
System.out.println("It is not a valid triangle");
}
}
4. a)Check if user input is only of one character, otherwiseprint “Invalid input”.
(H
int :Use built-in method String.length()to check length of input String)
b)In the same code, further check if that characterinput by user is a vowel or a
consonant.
Name-Megh Patel
div- B
Sub-Spm
package javaapp1;
import java.util.Scanner;
public class character {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String a;
System.out.println("Enter a character");
a=sc.nextLine();
if(a.length()==1)
{
System.out.println("The Character is valid");
if(a.equals("a")||a.equals("e")||a.equals("i")||a.equals("o")||a.equals("u"))
{
System.out.println("The character is a vowel");
}
else{System.out.println("The character is a consonant");}
}
else{System.out.println("The character is invalid");}
}
}
5. Input cost price and selling price of a product and determine if there is profit or loss.
Display the final amount of profit / loss.
(H
int : Profit = SP - CP and Loss= CP – SP)
package javaapp1;
import java.util.Scanner;
public class profitloss {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int sp,cp,profit,loss;
System.out.println("Enter cost price");
sp=sc.nextInt();
Name-Megh Patel
div- B
Sub-Spm
System.out.println("Enter selling price");
cp=sc.nextInt();
if(sp<cp)
{
System.out.println("There is a profit");
profit=sp-cp;
System.out.println("Profit="+profit);
}
else{
System.out.println("There is a loss");
loss=cp-sp;
System.out.println("Loss="+loss);
}
}