Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views112 pages

Computer Ia Grade 9

The document outlines an assignment for Grade 9 Computer Applications at Good Shepherd International School, detailing various programming tasks and their corresponding Java code implementations. It includes topics such as sequence programs, math functions, switch-case statements, and loops, with a total of 50 programs to be completed. Each section provides sample input/output and variable descriptions for clarity.

Uploaded by

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

Computer Ia Grade 9

The document outlines an assignment for Grade 9 Computer Applications at Good Shepherd International School, detailing various programming tasks and their corresponding Java code implementations. It includes topics such as sequence programs, math functions, switch-case statements, and loops, with a total of 50 programs to be completed. Each section provides sample input/output and variable descriptions for clarity.

Uploaded by

7835 Dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

GOOD SHEPHERD INTERNATIONAL SCHOOL

M.Palada, Udhagamandalam,The Nilgiris,Tamil Nadu-643004

ICSE
COMPUTER APPLICATIONS
YEAR: 2024-2025
Assignment Programs - Grade 9

NAME : Dhanush.N

CLASS & Section: ICSE 9B

DATE OF SUBMISSION: 15 February, 2025

INDEX

S.No. Topic No. of Page


programs number

1 Sequence Programs 5 3-10

2 Math Functions and Ternary Operator 5 11-20

3 switch-case 5 21-38
4 If - else 10 39-61

5 Loops 20 62-96

6 Nested Loops 5 97-111

TOTAL QUESTIONS 50 3-111

1)​ Sequence programs

1)​ To input 2 numbers from the user and print the sum, difference, product, quotient and
remainder.
import java.util.*;
public class A1
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int a, b, SUM = 0, DIF = 0, quotient= 0, remainder= 0;
System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
SUM = a + b; // calculating the total
DIF = a - b; // Calculating the difference
quotient = a/b; // Calculating the quotient
remainder = a%b; // Calculating the remainder
System.out.println("SUM =" + SUM);
System.out.println("DIFFERENCE=" + DIF);
System.out.println("QUOTIENT=" +quotient);
System.out.println("Remainder=" +remainder);
}
}

Sample Input/Output

Variable description
Name of the variable Data type Purpose

a int To store the first number

b int To store the second


number
SUM int To store the total

DIF int To store the difference

quotient int To store the quotient

remainder int To store the remainder

2)​ To input the length and breadth of a rectangle, print its perimeter and area.
import java.util. *;
public class A2
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int l, b, area=0;
//Asking the user to enter the length and breadth
System.out.println("Enter the length");
l = sc.nextInt();
System.out.println("Enter the breadth");
b = sc.nextInt();
area = l * b; // Calculating the area
System.out.println("Area of rectangle=" + area);
}
}

Sample Input/Output
Variable description
Name of variable Data type Purpose

l int To store the length

b int To store the breadth

area int To store the Area


3)​ To input the radius of a circle from the user and print its circumference and area.

import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int r, circumference = 0, area = 0;
System.out.println("Enter the radius of a circle");
r= sc.nextInt();
circumference = 2*22/7*r; // Calculating the circumference
area= 22/7*r*r; // Calculating the area
System.out.println("Circumference = "+ circumference);
System.out.println("Area =" + area);
}
}
Sample Input/Output
Variable description
Name of the variable Data Type Purpose

r int To store the radius

circumference int To store the circumference

area int To store the area

4)​ To input 4 numbers and find the sum of first 2 and difference of the next two and
sum of them both.

import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int a, b, c, d, sum1 = 0, DIF = 0, sum2 = 0;
System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
System.out.println("Enter the third number");
c = sc.nextInt();
System.out.println("Enter the fourth number");
d = sc.nextInt();
sum1 = a + b; // Calculating the total
DIF = c - d; // Calculating the difference
sum2 = (a + b) + (c - d);
System.out.println("SUM ="+sum1);
System.out.println("DIFFERENCE = "+DIF);
System.out.println("SUM2="+sum2);
}
}

Sample Input/Output
Variable Description
Name of the variable Data type Purpose

a int To store the first number

b int To store the second


number

c int To store the third number

d int To store the fourth number

sum1 int To store the sum

DIF int To store the difference

sum2 int To store the second sum

5) To input three numbers from the user and find out the sum and arithmetical
mean.
import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int a, b, c, SUM = 0;
double MEAN = 0.0;
System.out.println("Enter the first number");
a= sc.nextInt();
System.out.println("Enter the second number");
b= sc.nextInt();
System.out.println("Enter the third number");
c= sc.nextInt();
SUM= a + b + c; // Calculating the total
MEAN= (a + b + c)/3; // Calculating the average
System.out.println("Total ="+ SUM);
System.out.println("Mean ="+ MEAN);
}
}

