0% found this document useful (0 votes)
10 views14 pages

Vijay

Uploaded by

prateekmeenaa10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Vijay

Uploaded by

prateekmeenaa10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

HOMEWORK 2

NAME-VIJAY DHINGRA
BRANCH-COMPUTER SCIENCE
ROLL NO-2400270120175
1.check if a number is positive.
import java.util.Scanner;

public class Positive {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num >= 0) {
System.out.println("number is positive");
}

}
}
Output:
enter number
20
number is positive
2.check if person is eligible to vote.
import java.util.Scanner;

public class eligible {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int age;
System.out.println("enter age");
age = input.nextInt();
if (age >= 18) {
System.out.println("person is eligible for voting");
}
}
}
Output:
enter age
22
person is eligible for voting
3.check if a number is divisible by 5.

import java.util.Scanner;

public class divide {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num % 5 == 0) {
System.out.println("divisible by 5");
}
}
}
Output:
enter number
25
divisible by 5
4.check if a character is uppercase letter.
import java.util.Scanner;

public class Upper {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
char ch;
System.out.println("enter character");
ch = input.next().charAt(0);
if (ch >= 65 && ch <= 90) {
System.out.println("uppercase");
}
}
}
Output:
enter character
D
Uppercase
5.check if a number is even.
import java.util.Scanner;

public class Even {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num % 2 == 0) {
System.out.println("number is even");
}
}
}
Output:
enter number
4
number is even
6. check whether a number is even or odd.
import java.util.Scanner;

public class Even {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num % 2 == 0) {
System.out.println("number is even");
} else {
System.out.println("number is odd");
}
}
}
Output:
enter number
5
number is odd
7.find the greater of two numbers.
import java.util.Scanner;

public class great {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int a;
int b;
System.out.println("enter first number a");
a = input.nextInt();
System.out.println("enter first number b");
b = input.nextInt();
if (a > b) {
System.out.println("a is greater");
} else {
System.out.println("b is greater");
}

}
}
Output:
enter first number a
20
enter first number b
40
b is greater
8.compare and print the greater number.
import java.util.Scanner;

public class Great1 {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int a;
int b;
System.out.println("enter first number a");
a = input.nextInt();
System.out.println("enter first number b");
b = input.nextInt();
if (a > b) {
System.out.println(+a);
} else {
System.out.println(+b);
}

}
}
Output:
enter first number a
20
enter first number b
30
30
9.check whether a number is positive or negative.
import java.util.Scanner;
public class Positive1 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num >= 0) {
System.out.println("number is positive");
} else {
System.out.println("number is negative");
}
}
}
Output:
enter number
-20
number is negative
10.check whether a person is minor or adult.
import java.util.Scanner;

public class Minor {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int age;
System.out.println("enter age");
age = input.nextInt();
if (age < 18) {
System.out.println("person is minor");
} else {
System.out.println("person is adult");
}
}
}
Output:
enter age
20
person is adult
11.check if a year is leap year or not.
import java.util.Scanner;

public class Leap {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int year;
System.out.println("enter year");
year=input.nextInt();
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
System.out.println("leap year");
} else {
System.out.println("not a leap year");
}
}
}
Output:
enter year
2024
leap year
12.Assign grade based on marks.
import java.util.Scanner;

public class Grade {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int marks;
System.out.println("enter marks(0-100)");
marks = input.nextInt();
if (marks >= 90) {
System.out.println("A GRADE");
} else if (marks >= 80) {
System.out.println("B GRADE");
} else if (marks >= 70) {
System.out.println("C GRADE");
} else if (marks >= 60) {
System.out.println("D GRADE");
} else {
System.out.println("FAIL");
}
}
}
Output:
enter marks(0-100)
95
A GRADE
13.check if a number is positive,zero,negative.
import java.util.Scanner;

public class Zero {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number");
num = input.nextInt();
if (num > 0) {
System.out.println("number is positive");
} else if (num < 0){
System.out.println("number is negative");
} else {
System.out.println("number is zero");
}
}
}
Output:
enter number
0
number is zero
14.print the day of the week based on number(1 to 7).
import java.util.Scanner;

