We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Program to compare two numbers.
Program to check whether an integer is an even number or
19 odd number. io
| 11 | Program to calculate commission based on sales,
ce Program to read a 4-digit year and test whether it is a leap
12
year or not.
13 __ | Program that reads a string and tests whether it begins with a
vowel.
Program based on if
9. Program to compare two numbers and display which of them is greater or whether they
are equal.
class Compare
{static void main( )
if{num| > num2)
{[Link] printin(num! + " is greater than " + num2);}
if{num! =10000)
{comm=sales*0.2;
[Link]("Commission is "+comm);}
else if(sales<10000 && sales>=5000)
{comm=sales*0.1;
[Link]("Commission is "+comm);}
else
[Link]("No Commission"); }
static void main( )
£ sales(10000);
sales(5000);
sales(4000); }}
Variable Description:
i
| Name of the Variable Data Type Purpose/Description
sales double _[ Parameter input.
| comm double _ [To store commission.‘Output:
Commission is 2000.0
Commission is 500.0
No Commission
Program based on nested if
12. Program to read a 4-digit year and test whether it is a leap year or not.
import [Link].*;
class Leapyear{
static void main( )
{ Scanner sc=new Scanner([Link]);
int year;
[Link]("Enter a 4-digit ye
year=[Link]();
if ( year % 100 =
if (year % 40 )
[Link] printin("Year " + year +" is a leap year.");
else
[Link]("Year " + year +" is not a leap year."); }
else
{if (year % 4==0)
[Link](" Year " + year +" is a leap year.");
else [Link]("Year "+ year +" is not a leap year."); } }}
OE
Variable Description:
ea) sR
Name of the Variable | Data Type Purpose/Description
| year int | To store a 4-digit year.
Output:
Enter a 4-digit year:
2006
Year 2006 is not a leap year.
Output2:
Enter a 4-digit year:
1900
Year 1900 is not a leap year.
Output2:
Enter a 4-digit year:
2000
Year 2000 is a leap year,10
Program on switch case ee
13. Program that reads a string and tests whether it begins with a vowel.
import [Link].";
class Text
{ static void main( ){
String line;
Scanner se = new Scanner([Link]) ;
[Link] ("Enter a string: ");
line = [Link] ( );
char ch = [Link](0);
switeh(ch)
{ case 'A':
case 'E':
case 'T':
case 'O'
case 'U'
case ‘a’:
+: [Link](" String begins with an uppercase vowel."); break;
case 'e’:
case '':
case 'o':
case 'u'; [Link] printin(" String begins with a lowercase vowel."); break;
default: [Link](" String begins with a consonant. "); }}}
Variable Description:
Name of the Variable | Data Type | Purpose/Description
ch char To store a character.
| line | String | To store a string.
Output! :
Enter a string: Umesh
String begins with an uppercase vowel.
Output2:
Enter a string: umesh
String begins with a lowercase vowel.
Output3:
Enter a string: Bengaluru
String begins with a consonant.=
T
lors ie likeO 3 6.
___| Program to print fist § terms of Fibonacci se
Program to accept
8 spy number or not.
Ss.
umber, check and display whether itis | oT
_| Program to print the pattem,
Program to generate 10 mul
Itiples of numbers from I to the
limit input by the user.
20
| Menu driven program,
Program based on looping statement.
14. Program using for loop to print numbers from 1 to 5,
class Test
{ void printNos( )
{ int i=0:
for(i=1; i<=5; +4i)
[Link](i); 7B]
Variable Description:
Name of the Variable | Data Type
Purpose/Description |
i int | for loop variable |
utput:
ween on
Program based on printing simple series.
15. Program to print numbers between 0 and 20 with a
class Numbers
{ static void main( )
{for(int i=0; i<20; i+=3)
[Link](i + "\t"); }}
Variable Description:
gapof3ieliked 3 6,,
Name of the Variable
i
Data Type Purpose/Description
int for loop variableeg 69. AIS IBS cy
16, Program to print first 5 terms of Fibonac
class Number
{ static void Fibonacci( )
{int f=0, s=1, t,
[Link](f + “\t"+ s);
while(n<=5)
{ t=fts;
[Link](“\t"+t);
fs;
st;
n++3}}}
Variable Description:
Name of the Variable Data Type Purpose/Description
f To store first number
s int To store second number
t To store sum of 2 preceding numbers
a while loop variable
Output:
Osea lpesgals He 2eiee
17. Program to accept a number, check and display whether it is a spy number or not. (A
number is spy if the sum of its digits equals the product of its digits. Example: consider
the number 1124,
Sum of the digits= 1+ 1+2+4=8
Product of the digits = 1 x 1 x2x4=8
class SpyNumber
{ void print (int n)
{int d, s=0, p=1;
while (n>0)
{ d=n%10; s+=d; p*=d;
n=n/10;}
[Link] printin( "It is a spy number.");
else
[Link] printin( "It is not a spy number.");} }‘variable Description:
13
ime of the Variable Data Type Purpose/Description
n Parameter input
d int To store a digit
P To store product of digits
s To store sum of digits
Output! : (n =1124)
It
is a spy number.
Program based on nested for loop
8, Program to print the pattem.
class Nested
{static void main( )
{ inti, js
for (i=1; i <= 5; i++)
{ for (JI j <= j+4)
{[Link]( i + "\¢");}
[Link]( );}}}
Output2; (n =124)
Itis not a spy number.
Variable Description:
‘Name of the Variable [ Data Type Purpose/Description
ij int__ [for loop variables
Output
1
oer)
FaeeeS
Aged iyald eA,
5 eet 5 on Sees
19. Program to generate 10 multiples of numbers from | to the limit input by the user.
import [Link].*;
class Mult_Table{
static void main( ){
Scanner s=new Scanner([Link]);
int num, i,j;
[Link]("Enter the limit:");
.nextint( );
3 i<=num; i++)
{ for (j=1; j <=10; j++)4
{[Link]( i*j + "\")s)
[Link]( ):}}}
Name of the Variable Data Type Purpose/Description
num int Tostore the limit, |
ij int for loop variables
Output:
Enter the
V2 8 Aa eS 6 Tee Roe 10
2 ae 6B AO 12. 5 14 16 182900
3 12-215. 18: Qh 24» 4275830
Menu driven program.
20. Program to display menu for areas of different shapes and then calculate area of selected
shape.
import [Link]. Scanner;
class Menu
{ static void main( )
{ int a, b, area=0, choice;
Scanner s = new Scanner([Link]);
[Link]("Shapes' Area Menu");
[Link]("1. Square"); [Link]("2. Rectangle");
[Link]("3. Exit"); [Link]("Enter your choice 1 to
choice=[Link]( );
switch(choice)
{ case I: [Link]("Enter side:");
a=[Link]( ); area =a * a; break;
case 2: [Link]("Enter length and breadth:");
a=[Link](); b=[Link]( ); area =a * b; break;
case 3: [Link](0);
default: [Link] printIn("Valid choices are I to 3");}
[Link] printin("Area=" + area);} }variable Description:
1s
Name of the Variable Data Type Purpose/Description
a int | To store the value of side and length
b int To store the breadth of rectangle
area int___|To store the area of shape
choice int [To store the choice
Output
Shapes’ Area Menu
1. Square
2. Rectangle
3. Exit
Enter your choice 1 to 3:
1
Enter side:
10
Area=100
Output2:
Shapes’ Area Menu
1. Square
2. Rectangle
3. Exit
Enter your choice | to 3:
2
Enter length and breadth:
10
20
Area=200
Outputs:
Shapes’ Area Menu
1. Square
2. Rectangle
3. Exit
Enter your choice I to 3:
4
Valid choices are | to 3