Java Assign

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

1. Write a Java program to accept two integers and check whether they are equal or not.

Ans:

import java.util.*;

public class IntegerEqual

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int n1=sc.nextInt();

sc.nextLine();

int n2=sc.nextInt();

sc.nextLine();

if(n1==n2)

System.out.println("Equal");

else

System.out.println("Not Equal");

}
2. Write a java program to check whether a given number is even or odd.

Ans-

import java.util.*;

public class Odd_Even

public static void main(String[] args)

int n;

Scanner s = new Scanner(System.in);

n = s.nextInt();

s.nextLine();

if(n % 2 == 0)

System.out.println(n+" is Even ");

else

System.out.println(n+" is Odd ");

3. Write a java program to check whether a given number is positive or negative.


Ans:
import java.util.*;
public class PosNeg
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
sc.nextLine();
if(n1>0)
{
System.out.println("positive");
}
else if(n1<0)
{
System.out.println("negative");
}
}
}

4. Write a java program to find whether a given year is a leap year or not.
Ans:
import java.util.*;
public class Leap
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int lp=s.nextInt();
s.nextLine();
if(lp%400==0)
{
System.out.println("Leap year");
}
else if((lp%4==0) && (lp%100==0))
{
System.out.println("leap year");
}
else
{
System.out.println("not a leap year");
}
}
}

5. Write a java program to read the age of a candidate and determine whether it is eligible for
casting his/her own vote.

Ans:

import java.util.*;

public class Age

public static void main(String[] args)

Scanner s=new Scanner(System.in);

int a=s.nextInt();

s.nextLine();

if(a>=18)

System.out.println("Eligible");

else

System.out.println("Not Eligible");

}
6. Write a Java program to accept the height of a person in centimeter and categorize the
person according to their height.

Ans:

import java.util.*;

public class Height

public static void main(String args[])

int h;

Scanner s = new Scanner(System.in);

h = s.nextInt();

s.nextLine();

if(h > 175)

System.out.println("tall");

else if(h > 155 && h <= 175)

System.out.println("average height");

else

System.out.println("dwarf");
}

7. Write a java program to find the largest of three numbers.

Ans:

Import java.util.*;

public class Larger

public static void main(String[] args)

int a,b,c;

Scanner sc=new Scanner(System.in);

a=sc.nextInt();

sc.nextLine();

b=sc.nextInt();

sc.nextLine();

c=sc.nextInt();

sc.nextLine();

if(a>b && a>c)

System.out.println("a is greater");
}

else if(b>a && b>c)

System.out.println("b is greater");

else

System.out.println("c is greater");

8. Write a Java program to find the eligibility of admission for a professional course based on
the following criteria: Eligibility Criteria : Marks in Maths >=65 and Marks in Phy >=55 and
Marks in Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140
------------------------------------- Input the marks obtained in Physics :65 Input the marks
obtained in Chemistry :51 Input the marks obtained in Mathematics :72 Total marks of
Maths, Physics and Chemistry : 188 Total marks of Maths and Physics : 137 The candidate is
not eligible.

Ans:

import java.util.*;

public class Criteria

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter marks in Physics: ");

int phy = sc.nextInt();

sc.nextLine();

System.out.print("Enter marks in Chemistry: ");

int chem = sc.nextInt();

sc.nextLine();

System.out.print("Enter marks in Mathematics: ");

int maths = sc.nextInt();

sc.nextLine();

int total = phy + chem + maths;

int totalphy_maths = phy + maths;

if ((phy >= 55 && chem >= 50 && maths >= 65)

&& (total >= 190 || totalphy_maths >= 140))

System.out.println("Eligible");

else

System.out.println("Not Eligible");

}
9. Write a Java program to read roll no, name and marks of three subjects and calculate the
total, percentage and division.

Ans:

import java.util.*;

public class CalculateMarks

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter roll no of student: ");

int rollNo = sc.nextInt();

System.out.print("Enter marks in 1st subject: ");

int m1 = sc.nextInt();

System.out.print("Enter marks in 2nd subject: ");

int m2 = sc.nextInt();

System.out.print("Enter marks in 3rd subject: ");

int m3 = sc.nextInt();

int total = m1 + m2 + m3;