Sample Input/Output
Variable Description
Name of the variable Data type Purpose

a int To store the first number

b int To store the second


number

c int To store the third number

SUM int To store the sum

MEAN double To store the average

Math Functions and Ternary Operators


1)​ To input 3 numbers and display the greatest and smallest of the 3 numbers
(using ternary operator)

import java.util.*;
public class G3
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int a, b, c, g=0, s=0;
System.out.print("Enter First Number: ");
a = in.nextInt();
System.out.print("Enter Second Number: ");
b = in.nextInt();
System.out.print("Enter Third Number: ");
c = in.nextInt();
g = Math.max(a, b); // Calculating the maximum value
g = Math.max(g, c);
s = Math.min(a, b); // Calculating the minimum value
s = Math.min(s, c);
System.out.println("Greatest Number = " + g);
System.out.println("Smallest Number = " + s);
}
}

Sample Input/Output
Variable Description
Name of the Data type Purpose
variable

a int To store the first number

b int To store the second number

c int To store the third number

g int To store the greatest number

s int To store the smallest number


2) Calculate and display the hypotenuse of a right-angled triangle by taking the
perpendicular and base as input.

import java.util.*;
public class P1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double p, b, h = 0.0;
//Asking the user to enter the perpendicular and base
System.out.print("Enter Perpendicular: ");
p= in.nextDouble();
System.out.print("Enter Base: ");
b= in.nextDouble();
h= Math.sqrt(Math.pow(p, 2) + Math.pow(b, 2)); // Calculating the
hypotenuse
System.out.println("Hypotenuse = " + h);
}
}
Sample Input/Output

Variable Description
Name of the Data type Purpose
variable

p double To store the perpendicular

b double To store the base

h double To store the hypotenuse


3) To input 2 numbers and find the square and square root of 1 of them and the
cube and cube root of another and display them.

import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a, b;
double s = 0.0, c = 0.0;
System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
s = Math.sqrt(a); // Calculating the square root
c = Math.cbrt(b); // Calculating the cube root
System.out.println("Square :"+ (a*a));
System.out.println("Square root = " + s);
System.out.println("Cube:"+ (b*b*b));
System.out.println("Cube root = " +c);
}
}

Sample Input/Output
Variable Description
Name of the variable Data type Purpose

a int To store the first number

b int To store the second number

s double To store the square root

c double To store the cube root


4) To input a number, evaluate and display the results based on the number
entered by the user:
a)​ The absolute value of the number
b)​ The square root of the number
c)​ Cube of the number
d)​ Random numbers between 0 and 1.
Ans) import java.util.*;
public class P1
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int i;
double a, ABS=0.0, SQRT=0.0, CUBE=0.0, d = 0.0;
System.out.println("Enter the number");
a= sc.nextDouble();
ABS = Math.abs(a); // Calculating the absolute value
SQRT = Math.sqrt(a); //Calculating the square root
CUBE = Math.pow(a, 3); //Calculating the cube
System.out.println("Absolute = "+ABS);
System.out.println("Square root = "+SQRT);
System.out.println("CUBE = "+CUBE);
System.out.println("Random number between 0 and 1");
for(i=0; i<1; i++)
{
d= Math.random();
System.out.println(d);
}
}
}

Sample Input/ Output


Variable description
Name of the variable Data Type Purpose

i int To store the value of i

a double To store the number

ABS double To store the absolute value

SQRT double To store the square root

CUBE double To store the cube

d double To store the random number


5) Calculate and display the final velocity by taking the initial velocity,
acceleration, and the time covered as inputs.

import java.util.*;
public class ZC1
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
double u, a, t, v = 0.0;
System.out.println("Enter the initial velocity");
u= sc.nextDouble(); // Storing the initial velocity
System.out.println("Enter the acceleration");
a= sc.nextDouble();
System.out.println("Enter the time period");
t= sc.nextDouble();
v = u + (2 * a * t); // Calculating the final velocity
System.out.println("The final velocity = " +v);
}
}

Sample Input/ Output


Variable description
Name of the variable Data type Purpose

u double To store the initial velocity

a double To store the acceleration

t double To store the time period

v double To store the final velocity


Switch case

1)​ Using the switch case, make a menu-driven program for cube, cube root, square,
square root, and round of the number.