public class Day1 {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number(1-7)");
num = input.nextInt();
if (num == 1) {
System.out.println("MONDAY");
} else if (num == 2) {
System.out.println("TUESDAY");
} else if (num == 3) {
System.out.println("WEDNESDAY");
} else if (num == 4) {
System.out.println("THURSDAY");
} else if (num == 5) {
System.out.println("FRIDAY");
} else if (num == 6) {
System.out.println("SATURDAY");
} else if (num == 7) {
System.out.println("SUNDAY");
}
}
}
Output:
enter number(1-7)
5
FRIDAY
15.classify age group.
import java.util.Scanner;

public class Agegrp {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int age;
System.out.println("enter age(>0)");
age = input.nextInt();
if (age > 0 && age <= 12) {
System.out.println("CHILD");
} else if (age >= 13 && age <= 19) {
System.out.println("TEEN");
} else if (age >= 20 && age <= 59) {
System.out.println("ADULT");
} else {
System.out.println("SENIOR");
}

}
}
Output:
enter age(>0)
40
ADULT
16.temprature checker.
import java.util.Scanner;

public class Temperature {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int temp;
System.out.println("enter temperatue");
temp = input.nextInt();
if (temp < 0) {
System.out.println("FREEZING");
} else if (temp >= 0 && temp <= 10) {
System.out.println("COLD");
} else if (temp >= 11 && temp <= 20) {
System.out.println("COOL");
} else if (temp >= 21 && temp < 30) {
System.out.println("WARM");
} else {
System.out.println("HOT");
}
}
}
Output:
enter temperatue
20
COOL
17.simple calculator using switch case.
import java.util.Scanner;

public class Calculator {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num1;
System.out.println("enter first number ");
num1 = input.nextInt();
int num2;
System.out.println("enter second number ");
num2 = input.nextInt();
double result;
char ch;
System.out.println("enter operator(+,-,*,/)");
ch = input.next().charAt(0);
switch (ch) {
case '+':
result = num1 + num2;
System.out.println(+result);
break;
case '-':
result = num1 - num2;
System.out.println(+result);
break;
case '*':
result = num1 * num2;
System.out.println(+result);
break;
case '/':
result = num1 / num2;
System.out.println(+result);
break;

default:

System.out.println("invalid");
}
}
}
Output:
enter first number
20
enter second number
5
enter operator(+,-,*,/)
+
25.0
18.print the day name using switch(1 to 7).
import java.util.Scanner;

public class Day {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int day;
System.out.println("enter number(1-7)");
day = input.nextInt();
switch (day) {
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("thur5sday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("saturday");
break;
case 7:
System.out.println("sunday");
break;
default:

System.out.println("invalid");
}
}
}
Output:
enter number(1-7)
5
friday
19.check whether a character is a vowel or consonant
using switch.
import java.util.Scanner;

public class Letter {


public static void main(String args[]) {
Scanner input = new Scanner(System.in);
char ch;
System.out.println("enter character(a-z or A-Z)");
ch = input.next().charAt(0);
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println("VOWEL");
break;
default:
System.out.println("CONSONANT");

}
}
}

Output:
enter character(a-z or A-Z)
u
VOWEL
20.print month name from month number(1 to 12).
import java.util.Scanner;
public class Month {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number(1-12)");
num=input.nextInt();
switch (num) {
case 1:System.out.println("JANUARY");
break;
case 2:System.out.println("FEBRUARY");
break;
case 3:System.out.println("MARCH");
break;
case 4:System.out.println("APRIL");
break;
case 5:System.out.println("MAY");
break;
case 6:System.out.println("JUNE");
break;
case 7:System.out.println("JULY");
break;
case 8:System.out.println("AUGUST");
break;
case 9:System.out.println("SEPTEMBER");
break;
case 10:System.out.println("OCTOBER");
break;
case 11:System.out.println("NOVEMBER");
break;
case 12:
System.out.println("DECEMBER");
break;
default:
System.out.println("INVALID INPUT");

}
}
}
Output:
enter number(1-12)
5
MAY
21.perform action based on user role.
import java.util.Scanner;
public class Action {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("enter number(1-3)");
num = input.nextInt();
switch (num) {
case 1:
System.out.println("ADMIN");
break;
case 2:
System.out.println("TEACHER");
break;
case 3:
System.out.println("STUDENT");
break;
default:
System.out.println("GUEST");

}
}
}
Output:
enter number(1-3)
2
TEACHER

You might also like