IF-ELSE PROGRAM IN JAVA
- WAP to enter age and check if it greater than equal to 18 then print “Eligible to vote” else print “Not eligible to vote”.
import java.util.*;
class
Age
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int age;
System.out.println(“Enter the age :”);
age=sc.nextInt( );
if(age>=18)
System.out.println(“Eligible
to vote”);
else
System.out.println(“Not eligible to
vote”);
}
}
b.WAP
to check whether a number is odd or even.
import java.util.*;
class
EvenOdd
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println(“Enter the number :”);
num=sc.nextInt( );
if(num%2= =0)
System.out.println(“Number
is Even”);
else
System.out.println(“Number is Odd”);
}
}
- WAP
to check whether a number is Positive or Negative.
import java.util.*;
class PosNeg
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println(“Enter
the number :”);
num=sc.nextInt( );
if(num>0)
System.out.println(“Number
is Positive”);
else
System.out.println(“Number
is Negative”);
}
}
- WAP
to enter year and check whether it is leap year or not.
import java.util.*;
class LeapYear
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int year;
System.out.println(“Enter
the year :”);
year=sc.nextInt(
);
if(year%4= =0
&& year%100!=0 || year%400==0)
System.out.println(“It
is Leap Year”);
else
System.out.println(“It
is not a Leap Year”);
}
}
- WAP
to enter character and check whether it is a vowel or not.
import java.util.*;
class Vowel
{
public static void main()
{
Scanner sc=new Scanner(System.in);
char ch;
System.out.println(“Enter
the character”);
ch=sc.next().charAt(0);
if(ch==’a’||
ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’)
System.out.println(“It
is a vowel”);
else
System.out.println(“It
is not a vowel”);
}
}
- WAP
to calculate the commissutiln of a salesman which is 10% if the sale made
by the employee is above 10,000.
import java.util.*;
class Comm
{
public static void main()
{
Scanner sc=new
Scanner(System.in);
double
sales,comm;
System.out.println
(“Enter the sales :”);
sales=sc.nextDouble
();
if(sales
>=10000)
comm=(10/100)*sales;
System.out.println(“Commissutiln
is :”+comm);
}
}
- WAP
to calculate the commissutiln of a salesman which is 10% if the sale made
by the employee is above 10,000 and 5% if sales is below it.
import java.util.*;
class Comm
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
double sales,comm;
System.out.println(“Enter
the sales :”);
sales=Sc.nextDouble(
);
if(sales
>=10000)
comm= 0.10 * sales;
else
comm= 0.05*sales;
System.out.println(“Commissutiln
is : ”+comm);
}
}
- WAP
to find the grade of the students depending upon the marks given as below
:
Marks >= 90 Grade ‘A’
Marks >= 80
and Marks < 90 Grade ‘B’
Marks >= 70
and Marks < 80 Grade ‘C’
Marks >= 60
and Marks < 70 Grade ‘D’
Marks < 60 Grade ‘F’
import java.util.*;
class Grade
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int marks;
char grade;
System.out.println(“Enter
the marks :”);
marks=sc.nextInt(
);
if(marks >=90)
grade=’A’;
else if(marks
>=80 && marks <90)
grade=’B’;
else if(marks
>=70 && marks <80)
grade=’C’;
else if(marks
>=60 && marks <70)
grade=’D’;
else
grade=’F’;
System.out.println(“Grade is :”+ grade);
}
}
- WAP
to enter any two numbers and find the greater of the two.
import java.util.*;
class Greater
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println(“Enter number”);
a=sc.nextInt( );
System.out.println(“Enter number”);
b=sc.nextInt( );
if(a>b)
System.out.println(“a is greater”+a);
else
System.out.println(“b is greater”+b);
}
}
- WAP to enter any three numbers and find
the greatest of the three.
import java.util.*;
class Greatest
{
public static void main( )
{
Scanner
sc=new Scanner(System.in);
int
a,b,c;
System.out.println(“Enter number”);
a=sc.nextInt( );
System.out.println(“Enter number”);
b=sc.nextInt( );
System.out.println(“Enter number”);
c=sc.nextInt( );
if(a>b &&
a>c)
System.out.println(“a
is greater”+ a);
if(b>a && b>c)
System.out.println(“b
is greater”+ b);
if(c>a
&& c>b)
System.out.println(“c
is greater”+ c);
}
}
- A library charges a fine for books
returned late. Following are the fines.
First 5 days : 40 paise per day
6 to 10 days : 65 paise per day
Above 10 days :
80 paise per day
WAP
to calculate the fine assuming that a book is returned ‘n’ days late.
import java.util.*;
class fine
{
public static void main()
{
Scanner
br=new Scanner(System.in);
int
day;
double fine;
System.out.println("Enter number of
days: ");
day=sc.nextInt();
if(day>=1
&& day<=5)
fine=day * 0.40;
else
if(day>=6 && day<=10)
fine=(5 * 0.40)+((day-5) * 0.65 );
else
fine=(5 * 0.40) + (5 * 0.65) + ((day-10) *
0.80);
System.out.println("Fine for
"+day+" days is Rs."+fine);
}
}
- WAP to compute charges for sending
parcels where the charges are as follows:
For the first 1 kg Rs.15.00.For additutilnal
weight for every 500gm or fractutiln there of Rs. 8.00.
import java.util.*;
class Parcels
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int w,cost,b;
double d;
System.out.println("Enter
weight : ");
w=sc.nextInt(br.readLine());
if(w<=1000)
cost=15;
else
{
b=w-1000;
d=b/500;
if(d==(int)d)
cost=15+((int)d*8);
else
cost=15+(8*((int)d+1));
}
System.out.println("Cost is
Rs."+cost);
}
}
- WAP
to calculate the telephone bill amount as per instructutilns.
For 1st
100 calls – Rs. 100
For additutilnal 50
calls – Rs. 2 per call
For calls > 150 –
Rs. 2.50 per call
import java.util.*;
class Bill
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int call;
double bill;
System.out.println(“Enter number of calls :”);
call=sc.nextInt( );
if(call<=100)
bill=100;
else if(call>100
&& call<=150)
bill=100 + (call-100)*2;
else
bill=100 + (50*2) +
((call-150) * 2.5);
}
}
- WAP
to enter any character and check if it is a character, digit or a special
character.
import java.util.*;
class Check
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
char ch;
System.out.println(“Enter any character :”);
ch=sc.next( ).charAt(0);
int c=(int)ch;
if(c>=65 && ch<=91 || c>=97 &&
c<=122)
System.out.println(“It
is a character”);
else if(c>=48 && c<=56)
System.out.println(“It is a digit”);
else
System.out.println(“It is a special character”);
}
}
- WAP
to find the age in years, months and days by inputting the date of birth
and the current date.
import java.util.*;
class Check
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
Int a,b;
System.out.print(“Enter date of
birth”);
a=sc.nextInt( );
System.out.print(“Enter current date”);
b=sc.nextInt( );
if(cd>dd)
d=cd-dd;
else
{
cd=cd+30;
cm=cm-1;
d=cd-dd;
}
If(cm>dm)
m=cm-dm;
else
{
cm=cm+12;
cy=cy-1;
m=cm-dm;
}
System.out.print(y+”yrs”+m+”months”+d+”days”);
}
}
- WAP
to find the grade of the students depending upon the marks given as below
:
Marks >= 90 Grade ‘A’
Marks >= 80
and Marks < 90 Grade ‘B’
Marks >= 70
and Marks < 80 Grade ‘C’
Marks >= 60
and Marks < 70 Grade ‘D’
Marks < 60 Grade ‘F’
2. A no. of answer script with scores 0 to
99 awarded after marking are to be searched for finding no. of answer scripts
marked: [ Year 1998]
1)
Percentage of candidates getting 85 and above
2) Percentage of candidates getting 39 or less
import java.util.*;
class Grade
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int
n ;
String y;
double p1,p2;
System.out.print(“Enter no. of scripts”);
n=sc.nextInt( );
for(i=1;i<=n;i++)
{
System.out.print(“Script” + i + ”is marked”);
y=sc.next( );
if(y.equals(“y”);
{
c=c+1;
System.out.print(“Enter score”);
s=Sc.nextInt( );
if(s>85)
a++;
if(s<=39)
b++;
}
}
p1= (a/c) *100;
p2= (b/c) *100;
System.out.print(“No. of scripts
marked”+c);
System.out.print(“Percentage of
candidate getting 85 and above”+p1);
System.out.print(“Percentage of
candidate getting 39 or less”+p2);
}
}
- WAP
to input age and distance in km and print the corresponding fare.
Age Distance Fare
Below 10 below
10 5
10-50
10
Above 50 20
10-50 below 10 15
10-50 25
Above
50 35
Above
50 below 10 25
10-50
35
Above 50 45
import java.util.*;
class Grade
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int age,dis,fare;
System.out.println(“Enter age”);
age=Sc.nextInt( );
System.out.println(“Enter distance”);
dis=Sc.nextInt( );
if(age<10)
{
if(dis<10)
fare=5;
else if(dis>=10 &&
dis<=50)
fare=10;
else
fare=20;
}
else if(age>=10 && age<=50)
{
if(dis<10)
fare=15;
else if(dis>=10 && dis<=50)
fare=25;
else
fare=35;
}
else
{
if(dis<10)
fare=35;
else
if(dis>=10 && dis<50)
fare=35;
else
if(dis>50)
fare=45;
}
}
- A cloth showroom has announced the
following festival discounts on the purchase of items based on the total
cost of the items purchased. [Year 2006]
Total Cost
Discount
Less than Rs. 2000 5%
Rs. 2000 to Rs. 5000 25%
Rs. 5000 to Rs. 10,000 35%
Above 10,000 50%
import java.util.*;
class Discount
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
double cost,d;
if(cost<=2000)
d=(5/100)*cost;
else if(cost>2000
&& cost<=5000)
d=(25/100)*cost;
else if(cost>5000
&& cost<=10000)
d=(35/100)*cost;
else
d=(50/100)*cost;
System.out.print(“Total amt is”+(cost+d));
}
}
- Given three numbers A,B,C, WAP to write
their values in descending order.
If A=7,B=4, C=10, print 10, 7, 4
import java.util.*;
class Desc
{
public static void main( )
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.print(“Enter number”);
a=sc.nextInt( );
System.out.print(“Enter number”);
b=sc.nextInt(
);
System.out.print(“enter number”);
c=sc.nextInt( );
if(a>b && a>c)
{
System.out.println(a);
if(b>c)
{
System.out.println(b);
System.out.println(c);
}
else
{
System.out.println(c);
System.out.println(b);
}
}
else if(b>a && b>c)
{
System.out.println(b);
if(a>c)
{
System.out.println(a);
System.out.println(c);
}
else
{
System.out.println(c);
System.out.println(a);
}
}
else if(c>a && c>b)
{
System.out.println(c);
if(a>b)
{
System.out.println(a);
System.out.println(b);
}
else
{
System.out.println(b);
System.out.println(a);
}
}
}
}
It helped a lot coz our computer teacher gives us a wrong program with errors his name is pR
ReplyDeletethanks
Deletethis was really helpful!!
ReplyDelete