import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int m;
double n, cube=0.0, cuberoot=0.0, square=0.0, squareroot=0.0, r=0.0;
System.out.println("Enter the number");
n = sc.nextDouble();// Enter the number
System.out.println("Enter your choice");
System.out.println("Enter 1 for cube");
System.out.println("Enter 2 for cube root");
System.out.println("Enter 3 for square");
System.out.println("Enter 4 for square root");
System.out.println("Enter 5 for random");
m = sc.nextInt();
switch (m)
{
case 1:
cube = n*n*n; // Calculating the cube
System.out.println("Cube = " +cube);
break;
case 2:
cuberoot = Math.cbrt(n);
System.out.println("Cube root ="+cuberoot);
break;
case 3:
square = n* n; // Calculating the square
System.out.println("Square = "+square);
break;
case 4:
squareroot = Math.sqrt(n); // Calculating the square root
System.out.println("Square root = " +squareroot);
break;
case 5:
r = Math.round(n); // Calculating the rounded value
System.out.println("Round = " +r);
break;
default:
System.out.println("Wrong Choice");
}

}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

m int To store the expression and codes

n double To store the number

cube double To store the cube of the number

cuberoot double To store the cube root of the number

square double To store the square of the number

squareroot double To store the square root of the number

r double To store the rounded number


2)​ Using a switch case, make a program to display the day based on the choice.

Ans) import java.util.*;


public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int m; // Calculating the weekends
System.out.println("Enter your choice");
m = sc.nextInt();
switch (m)
{
case 1: System.out.println("Sunday");
break;
case 2: System.out.println("Monday");
break;
case 3: System.out.println("Tuesday");
break;
case 4: System.out.println("Wednesday");
break;
case 5: System.out.println("Thursday");
break;
case 6: System.out.println("Friday");
break;
case 7: System.out.println("Saturday");
break;
default:
System.out.println("Wrong Input");
}
}
}
Sample Input/ Output
Variable description

Name of the variable Data type Purpose

m int To store the values


3)​ Using a switch, make a menu-driven program to convert a given temperature
from Fahrenheit to Celsius. An appropriate message should be given to the user
for an incorrect choice. hint= c=(5/9)(f-32) and f=1.8*c+32

import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int m;
double t;
System.out.println("Enter your choice - celsius or fahrenheit");
System.out.println("Enter 1 for finding celsius value");
System.out.println("Enter 2 for finding Fahrenheit value");
m = sc.nextInt();
switch(m)
{
case 1:
System.out.println("Enter the temperature in fahrenheit");
t = sc.nextDouble();
double c = (5/9) * (t-32); // Calculating the celsius value
System.out.println("Temperature in celsius = " + c);
break;
case 2:
System.out.println("Enter the temperature in celsius");
t = sc.nextDouble();
double f = 1.8 * t + 32; // Calculating the fahrenheit
System.out.println("Temperature in fahrenheit="+ f);
break;
default:
System.out.println("Wrong Input");
}
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

m int To store the value of m


4)​ Using switch case, make a menu-driven program for addition, subtraction,
multiplication, division, and remainder.

import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int m, a, b, SUM, DIF, PRODUCT, DIV, REM;
System.out.println("Enter your choice");
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for remainder");
m = sc.nextInt();
switch(m)
{
case 1: System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
SUM = a + b; //Calculating the total
System.out.println("Total ="+SUM);
break;
case 2: System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
DIF = a - b;// Calculating the difference
System.out.println("Difference = " +DIF);
break;
case 3: System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
PRODUCT = a * b;// Calculating the product
System.out.println("PRODUCT =" +PRODUCT);
break;
case 4: System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
DIV = a/b; // Calculating the quotient
System.out.println("Quotient =" +DIV);
break;
case 5: System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
REM = a%b; //Calculating the remainder
System.out.println("Remainder ="+REM);
break;
default:
System.out.println("Wrong input");

}
}
}
Sample Output/Input
Variable description

Name of the variable Data type Purpose

m int To store the value of m

a int To store the first number

b int To store the second number

SUM int To store the total

DIF int To store the difference

PRODUCT int To store the product

DIV int To store the quotient

REM int To store the remainder


5)​ Using a switch case, make a program for outputting the month based on the
choice.

import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int m;
System.out.println("Enter the number of the month according to your choice");
m = sc.nextInt();// Printing the months
switch(m)
{
case 1: System.out.println("January");//Output
break;
case 2: System.out.println ("February");//Output
break;
case 3: System.out.println("March");//Output
break;
case 4: System.out.println("April");//Output
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("Wrong Input");
}
}
}

Sample Input/ Output


Variable description
Name of the variable Data type Purpose

m int To store the value of m


If - else, if - else if ladder and nested if questions

1)​ A pre-paid taxi charge from the passenger as per the tariff given below:
distance rate

Upto 5 km 100

For the next 10 km 10/km

For the next 10 km 8/km

More than 25 km 5/km


Write a program to find the tariff needed based on the distance entered by the
user

