{IF STATEMENTS}
--------------
1.28 Write a simple program in Java to prove the execution of if Statement
-------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int a;
System.out.println("Enter a number to check");
a=input.nextInt();
if(a>10)
{
System.out.println("The number greater than 10 is:"+a);
}
if(a<10)
{
System.out.println("The number smaller than 10 is:"+a);
}
if(a==10)
{
System.out.println("The number equals to 10 is:"+a);
}
}
}
Output:
--------
Enter a number to check
5
The number smaller than 10 is:5
------------------------------------------------------------------------
1.29 Write a program in Java to take two numbers as input and Compare them using if Statement
------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int a,b;
System.out.println("Enter the first number");
a=input.nextInt();
System.out.println("Enter the second number");
b=input.nextInt();
if(a>b)
{
System.out.println(a+" is greater than"+b);
}
if(a<b)
{
System.out.println(a+" is smaller than "+b);
}
if(a==b)
{
System.out.println(a+" is equals to "+b);
}
}
}
Output:
--------
Enter the first number
30
Enter the second number
60
30 is smaller than 60
------------------------------------------------------------------------
1.30 Write a program to check the greatestest amongst all inputs
------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int a,b,c,x=-32767;
System.out.println("Enter the first number");
a=input.nextInt();
System.out.println("Enter the second number");
b=input.nextInt();
System.out.println("Enter the third number");
c=input.nextInt();
if(a>x)
{
x=a;
}
if(b>x)
{
x=b;
}
if(c>x)
{
x=c;
}
System.out.println("The greatest amongst three is :"+x);
}
}
Output:
--------
Enter the first number
8
Enter the second number
9
Enter the third number
7
The greatest amongst three is :9
------------------------------------------------------------------------
1.31 Write a program to check which is the lowest amongst all inputs
----------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int a,b,c,x=32767;
System.out.println("Enter the first number");
a=input.nextInt();
System.out.println("Enter the second number");
b=input.nextInt();
System.out.println("Enter the third number");
c=input.nextInt();
if(a<x)
{
x=a;
}
if(b<x)
{
x=b;
}
if(c<x)
{
x=c;
}
System.out.println("The smallest amongst three is :"+x);
}
}
Output:
--------
Enter the first number
6
Enter the second number
8
Enter the third number
7
The smallest amongst three is :6
------------------------------------------------------------------------
1.32 Write a program to check the password using if only.
----------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p;
System.out.println("Enter your password\t ");
p=input.nextInt();
if(p==12345)
{
System.out.println("Password accepted");
}
if(p!=12345)
{
System.out.println("Pass Incorrect");
}
}
}
Output:
--------
Enter your password
12345
Password accepted
------------------------------------------------------------------------
1.33 Write a program to check the password using if only and it will add two numbers.
---------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p,a,b,c;
System.out.println("Enter your password\t ");
p=input.nextInt();
if(p==12345)
{
System.out.println("Password accepted");
System.out.println("\nEnter Two numbers");
a=input.nextInt();
b=input.nextInt();
c=a+b;
System.out.println("The Result is :"+c);
}
if(p!=12345)
{
System.out.println("Pass Incorrect");
}
}
}
Output:
--------
Enter Two numbers
1
2
The Result is :3
------------------------------------------------------------------------
ELSE
1.34 (Write a program to check the password with if – else and if it is matched then add two numbers.)
-----------------------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p,a,b,c;
System.out.println("Enter your password\t ");
p=input.nextInt();
if(p==12345)
{
System.out.println("Password accepted");
System.out.println("\nEnter Two numbers");
a=input.nextInt();
b=input.nextInt();
c=a+b;
System.out.println("The Result is :"+c);
}
else
{
System.out.println("Pass Incorrect");
}
}
}
Output:
--------
Enter your password
987888
Pass Incorrect
------------------------------------------------------------------------
1.35 ( Write a program that can check your voter ability.)
----------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p;
System.out.println("Enter your age\t ");
p=input.nextInt();
if(p>=18)
{
System.out.println("You are eligible to vote");
}
else
{
System.out.println("Not eligible");
}
}
}
Output:
--------
Enter your age
45
You are eligible to vote
----------------------------------------------------------------------------------------
Else If
1.36 Write a program to find the grade using else if().
----------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p;
System.out.println("Enter your average marks\t ");
p=input.nextInt();
if(p>=60)
{
System.out.println("Congrats 1St Division");
}
else if(p>=45)
{
System.out.println("Not bad 2nd Divsion");
}
else if(p>=30)
{
System.out.println("Pass");
}
else
{
System.out.println("very Bad! you have Failed");
}
}
}
Output:
--------
Enter your average marks
40
Pass
--------------------------------------------------------------------------------------------------------------
Conditional Operator(? :)
1.37 Check password authenticity using conditonal operator.
-------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args) throws Exception
{
Scanner input =new Scanner(System.in);
int p,g;
System.out.println("Enter your password\t ");
p=input.nextInt();
g=(p==123) ? 1:0;
if (g==1)
System.out.println("Pass accepted");
else
System.out.println("denied");
}
}
Output:
--------
Enter your password
123
Pass accepted
----------------------------------------------------------------
Nested if :
1.38 Write a program using nested if to check nationality, if Indian ask to enter age and check voter ability.
---------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int ch;
int age;
System.out.println("Enter 1 if you are Indian or 2 for others ");
ch=input.nextInt();
if(ch==1)
{
System.out.println("Enter your age");
age=input.nextInt();
if(age>=18)
{
System.out.println("You can vote");
}
else
{
System.out.println("You can't vote");
}
}
else
{
System.out.println("You can't access you are not an Indian");
}
}
}
Output:
--------
Enter 1 if you are Indian or 2 for others
1
Enter your age
45
You can vote
------------------------------------------------------------------------
1.39 Write a program using nested if to check nationality, if Indian ask to enter age alongside matching lingustic ability(criteria:Bengali) as well as check voter ability.
---------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int ch,h;
int age;
System.out.println("Enter 1 if you are Indian or 2 for others ");
ch=input.nextInt();
System.out.println("Do you know bengali if yes press 1 other wise 0");
h=input.nextInt();
if(ch==1&&h==1)
{
System.out.println("Enter your age to check voter ability");
age=input.nextInt();
if(age>=18&&h==1)
{
System.out.println("You can vote");
System.out.println("You are Welcome");
}
else
{
System.out.println("You can't vote");
System.out.println("Sorry we Cant allow");
}
}
else
{
System.out.println("You can't access you are not an Indian");
}
}
}
Output:
--------
Enter 1 if you are Indian or 2 for others
1
Do you know bengali if yes press 1 other wise 0
1
Enter your age to check voter ability
15
You can't vote
Sorry we Cant allow
------------------------------------------------------------------------
Logical Operators
1.40 (Write a program to cast vote for certain age using logical operators)
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int age;
System.out.println("Enter your age");
age=input.nextInt();
if((age>=18)&&(age<=60))
{
System.out.println("You can vote");
}
else{
System.out.println("You can't vote");
}
}
}
Output:
--------
Enter your age
18
You can vote
----------------------------------------------------------
1.41 Write a program to display the concession message from railway if he or she falls under the criteria (3-11 years and 60 to 100 years of age).
-----------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int age;
System.out.println("Enter your age");
age=input.nextInt();
if (((age>=3) && (age<=11)) || ((age>=60) && (age<=100)))
{
System.out.println("You can get the concession ");
}
else
{
System.out.println("You can not get the concession ");
}
}
}
Output:
--------
Enter your age
61
You can get the concession
--------------------------------------------
More problems on if else............
1.42 Write a program to check a triangle is perfect or not.
----------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int a,b,c;
System.out.println("Enter the three angles of a Triangle");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
if(a+b+c==180)
{
System.out.println("The Triangle is perfect");
}
else
{
System.out.println("The Triangle is not perfect");
}
}
}
Output:
--------
Enter the three angles of a Triangle
60
60
60
The Triangle is perfect
------------------------------------------------------------------------
1.43 Write a program to check a rectangle whether it is perfect rectangle or not?
---------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int a,b,c,d;
System.out.println("Enter the three angles of a Rectangle");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
d=input.nextInt();
if(a+b+c+d==360)
{
System.out.println("The Rectangle is perfect");
}
else
{
System.out.println("The Rectangle is not perfect");
}
}
}
Output:
--------
Enter the three angles of a Rectangle
90
90
90
90
The Rectangle is perfect
-------------------------------------------------------------------------------------------------
1.43 Write a program to check is there any supply needs as for the respect of demand.
-------------------------------------------------------------------------------------------------
CODE:-
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int demand, supply,need;
System.out.println("Enter the Demand");
demand=input.nextInt();
System.out.println("Enter the Supply");
supply=input.nextInt();
if (demand>=supply)
{
need=demand-supply;
System.out.println("We Need to Purchase:"+need);
}
else
{
System.out.println("We have no need to purchase");
}
}
}
Output:
--------
Enter the Demand
300
Enter the Supply
200
We Need to Purchase:100
------------------------------------------------------------------------
1.44 Write a program to check a year whether it is leap year or not.
---------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
System.out.println("Enter a year to check");
int year;
year=input.nextInt();
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4, not by 100, or divisible by 400
isLeapYear = isLeapYear && (year % 100 != 0 || year % 400 == 0);
if (isLeapYear)
{
System.out.println(year + " is a leap year.");
}
else
{
System.out.println(year + " is not a leap year.");
}
}
}
Output:
--------
Enter a year to check
2020
2020 is a leap year.
------------------------------------------------------------------------
1.45 Write a program to determine a number is odd or even.
------------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int a;
System.out.println("Enter a number to check");
a=input.nextInt();
if (a%2==1)
{
System.out.println("The Number is Odd");
}
else
{
System.out.println(" The Number is even");
}
}
}
Output:
--------
Enter a number to check
5
The Number is Odd
------------------------------------------------------------------------
1.46 Create a program to make a result sheet with division.
----------------------------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
String name;
int eng=0,ben=0,math=0,geo=0,hist=0,phy=0,lsc=0;
float total= 0,avg=0;
System.out.println("Enter your Name");
name=input.next();
System.out.println("Enter the marks for English");
eng=input.nextInt();
System.out.println("Enter the marks for Bengali");
ben=input.nextInt();
System.out.println( " Enter the marks for Math");
math=input.nextInt();
System.out.println("Enter the marks for Geography");
geo=input.nextInt();
System.out.println("Enter the marks for History");
hist=input.nextInt();
System.out.println(" Enter the marks for Physical Science ");
phy=input.nextInt();
System.out.println("Enter the marks for Life Science");
lsc=input.nextInt();
total=eng+ben+math+geo+hist+phy+lsc;
avg=total/7;
System.out.println("Total Marks ="+total);
System.out.println("Average ="+avg);
if (avg>=60)
System.out.println("1st Division");
else
if( avg>=45)
System.out.println(" 2nd Division");
else
if (avg>=30)
System.out.println( " P Division");
else
System.out.println("Fail");
}
}
Output:
--------
Enter your Name
Souradeep
Enter the marks for English
85
Enter the marks for Bengali
75
Enter the marks for Math
70
Enter the marks for Geography
82
Enter the marks for History
86
Enter the marks for Physical Science
75
Enter the marks for Life Science
78
Total Marks =551.0
Average =78.71429
1st Division
------------------------------------------------------------------------
1.47 A certain grade of steel is graded according to the following conditions:
1) Hardness must be greater than 50.
2) Carbon content must be less than 0.7
3) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three condition are met
Grade is 9 if conditions(1) and (2) are met.
Grade is 8 if conditisons(2) and (3) are met.
Grade is 7 if conditisons(1) and (3) are met.
Grade is 6 if only one conditison is met.
Grade is 5 if none of the conditions are met.
-----------------------------------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
float carbon;
int hard,tensile;
System.out.println("enter the hardness of steel");
hard=input.nextInt();
System.out.println("enter the carbon of steel");
carbon=input.nextFloat();
System.out.println("enter the tensile");
tensile=input.nextInt();
if(((hard>=50)&&(carbon<=0.7)&&(tensile>=5600)))
{
System.out.println("Grade is 10");
System.out.println("\npress any key");
}
if(((hard>=50)&&(carbon<=0.7)&&(tensile<=5600)))
{
System.out.println("grade is 9");
System.out.println("\n press any key");
}
if(((hard<=50)&&(carbon<=0.7)&&(tensile>=5600)))
{
System.out.println("Grade is 8");
System.out.println("\n press any key");
}
if(((hard>=50)&&(carbon>=0.7)&&(tensile>=5600)))
{
System.out.println("Grade is 7");
System.out.println("\n press any key");
}
if(((hard>=50)&&(carbon>=0.7)&&(tensile<=5600)))
{
System.out.println("Grade is 6");
System.out.println("\n press any key");
}
if(((hard==40)&&(carbon==0.5)&&(tensile==5600)))
{
System.out.println("Grade is 5");
System.out.println("\n press any key");
}
}
}
Output:
--------
enter the hardness of steel
50
enter the carbon of steel
60
enter the tensile
80
Grade is 6
press any key
------------------------------------------------------------------------
1.48 WAP to add two distance
-------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int f1,f2,i1,i2,f3,i3;
System.out.println("Enter 1st distance=");
f1=input.nextInt();
i1=input.nextInt();
System.out.println("Enter 2nd distance=");
f2=input.nextInt();
i2=input.nextInt();
f3=f1+f2;
i3=i1+i2;
if(i3>=12)
{
i3=i3%12;
f3=f3+(i3/12);
}
System.out.println("\nFeet:"+f3);
System.out.println("\nInch:"+i3);
}
}
Output:
--------
Enter 1st distance=
10
20
Enter 2nd distance=
30
10
Feet40
Inch6
-------------------------------------------------------------------------------------------
1.49 Write a program to determine whether discount of 10% will be allowed if purchase price will exceed Rs 5000.
------------------------------------------------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
String pn;
float rate,qty,disp,pr;
System.out.println("Enter your product name");
pn=input.next();
System.out.println("Enter your rate");
rate=input.nextFloat();
System.out.println("Enter the Quantity");
qty=input.nextFloat();
pr=rate*qty;
if (pr>=5000)
{
disp=(pr-pr*10/100);
System.out.println("Price after 10% discount = Rs"+disp);
}
else
{
System.out.println("No discount allowed as price= Rs. "+pr);
}
}
}
Output:
--------
Enter your product name
vivo earphones
Enter your rate
1000
Enter the Quantity
10
Price after 10% discount = Rs9000.0
------------------------------------------------------------------------
1.50 Write a program to levied Re1 for each day if submit date exceeds more than 10th of the current month.
--------------------------------------------------------------------------------------------------------------
Code:
------
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Mavenproject1
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
String pn;
int date,sub,fine,total;
System.out.println("Enter your name");
pn=input.next();
System.out.println("Enter Subscription Amount");
sub=input.nextInt();
System.out.println("Enter the Date(Day only)");
date=input.nextInt();
if (date>=10)
{
fine=date-10;
total=fine+sub;
System.out.println("Fine Rs"+fine );
System.out.println("\nTotal Amount"+total);
}
else
{
System.out.println("No fine Please Submit the Subscription Rs "+sub);
}
}
}
Output:
--------
Enter your name
Souradeep
Enter Subscription Amount
1000
Enter the Date(Day only)
31
Fine Rs21
Total Amount1021
----------------------------------------------------------------------------------------------------------
At Subhasgram we provide Computer Training and Problem Solving. We always believe in deep learning policies. The more you learn more you can earn. Knowledge is the most powerful weapon in this world. If you can use it then the whole world is waiting for you. Be with us and feel the magic of deep learning. Technologica Computer Education Society (Govt.Regd).
Description
Thursday, 15 June 2023
Java Chapter 2
Subscribe to:
Post Comments (Atom)
JAVA PROGRAMMING - OOPS CHAPTER 1
Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...
-
Spoken English Level 4 A. COMPLEX EXPERIENCE SHARE: [All about your feelings] 1. When you experienced a quarrel. 2. When you expe...
-
Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...
No comments:
Post a Comment