double percent = total / 500.0 * 100.0;

String div = "";

if (percent >= 70)

div = "First";

else if (percent <70 && percent>=40 )

div = "Second";

else

div = "Fail";

System.out.println("Total Marks = " + total);

System.out.println("Percentage = " + percent);

System.out.println("Division = " + div);

}
}

10. Write a Java program to read temperature in centigrade and display a suitable message
according to temperature state below : Temp < 0 then Freezing weather Temp 0-10 then
Very Cold weather Temp 10-20 then Cold weather Temp 20-30 then Normal in Temp Temp
30-40 then Its Hot Temp >=40 then Its Very Hot

Ans:

import java.util.*;

class TemperatureC

public static void main(String[] args){

Print("Enter temperature in celcius: \n");

Scanner sc = new Scanner(System.in);

int temp = sc.nextInt();

sc.nextLine();

if(temp < 0)
System.out.println("Freezing Weather");

else if(temp >= 0 && temp < 10)

System.out.println("Very Cold");

else if(temp >= 10 && temp < 20)

System.out.println("Cold");

else if(temp >= 20 && temp<30)

System.out.println("Normal Weather");

else if(temp>=30 && temp<40)

System.out.println("Hot Temperature");

else if(temp>=40)

System.out.println("Very Hot");

}
11. Write a Java program to check whether a triangle is Equilateral, Isosceles or Scalene.

import java.util.*;

public class TriangleType

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

int a=sc.nextInt();

int b=sc.nextInt();

int c=sc.nextInt();

if(a==b && b==c)

System.out.println("equilateral triangle");

else if(a==b || b==c || c==a)

System.out.println("Isosceles triangle");

else if((b*b)+(c*c)==(a*a) || (a*a)+(b*b)==(c*c) || (a*a)+(c*c)==(b*b))

System.out.println("Right angled triangle");

else

System.out.println("Scaler triangle");

}
12. Write a Java program to check whether a character is an alphabet, digit or special character.

Ans:

import java.util.*;

public class Check

public static void main(String[] args)

char ch;

Scanner p=new Scanner(System.in);

ch=p.next().charAt(0);

if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

System.out.println(ch+" is Alphabit");

else if(ch>='0'&&ch<='9')

System.out.println(ch+" is Digit");

else
{

System.out.println(ch+" is special Symbol");

13. Write a Python program to check whether a triangle can be formed by the given value for
the angles.
Ans:

import java.util.*;
class Angle
{
public static void main(String args[])
{

Scanner sc=new Scanner(System.in);


int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();

if (a + b + c == 180 && a != 0 && b != 0 && c != 0)


System.out.println("Valid");
else
System.out.println("Invalid");

}
}
14. Write a Python program to check whether an alphabet is a vowel or consonant.

Ans:

import java.util.*;

public class Vowel {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

char ch;

ch=sc.next().charAt(0);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )

System.out.println(ch + " is vowel");

else

System.out.println(ch + " is consonant");

}
15. Write a Java program to calculate profit and loss on a transaction.

Ans:

import java.util.*;

class ProfitandLoss

public static void main(String args[])

Scanner s= new Scanner(System.in);

System.out.println("Enter the Costprice ");

double cp=s.nextDouble();

System.out.println("Enter the Selling price:");

double sp=s.nextDouble();

if(cp-sp>0)

System.out.println("loss:" +(cp-sp));

else if(cp-sp<0)

System.out.println("profit:" +(sp-cp));

else

System.out.println("NEUTRAL");
}

16. Write a program in Python to accept a grade and declare the equivalent description : Grade
Description E Excellent V Very Good G Good A Average F Fail
Ans:
import java.util.*;
public class Grade
{
public static void main(String[] args)
{
char a;
Scanner sc = new Scanner(System.in);
System.out.print("Enter grade of the student:");
a = sc.next().charAt(0);
if(a == 'E' || a == 'e')
{
System.out.println("Excelent");
}
else if(a == 'V' || a == 'v')
{
System.out.println("Very Good");
}
else if(a == 'G' || a == 'g')
{
System.out.println("Good");
}
else if(a == 'E' || a == 'e')
{
System.out.println("Average");
}
else if(a == 'F' || a == 'f')
{
System.out.println("Average");
}
}
}