import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int d;
//Input
System.out.println("Enter the distance");
d = sc.nextInt();
//Calculating the tariff
{
if (d<=5);
System.out.println("Tariff ="+100);
}
{
if (d > 5 && d<=15)
System.out.println("Tariff ="+d*10);
}
{
if (d > 15 && d <=25)
System.out.println("Tariff ="+d*8);
}
if (d>25)
System.out.println("Tariff ="+d*5);
}
}
Sample Input/Output

Variable Description
Name of the variable Data type Purpose

d int To store the distance


2)​ A cloth showroom has announced festival discounts and gifts are being awarded
based on the amount.

Total cost discount gift

Upto 2000 5% calculator

2001 to 5000 10% School bag

5001 to 10000 15% Wall clock

10000 above 20% Wristwatch


Create a program to input the cost and find the final price after the discount and
the gift they obtained.
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int p;
double d = 0.0, FP = 0.0;
String gi="";
//Asking the user to enter the amount of purchase
System.out.println("Enter the amount of purchase");
p = sc.nextInt();
{
if (p<=2000)
d = p*5.0/100.0;
gi = "Calculator";
}
{
if (p>2000 && p<=5000)
d = p*10.0/100.0;
gi = "School Bag";
}
{
if (p>5000 && p<=10000)
d = p*15.0/100.0; // Calculating the discount
gi = "Wall clock";
}
{
if (p>10000)
d = p*20.0/100.0;
gi = "Wristwatch";
}
//Calculating the final price
FP = p - d;
System.out.println("Final price =" +FP);
System.out.println("Gift =" +gi);
}
}
Sample Input/Output
Variable description
Name of the variable Data type Purpose

p int To store the amount of


purchase

d double To store the discount

FP double To store the final price

gi String To store the gift


3)​ A company announces revised dearness allowance and special allowance for
their employees as per the tariff given below.

basic Dearness allowance (DA) Special allowance (SA)

Up to 10000 10% 5%

10001 to 20000 12% 8%

20001 to 30000 15% 10%

Above 30000 20% 12%


Write a program to accept the name and basic salary of an employee and find
the gross salary
Gross salary= Basic+DA+SA

Display the information in the given format-

Name basic DA spl allowance gross salary.​


XX XX XX XX XX
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int b;
double DA = 0.0, SA = 0.0, GS = 0.0;
String n;
System.out.println("Enter your name");
n = sc.next();
System.out.println("Enter your basic salary");
b = sc.nextInt();
//Calculating SA and DA
{
if(b<=10000)
DA = b*10.0/100.0;
SA = b*5.0/100.0;
}
{
if(b>10000 && b<=20000)
DA = b*12.0/100.0;
SA = b*8.0/100.0;
}
{
if(b>20000 && b<=30000)
DA = b*15.0/100.0;
SA = b*10.0/100.0;
}
{
if(b>30000)
DA = b*20.0/100.0;
SA = b*12.0/100.0;
}
//Calculating the gross salary
GS = b+DA+SA;
System.out.println("Name"+" "+"basic"+" " +"DA"+" "+"spl allowance"+"
"+"gross salary");
System.out.println (n+" "+b+" "+DA+" "+SA+" "+" "+GS);
}
}
Sample Input/Output

Variable Description

Name of the variable Data type Purpose

b int To store the basic salary

DA double To store the dearness


allowance

SA double To store the special


allowance

GS double To store the gross salary

n String To store the name of the


person
4)​ Write a program to take values of length and breadth of a shape from the user
and check if the shape is a square or not.
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int l, b;
System.out.println("Enter the length");//Input
l = sc.nextInt();
System.out.println("Enter the breadth");//Input
b = sc.nextInt();
//Checking whether the shape is a square or not
if (l==b)
System.out.println("Its a square");
else
System.out.println("Not a square");
}
}

Sample Input/Output
Variable description
Name of the variable Data type Purpose

l int To store the length

b int To store the breadth


5) Write a program display the grade according to the grading system of a school :
Below 25 - F ;
26 to 45 - E ;
46 to 50 - D ;
51 to 60 - C ;
61 to 80 - B ;
Above 80 - A ;
Ask the user to enter marks and print the corresponding grade.

Ans) import java.util.*;


public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int g;
char m;
System.out.println("Enter your marks out of 100");//Input
g = sc.nextInt();
//Grading the students according to their marks
if(g>80);
m = 'A';
if(g<=80 && g>60)
m = 'B';
if(g<=60 && g>50)
m = 'C';
if(g<=50 && g>=46)
m = 'D';
if(g<46 && g>=25)
m ='E';
if(g<25)
m = 'F';
System.out.println("Grades ="+m);
}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

g int To store the marks


obtained by the student

