PF 2
PF 2
Assignment No II
ASSIGNMENT # 02
public static void main (String args)
int count;
\\ integer declaration
String title;
\\ string declaration
boolean isAsleep;
NAME: TALAL IQBAL
\\ Boolean declaration
REGISTERATION
int myAge,NO: FA21-BCT-017
PROGRAM:mySize,
CYBER SECURITY
numShoes = 28;
\\ integer declaration/initialization
SUBJECT: PROGRAMMING
String myName = “Laura”;
FUNDAMENTALS
SUBMITTED TO:initialization
\\ string Ms. SADIA KHAN
boolean isTired = true;
DATE OF SUBMISSION: 3/8/2021
\\ Boolean initialization
int a = 4, b = 5, c = 6;
\\ integer declaration/initialization
}
2. Write the range of following variables:
Data Description
Type
\ddd:
\xdd:
\udddd:
a. class ArithmeticTest
short x = 6;
int y = 4;
float a = 12.5f;
float b = 7f;
System.out.println(“x is “ + x + “, y is “ + y);
System.out.println(“x + y = “ + (x + y));
System.out.println(“x - y = “ + (x - y));
System.out.println(“x / y = “ + (x / y));
System.out.println(“x % y = “ + (x % y));
System.out.println(“a is “ + a + “, b is “ + b;
OUTPUT:
x is 6 y is 4
x+y=10
x-y=2
x/y=1
x%y=2
a is 12.5 b is 7.0
a/b=1.7857143
b.
OUTPUT:
x and y are 0 and 0
x++ results in 1
x++ results in 2
Resetting x back to 0.
_ _ _ _ _ _
y = x++ (postfix) results in:
x is 1
y is 0
_ _ _ _ _ _
y = ++x (prefix) results in:
x is 2
y is 2
class Qs{
Public static void main(String[] args){
Int x ,y;
x=5+5;
x = 5-5;
x =5*5;
x = 5/5;
x = 5%5;
if (1<0){
System.out.print(“HELLO”);
}
if (1<0){
System.out.print(“HELLO”);
}
if (1<=0){
System.out.print(“HELLO”);
}
if (1>=0){
System.out.print(“HELLO”);
}
if (1==0){
System.out.print(“HELLO”);
}
if (1!=0){
System.out.print(“HELLO”);
}
if (1<0 && 1>3){
System.out.print(“HELLO”);
}
if (1<0 || 1>3){
System.out.print(“HELLO”);
}
x = x & 3;
x= x | 3;
y = !(1>5);
System.out.println(10<<2);//10*2^2
System.out.println(10>>2);//10/2^2 same as 10>>>2
x++;
y--;
x+=1;
y-=1;
x%=6;
y*=7;
y>>=3;
x<<=2;
x>>>3;
}
}
class gg {
int num,years,gender;
gender = input.nextInt();
num = input.nextInt();
System.out.print("Enter your years of service: ");
years = input.nextInt();
if(gender==2){
if (num==1){
if(years>=10){
else {
else if (years>=10) {
else {
System.out.print("Your salary is 10000.");
else if (num==1) {
if(years>=10){
else{
else if (years>=10) {
else{
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
System.out.printf ( "First division ") ;
else
{
if ( per >= 50 )
System.out.printf ( "Second division" ) ;
else
{
if ( per >= 40 )
System.out.printf ( "Third division" ) ;
else
System.out.printf ( "Fail" ) ;
}
}
}
OUTPUT:
Enter marks in five subjects
50
50
50
50
50
50
Second division
import java.util.Scanner;
class main{
public static void main(String[] args )
{
int status,age,gender;
Scanner input = new Scanner(System.in);
System.out.print("Press 1 if you are Female, 2 if you are Male: ");
gender = input.nextInt();
System.out.print("Press 1 if you're married, 2 if you're single: ");
status = input.nextInt();
System.out.print("Enter your age: ");
age = input.nextInt();
if(status==1){
System.out.print("You will be given insurance.");
}
else if(status==2){
if(gender==2){
if(age>30){
System.out.print("You will be given insurance.");
}
else{
System.out.print("No insurance.");
}
}
else if(gender==1){
if(age>25){
System.out.print("You will be given insurance.");
}
else{
System.out.print("No insurance.");
}
}
}
}
9. Write the output of following:
char a ;
int y ;
Char a= input.nextChar();
int x, y ;
Scanner input= new Scanner(System.in);
Int a= input.nextInt();
y=(x>5?3:4);
10.
import java.util.Scanner;
class gg{
{ int ch;
Scanner input = new Scanner(System.in);
System.out.print("Enter ASCII value of a Character = ");
ch = input.nextInt();
if (ch >= 65 && ch <= 92)
System.out.print("Capital letter");
else if (ch >= 97 && ch <= 122)
System.out.print("Small letter");
else if (ch >= 48 && ch <= 57)
System.out.print("Digit");
else
System.out.print("Special character");
}
}
11. A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for
6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after
30 days your membership will be cancelled. Write a program to accept the number of days
the member is late to return the book and display the fine or the appropriate message.
import java.util.Scanner;
class main{
public static void main(String[] args )
{
int days;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of days you are late: ");
days = input.nextInt();
if(days<=5){
System.out.print("Your fine is 50 paisa.");
}
else if(days<=10){
System.out.print("Your fine is 1 rupee.");
}
else if(days>10 && days<=30){
System.out.print("Your fine is 5 rupee.");
}
else if(days>30){
System.out.print("Your membership is cancelled.");
}
}
}
12. A university has the following rules for a student to qualify for a degree with A as the main
subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he
should get at least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to
reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed. Write a program to receive marks in A
and B and Output whether the student has passed, failed or is allowed to reappear in B.
import java.util.Scanner;
class main{
public static void main(String[] args )
{
int A,B;
Scanner input = new Scanner(System.in);
System.out.print("Enter your percentage in A :");
A = input.nextInt();
System.out.print("Enter your percentage in B :");
B = input.nextInt();
if(A>=55 && B>=45){
System.out.print("You have passed.");
}
else if(A<55 && A>=45 && B>=55){
System.out.print("Your have passed.");
}
else if(B<45 && A>=65){
System.out.print("You are allowed to reappear in examination B.");
}
else{
System.out.print("You have failed.");
}
}
}
Int x= input.nextInt();
if ( x > max )
max = x ;
else
min = x ;
}
main( ) {
int x, min, max;
Scanner input= new Scanner(System.in);
x= input.nextInt();
max= input.nextInt();
max = (x > max)? x : 0 ;
min = (x < max)? x : 0 ;}
2. {
float sal ;
System.out.printf ("Enter the salary" ) ;
Scanner input= new Scanner(System.in);
float sal ;
System.out.printf ("Enter the salary" ) ;
Scanner input= new Scanner(System.in);
sal= input.nextFloat();
String result = ( sal < 40000 && sal > 25000 )? "Manager" : ( sal < 25000 && sal >
15000 ) ? "Accountant" : "Clerk";
System.out.print(result);
14. Write a program using conditional operators to determine whether a year entered through
the keyboard is a leap year or not.
import java.util.Scanner;
class gg{
int year;
year = input.nextInt();
String result = (year%100==0)? (year%400==0)? "This is a leap year." :"This is not a leap
year." : (year%4==0)? "This is a leap year." :"This is not a leap year.";
System.out.print(result);
}
}
15. Write a program to find the greatest of the three numbers entered through the keyboard
using conditional operators.
import java.util.Scanner;
class gg {
int no1,no2,no3;
Scanner input = new Scanner(System.in);
System.out.println("Enter first number.");
no1 = input.nextInt();
System.out.println("Enter second number.");
no2 = input.nextInt();
System.out.print("Enter third number.");
no3 = input.nextInt();
String greatest = (no1>no2 && no1>no3)? "Greatest number is 1st number" :
(no2>no1 && no2>no3)? "Greatest number is 2nd number " : (no3>no1 && no3>no2)? "Greatest
number is 3rd number ": " ";
System.out.print(greatest);
}
}