17. Write a program in Java to calculate and print the Electricity bill of a given customer. The
customer id., name and unit consumed by the user should be taken from the keyboard and
display the total amount to pay to the customer. The charge are as follow : Unit Charge/unit
upto 199 @1.20 . 200 and above but less than 400 @1.50 400 and above but less than 600
@1.80 600 and above @2.00 If bill exceeds Rs. 400 then a surcharge of 15% will be charged
and the minimum bill should be of Rs. 100/-

Ans:

import java.util.*;

public class ElectricityBill

public static String n;

public static int units;

public static double bill;

void calculate()

if(units<=199)

bill = 1.20*units;

else if(units>=200 && units<400)

bill = 1.20*100 + (units-100)*1.50;

else if(units>=400 && units<600)

bill = 1.20*100 + 1.50*200 + (units-400)*1.80;

else if(units>=600)

bill = 1.20*100 + 1.50*200 + (units-400)*1.80+(units-600)*2.00;

}
if(bill>400)

bill = bill + 2.5*bill/100;

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the name of the customer");

n = sc.nextLine();

System.out.println("Enter the number of units consumed");

units = sc.nextInt();

ElectricityBill obj = new ElectricityBill();

obj.calculate();

System.out.println("Name of the customer : " + n);

System.out.println("Number of units consumed : " + units);

System.out.println("Bill Amount: " + bill);

}
18. Write a program in java to read any day number in integer and display day name in the
word.

Ans:

import java.util.*;

public class Weekday

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.println("Enter weekday number : ");

int weekday = scanner.nextInt();

if(weekday == 1)

System.out.println("Monday");

else if(weekday == 2)

System.out.println("Tuesday");

else if(weekday == 3)

System.out.println("Wednesday");

else if(weekday == 4)

System.out.println("Thursday");

else if(weekday == 5)

System.out.println("Friday");
}

else if(weekday == 6)

System.out.println("Saturday");

else if(weekday == 7)

System.out.println("Sunday");

else

System.out.println("Please enter weekday number between 1-7.");

19. Write a program in java to read any Month Number in integer and display Month name in
the word.

Ans:

import java.util.*;

public class Month

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);


System.out.println("Enter month number : ");

int month = scanner.nextInt();

if(month == 1)

System.out.println("January");

else if(month == 2)

System.out.println("February");

else if(month == 3)

System.out.println("March");

else if(month == 4)

System.out.println("April");

else if(month == 5)

System.out.println("May");

else if(month == 6)

System.out.println("June");

else if(month == 7)

System.out.println("July");

else if(month == 8)
{

System.out.println("August");

else if(month == 9)

System.out.println("September");

else if(month == 10)

System.out.println("October");

else if(month == 11)

System.out.println("November");

else if(month == 12)

System.out.println("December");

else

System.out.println("Invalid month");

}
20. Write a program in Java to read any Month Number in integer and display the number of
days for this month.

Ans:

import java.util.*;

public class MonthDay

public static void main(String[] strings) {

Scanner sc = new Scanner(System.in);

int number_Of_Days = 0;

String Month = "";

System.out.print("Input a month: ");

int month = sc.nextInt();

System.out.print("Input a year: ");

int year = sc.nextInt();

switch (month) {

case 1:

Month = "January";

number_Of_Days= 31;

break;

case 2:

Month = "February";

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {

number_Of_Days = 29;

} else {

number_Of_Days = 28;

break;

case 3:

Month = "March";

Number_Of_Days = 31;

break;
case 4:

Month= "April";

number_Of_Days = 30;

break;

case 5:

Month = "May";

number_Of_Days = 31;

break;

case 6:

Month = "June";

number_Of_Days = 30;

break;

case 7:

Month = "July";

number_Of_Days = 31;

break;

case 8:

Month = "August";

number_Of_Days= 31;

break;

case 9:

Month = "September";

number_Of_Days = 30;

break;

case 10:

Month = "October";

number_Of_Days = 31;

break;

case 11:

Month = "November";

number_Of_Days = 30;
break;

case 12:

Month = "December";

number_Of_Days= 31;

System.out.print( number_Of_Days + " days\n");

You might also like