m char To store the grades


6) Write a program to input the Selling Price and Cost Price of a commodity and find
the Profit or Loss made upon selling the product.
Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int sp, cp, m = 0;
System.out.println("Enter the cost price");
cp = sc.nextInt();
System.out.println("Enter the selling price");
sp = sc.nextInt();
//Finding whether its profit or loss
//Finding the profit or loss
{
if(cp>sp)
System.out.println("Loss:"+(cp - sp));
}
{
if(cp<sp)
System.out.println("Profit:"+(sp - cp));
}
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

sp int To store the selling price

cp int To store the cost price

m int To store the profit or loss


7) An electricity company charges their consumers according to the units consumed per
month
according to the given traffic:
Units Consumed ​ ​ ​ ​ ​ Charges
Up to 100 units​ ​ ​ ​ ​ ₹ 2 per unit
More than 100 units and up to 200 units​​ ₹ 1.80 per unit
More than 200 units​​ ​ ​ ​ ₹ 1.50 per unit
In addition to the above, every consumer has to pay ₹ 200 as Service Charge per
month. Write
a program to input the amount of units consumed and calculate the total charges
payable
(Bill) by the consumer.
Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int u, a = 200;
double d = 0.0, tc = 0.0;
System.out.println("Enter the units consumed");
u = sc.nextInt();
//Calculating the charge according to the units used
//Calculating the total charge
{
if(u<=100)
d = (u*2.0);
}
{
if(u>100 && u<=200);
d = (u*1.8);
}
{
if(u>200);
d = (u * 1.5);
}
tc = (d + 200.0);
System.out.println("Total charges ="+tc);
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

u int To store the units

a int To store the value of a

d double To store the charge

tc double To store the total charge


8)A library charges a fine for books returned late. Following are the fines:
First five days: 40 paise per day.
Six to Ten days: 65 paise per day.
Above ten days: 80 paise per day.
Design a program to calculate the fine in rupees assuming that a book is returned N
days late.
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int d;
double c;
System.out.println("Enter the number of days");
d = sc.nextInt();
//Calculating the fine
//Calculating the fine according to the number of the days late
{
if(d<=5)
c = d*40;
if(d>5 && d<=10)
c = d*65;
if(d>10)
c = d*80;
}
System.out.println("The fine:"+c);
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

d int To store the number of


days

c double To store the fine


9) A special two-digit number is such that when the sum of its digits is added to the
product of
its digits, the result is equal to the original two-digit number.
Example:
Consider the number 59.
Sum of digits= 5 + 9 = 14
Product of its digits = 5 x 9 =45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of
its
digits. If the value is equal to the number input, output the message “Special 2-digit
number”
otherwise, output the message “Not a special 2-digit number”.
Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int n, rem = 0, p = 0, r = 0, sum = 0, pro = 0, l = 0;
System.out.println("Enter the two-digit number only");
n = sc.nextInt();
//Finding if the two-digit number is a special number or not
{
if(n>0)
rem = n%10;
p = n/10;
r = p%10;
sum = r+rem;
pro = r*rem;
l = sum+pro;
}
{
if(l==n)
System.out.println("Its a special 2 digit number");//Output
else
System.out.println("It's not a special 2 digit number");//Output
}
}

Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

rem int To store the remainder

p int To store the quotient

r int To store the remainder

sum int To store the total

pro int To store the product


l int To store the sum of
variable sum and pro

10) To foster a sense of water conservation, the water department has an annual water
conservation tax policy. The taxes are based on the water consumption of the
consumer. The
tax rates are as follows:
Water Consumed (in Gallons) ​ ​ ​ ​ Tax Rate in ₹ /100 gallons
Upto 45 ​ ​ ​ ​ ​ ​ ​ ​ No Tax
More than 45 but 75 or less ​ ​ ​ ​ ​ 475.00
More than 75 but 125 or less ​ ​ ​ ​ 750.00
More than 125 but 200 or less ​ ​ ​ ​ ​ 1225.00
More than 200 but 350 or less​ ​ ​ ​ ​ 1650.00
More than 350 ​ ​ ​ ​ ​ ​ ​ 2000.00
Write a program to input the water consumed annually in gallons and output the tax
payable.
Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int g;
System.out.println("Enter the amount of water consumed in gallons");
g = sc.nextInt();
//Finding the tax
//Finding the tax according to the units consumed
{
if(g<=45)
System.out.println("Tax:"+0.0);
if(g>45 && g<=75)
System.out.println("Tax:"+(g*475.0));
if(g>75 && g<=125)
System.out.println("Tax:"+(g*750.0));
if(g>125 && g<=200)
System.out.println("Tax:"+(g*1225.0));
if(g>200 && g<=350)
System.out.println("Tax:"+(g*1650.0));
if(g>350)
System.out.println("Tax:"+(g*2000.0));
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

g int To store the unit in gallons


Loops
1)​ Print 1 to 20.
Ans)import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
//Printing 1 to 20
int k;
{
for(k=1;k<=20;k++)
System.out.print(k+”,”);//Output
}
}
}

Sample Input/Output
Variable description
Name of the variable Data type Purpose

k int To store the numbers

2) Print your name 20 times.


Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int k;
//Printing the name 20 times
{
for(k=1;k<=20;k++)
System.out.print("dhanush,");
}
}
}

Sample Input/Output
Variable description
Name of the variable Data type Purpose

k int To store the names

3) Print 1 to 10 with your name after the number.


Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int k;
//Printing the name after the number 1 to 10
{
for(k=1;k<=10;k++)
System.out.println(k+"-Dhanush");//Output
}
}
}
Sample Input/Output
Variable description
Name of the variable Data type Purpose

k int To store the number and


names

4) Print all the odd numbers from 1 to 50.


Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int k;
//Printing odd numbers from 1 to 50
{
for(k=1;k<=50;k+=2)
System.out.print(k+",");
}
}
}

Sample Input/Output
Variable Description
Name of the variable Data type Purpose

k int To store the odd numbers


5) Print all the even numbers from 1 to 50.
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int k;
//Printing odd numbers from 1 to 50
{
for(k=0;k<=50;k+=2)
System.out.print(k+",");//Output
}
}
}

Sample Input/Output

Variable description
Name of the variable Data type Purpose

k int To store the even


numbers
6) Print 1 to 20 backwards (i.e. 20 to 1).
Ans) import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int k;
//Printing 1 to 20 backwards
{
for(k=20;k>=1;k--)
System.out.print(k+",");//Output
}
}
}

Sample Input/Output

Variable description
Name of the variable Data type Purpose

k int To store the numbers


7) Print the multiplication table of a given number (n), in the format: (if n=5)
(5x1=5
5 x 2 = 10
And so on… ) Uptol 10
Ans)import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, k;
​ System.out.println("Enter the number:");
​ n=sc.nextInt();
//Printing the tables of the number given by the user
for( k=1; k <= 10; k++)
{
System.out.println(n+" x "+k+" = "+n*k);//Output
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

k int To store the multiplicand

n int To store the number


8) Enter a number from the user and print the factors.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, i;
//Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
//Printing the factors of the number entered by the user
{
for(i = 1;i<=n;i++)
{
if(n%i==0)
System.out.print(i+",");//Output
}
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

i int To store the number


9) Enter a number from the user and print its factorial.

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, i, f = 1;
//Asking the user to enter the number
System.out.println("Enter the positive number");
n = sc.nextInt();
//Printing the factors of the number entered by the user
{
if(n<0)
System.out.println("Its negative number therefore factorial cannot be well
defined");
}
if(n>0)
{
for(i = 1;i<=n;i++)
f = f * i;
{
System.out.print("Factorial of"+" "+n+" "+"is"+" "+f);//Output
}
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

i int To store the number

f int To store the factorial


10) Enter a number from the user and check whether it is prime or not.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, i, d = 0;
//Asking the user to enter the number
System.out.println("Enter the positive number");
n = sc.nextInt();
//Checking whether the number is a prime number
{
for(i = 1;i<=n;i++)
{
if(n%i==0)
d++;
}
if(d==2)
System.out.println("Its a prime number");
else
System.out.println("Not a prime number");
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

i int To store the value of i

d int To store the value of d


11) Using a while loop, write a program to input a number and find the sum of its
digits

Ans) import java. util.*;


public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int n, sum = 0, rem = 0;
//Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
//Finding the sum of the digits of the number
while(n>0)
{
rem= n% 10;
sum = sum + rem;
n= n/ 10;
}
System.out.println(sum);
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

rem int To store the remainder

Sum int To store the total


12) Write a program to pass an integer as an argument and check whether it is
an Armstrong number or not. Numbers whose sum of the cube of its digit is equal
to the number itself are called Armstrong numbers. Example 153=1^3+5^3 +3^3.
Ans) import java. util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int n, sum = 0, rem = 0;
//Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = n;
while(n>0)
{
rem= n% 10;
sum = sum + (rem*rem*rem);
n= n/ 10;
}
//Checking whether the number is an armstrong number
if (n1 == sum)
System.out.println("Armstrong number");
else
System.out.println("Not an armstrong number");
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

rem int To store the remainder

sum int To store the total

n1 int To store the armstrong number


13) Write a program to input a number and check whether the number is a perfect
number or not.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, i, sum = 0;
// Asking the user to enter a number
System.out.print("Enter a number: ");
n = sc.nextInt();
// Finding factors and adding them
{
for ( i = 1; i <= n / 2; i++)
{
if (n % i == 0)
{
sum = sum + i;
}
}
}
// Checking if the sum of factors equals the number
if (sum == n)
{
System.out.println(n + " is a perfect number");
}
else
{
System.out.println(n + " is not a perfect number");
}
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

i int To store the values

sum int To store the total

n int To store the number


14) Write a program to accept a number from the user and check whether it is a
Palindrome or not.
Ans) import java.util.*;
public class M23
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int n, d, rev = 0;
//Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = n;
while(n>0)
{
d = n%10;
rev = rev*10+d;
n = n/10;
}
//Checking whether the number is palindrome number
{
if (n1 == rev)
System.out.println("it's a palindrome number");
else
System.out.println("it's not a palindrome number");
}
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

n int To store the number

d int To store the modulus

rev int To store the reverse


15) Write a program to print the Fibonacci series i.e. 0,1,1,2,3,5,8,13….
Fibonacci series is such a series which starts from 0 and 1, and subsequent
numbers are the sum of the previous two numbers. Uptill 10 terms.
Ans) import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = 0, b = 1, c = 0, n = 3;
//Printing the fibonacci series
System.out.println("Fibonacci series");
System.out.print(a+",");
System.out.print(b+",");
do
{
c = a+b;
System.out.print(c+",");
a = b;
b = c;
n = n+1;
}
while(n<=10);
}
}

Sample Input/Output
Variable description
Name of the variable Data type Purpose

a int To store the number

b int To store the number

c int To store the sum

n int To store the value of n


16) Write a program to print sd series till 10 multiples of each.
Ans) import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int m, n, q = 2, d = 0, e = 0;
//Ask the user to enter the number
System.out.println("Enter the first number");
m = sc.nextInt();
System.out.println("Enter the second number");
n = sc.nextInt();
System.out.print(m+",");
System.out.print(n+",");
//Printing the sd series
do
{
d = m*q;
e = n*q;
System.out.print(d+",");
System.out.print(e+",");
q++;
}
while(q<=10);
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

m int To store the first number

n int To store the second


number

q int To store the multiplicand

d int To store the product

e int To store the product


17) Write a program to find the hcf/gcd and lcm of two numbers given by the
user.
import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
//Program to print the hcf and lcm
int a, b, c, d, e, hcf = 0, lcm = 0;
System.out.println("Enter the first number");
a = sc.nextInt();
System.out.println("Enter the second number");
b = sc.nextInt();
c = a;
d = b;
while(d != 0)
{
e = d;
d = c%d;
c = e;
}
hcf = c;
lcm = (a*b)/hcf;
System.out.println("HCF ="+hcf);//Output
System.out.println("LCM = "+lcm);//Output
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

a int To store the first number

b int To store the second number

c int To store value of c

d int To store the value of d

e int To store the value of e

hcf int To store HCF

lcm int To store lcm


18) Using do while, write a program to display the sum of positive even numbers and
negative odd numbers from a list of numbers entered by the user.the list terminates
when the user enters zero from the console.
Ans) import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int n, a = 0, b = 0;
System.out.println("Enter 0 to terminate");
do
//Asking the user to enter the number
{
System.out.println("Enter the number");
//Printing the sum
n = sc.nextInt();
if (n>0 && n%2 == 0)
{
a = a + n;
}
else if (n<0 && n%2 != 0)
{
b = b + n;
}
}
while (n!= 0);
System.out.println("Sum of positive even numbers:" +a);
System.out.println("Sum of negative odd numbers:" +b);
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

a int To store the sum of


positive even number

b int To store the sum of


negative odd number
19) Using do while, write a program to input ten different numbers.display the sum of
those numbers which are divisible by 3 and 5.
import java.util.*;
public class Main
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int n, a = 0, SUM = 0;
System.out.println("Enter 0 to terminate");
do
{
// Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
if (n % 3 == 0 && n % 5 == 0)
{
SUM = SUM + n; // Calculating the sum
}

a++;
}
while (a < 10);

System.out.println("Sum of numbers divisible by 3 and 5: " + SUM);


}
}

Sample Input/Output
Variable description
Name of the variable Data type Purpose

n int To store the number

a int To execute the loop

SUM int To store the total of those


divisible by 5 and 7
20) Using do while, write a program to display the first ten odd numbers starting from a
number entered by the user.
Sample: 10
Output: 11,13,15,...,29
Ans)
import java.util.*;
public class Main
{
public static void main(String Args[])
{
Scanner sc = new Scanner(System.in);
int n, e = 0;
//Asking the user to enter the number
System.out.println("Enter the number");
n = sc.nextInt();
//Printing the odd numbers
do
{
if (n % 2 != 0)
{
System.out.println(n);
e++;
}
n++;
}
while (e < 10);
}
}
Sample Input/Output

Variable description
Name of the variable Data type Purpose

n int To store the number

e int To store the numbers


Nested loops

Q1) Using switch statements, write programs to print a pattern based on the user's
choice and also ask the user for the number of iterations of the lines.
a)​ Pattern 1
1
12
123
1234
12345
b)​ Pattern 2
1
22
333
4444
55555
Ans 1) import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int i, j, m;
System.out.println("Enter your choice");
m = sc.nextInt();
//Printing the patterns
switch(m)
{
case 1:
for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
System.out.print(j); //Output
System.out.println();
}
break;
case 2:

for (i=1; i<=5; i++)


{
for (j=1; j<=i; j++)
System.out.print(i);//Output
System.out.println();
}
break;
default:
System.out.println("Wrong input");
}

}
}

Sample Input/Output
Variable description

Name of the variable Data type Purpose

i int To store the value of i

j int To store the value of j

m int To store the value of m


Q2) Using switch statements, write programs to print a pattern based on the user's
choice and also ask the user for the number of iterations of the lines.
a)​ Pattern 1
*****
****
***
**
*
b)​ Pattern 2
11111
22222
33333
44444
55555
Ans import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int i, j, m;
System.out.println("Enter your choice");
m = sc.nextInt();
//Printing the patterns
switch(m)
{
case 1:
for (i=5; i>=1; i--)
{
for (j=1; j<=i; j++)
System.out.print("*" + ""); //Output
System.out.println();
}
break;
case 2:
for (i=1; i<=5; i++)
{
for (j=1; j<=5; j++)
System.out.print(i + " ");//Output
System.out.println();
}
break;
default:
System.out.println("Wrong input");
}

}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

i int To store the value of i

j int To store the value of j

m int To store the value of m


3) Using switch statements, write programs to print a pattern based on the user's
choice and also ask the user for the number of iterations of the lines.
a)​ Pattern 1
5
54
543
5432
54321
b)​ Pattern 2
1
21
321
4321
54321
Ans) import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int i, j, m;
System.out.println("Enter your choice");
m = sc.nextInt();
//Printing the patterns
switch(m)
{
case 1:
for ( i = 5; i >= 1; i--)
{
for ( j = 5; j >= i; j--)
{
System.out.print(j + " ");//Output
}
System.out.println();
}
break;
case 2:

for ( i = 1; i <= 5; i++)


{
for ( j = i; j >= 1; j--)
System.out.print(j + " ");//Output
System.out.println();
}
break;
default:
System.out.println("Wrong input");
}

}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

i int To store the value of i

j int To store the value of j

m int To store the value of m


4) Using switch statements, write programs to print a pattern based on the user's
choice and also ask the user for the number of iterations of the lines.
a)​ Pattern 1
11111
2222
333
44
5
b)​ Pattern 2
54321
5432
543
54
5
Ans) import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int i, j, m;
System.out.println("Enter your choice");
m = sc.nextInt();
//Printing the patterns
switch(m)
{
case 1:
for ( i = 1; i <= 5; i++)
{
for ( j = 5; j >= i; j--)
{
System.out.print(i + " ");//Output
}
System.out.println();
}
break;
case 2:
for ( i =1; i <= 5; i++)
{
for ( j = 5; j >= i; j--)
{
System.out.print(j + " ");//Output
}
System.out.println();
}
break;
default:
System.out.println("Wrong input");
}

}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

i int To store the value of i

j int To store the value of j

m int To store the value of m

5) Using switch statements, write programs to print a pattern based on the user's
choice and also ask the user for the number of iterations of the lines.
a)​ Pattern 1
5
44
333
2222
11111
b)​ Pattern 2
#
##
###
####
#####
Ans a) import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int i, j, m;
System.out.println("Enter your choice");
m = sc.nextInt();
//Printing the patterns
switch(m)
{
case 1:
for ( i =5; i >= 1; i--)
{
for ( j = 5; j >= i; j--)
{
System.out.print(i+ " "); //Output
}
System.out.println();
}
break;
case 2:
for ( i =5; i >= 1; i--)
{
for ( j = 5; j >= i; j--)
{
System.out.print("#"+ " "); //Output
}
System.out.println();
}
break;
default:
System.out.println("Wrong input");
}

}
}
Sample Input/Output
Variable description

Name of the variable Data type Purpose

i int To store the value of i

j int To store the value of j

m int To store the value of m

You might also like