0% found this document useful (0 votes)
24 views12 pages

Java ISC Class 12 Scanner Programs

Uploaded by

Jai Ratan Mishra
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)
24 views12 pages

Java ISC Class 12 Scanner Programs

Uploaded by

Jai Ratan Mishra
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

Java ISC Class 12 Scanner

2025-09-03
Contents

1 Sum of Two Numbers 3

2 Calculate Simple Interest 4

3 Find Largest of Three Numbers 5

4 Check Even or Odd 7

5 Calculate Area of Circle 8

6 Convert Celsius to Fahrenheit 9

7 Calculate Factorial of a Number 10

8 Check Leap Year 11


Java ISC Class 12 Practical Programs Using Scanner

1 Sum of Two Numbers

Algorithm:

1. Start

2. Input two integers using Scanner

3. Add the two numbers

4. Display the sum

5. End

Java Program:

import [Link];
public class SumTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter first number: ”);
int num1 = [Link]();
[Link](”Enter second number: ”);
int num2 = [Link]();
int sum = num1 + num2;
[Link](”Sum = ” + sum);
}
}

Sample Output:

Enter first number: 10


Enter second number: 20
Sum = 30

Variable Description:

3
Java ISC Class 12 Practical Programs Using Scanner

Variable Description Data Type

sc Scanner object to read input from Scanner


console

num1 First integer input from user int

num2 Second integer input from user int

sum Sum of num1 and num2 int

2 Calculate Simple Interest

Algorithm:

1. Start

2. Input principal, rate, and time using Scanner

3. Calculate simple interest = (principal * rate * time) / 100

4. Display the simple interest

5. End

Java Program:

import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter principal: ”);
double principal = [Link]();
[Link](”Enter rate of interest: ”);
double rate = [Link]();
[Link](”Enter time in years: ”);
double time = [Link]();

4
Java ISC Class 12 Practical Programs Using Scanner

double interest = (principal * rate * time) / 100;


[Link](”Simple Interest = ” + interest);
}
}

Sample Output:

Enter principal: 10000


Enter rate of interest: 5
Enter time in years: 2
Simple Interest = 1000.0

Variable Description:

Variable Description Data Type

sc Scanner object for input Scanner

principal Principal amount double

rate Rate of interest per annum double

time Time in years double

interest Calculated simple interest double

3 Find Largest of Three Numbers

Algorithm:

1. Start

2. Input three integers using Scanner

3. Compare numbers to find the largest

4. Display the largest number

5. End

5
Java ISC Class 12 Practical Programs Using Scanner

Java Program:

import [Link];
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter first number: ”);
int a = [Link]();
[Link](”Enter second number: ”);
int b = [Link]();
[Link](”Enter third number: ”);
int c = [Link]();
int largest = a;
if(b > largest) {
largest = b;
}
if(c > largest) {
largest = c;
}
[Link](”Largest number is ” + largest);
}
}

Sample Output:

Enter first number: 10


Enter second number: 30
Enter third number: 20
Largest number is 30

Variable Description:

6
Java ISC Class 12 Practical Programs Using Scanner

Variable Description Data Type

sc Scanner object for input Scanner

a First integer input int

b Second integer input int

c Third integer input int

largest Holds the largest number among a, b, c int

4 Check Even or Odd

Algorithm:

1. Start

2. Input an integer using Scanner

3. Check if number % 2 equals 0

4. If yes, print ”Even”, else print ”Odd”

5. End

Java Program:

import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter a number: ”);
int num = [Link]();
if(num % 2 == 0) {
[Link](num + ” is Even”);
} else {

7
Java ISC Class 12 Practical Programs Using Scanner

[Link](num + ” is Odd”);
}
}
}

Sample Output:

Enter a number: 15
15 is Odd

Variable Description:

Variable Description Data Type

sc Scanner object for input Scanner

num Integer input to check parity int

5 Calculate Area of Circle

Algorithm:

1. Input radius

2. Calculate area = 3.1416 * radius * radius

3. Display area

Java Program:

import [Link];
public class AreaCircle {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter radius: ”);
double r = [Link]();
double area = 3.1416 * r * r;

8
Java ISC Class 12 Practical Programs Using Scanner

[Link](”Area = ” + area);
}
}

Sample Output:

Enter radius: 7
Area = 153.9384

Variable Description:

r radius of circle double

area calculated area double

6 Convert Celsius to Fahrenheit

Algorithm:

1. Input temperature in Celsius

2. Calculate Fahrenheit = (Celsius * 9/5) + 32

3. Display Fahrenheit

Java Program:

import [Link];
public class CelsiusToFahrenheit {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter Celsius: ”);
double c = [Link]();
double f = (c * 9 / 5) + 32;
[Link](”Fahrenheit = ” + f);
}
}

9
Java ISC Class 12 Practical Programs Using Scanner

Sample Output:

Enter Celsius: 25
Fahrenheit = 77.0

Variable Description:

c temperature in Celsius double

f temperature in Fahrenheit double

7 Calculate Factorial of a Number

Algorithm:

1. Input integer n

2. Initialize fact = 1

3. Multiply fact by every number from 1 to n

4. Display factorial

Java Program:

import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter a number: ”);
int n = [Link]();
int fact = 1;
for(int i=1; i<=n; i++) {
fact *= i;
}
[Link](”Factorial = ” + fact);
}
}

10
Java ISC Class 12 Practical Programs Using Scanner

Sample Output:

Enter a number: 5
Factorial = 120

Variable Description:

n input integer int

fact factorial result int

i loop counter int

8 Check Leap Year

Algorithm:

1. Input year

2. If year divisible by 400, print leap year

3. Else if year divisible by 100, not leap year

4. Else if year divisible by 4, leap year

5. Else not leap year

Java Program:

import [Link];
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](”Enter year: ”);
int year = [Link]();
if(year % 400 == 0) {
[Link](year + ” is a leap year.”);

11
Java ISC Class 12 Practical Programs Using Scanner

} else if(year % 100 == 0) {


[Link](year + ” is not a leap year.”);
} else if(year % 4 == 0) {
[Link](year + ” is a leap year.”);
} else {
[Link](year + ” is not a leap year.”);
}
}
}

Sample Output:

Enter year: 2024


2024 is a leap year.

Variable Description:

year input year to check int

12

Common questions

Powered by AI

The implicit assumptions in the provided Java programs include that users will input the correct data types and valid values that do not lead to runtime errors or unexpected behavior (e.g., non-numeric input for integers). To address potential input validation issues, the programs could incorporate exception handling using try-catch blocks to manage incorrect input types, and implement data range checks to prevent illogical results, such as negative values for a circle's radius. Additional validation methodologies might include using regex for format-specific input and feedback prompts to guide correct input entry .

The Java program checks if a year is a leap year by applying the Gregorian calendar leap year rules: a year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. The program first checks divisibility by 400, then by 100, and finally by 4, printing appropriate messages for each case. The consideration involves handling edge cases such as century years and transient rules governing leap years. This implementation is thorough and follows the historical definition accurately, accounting for nuances in calendar rules dealing with non-leap century years except multiples of 400 .

The Java program converts temperatures from Celsius to Fahrenheit using the formula Fahrenheit = (Celsius * 9/5) + 32. This formula is derived from the conversion relationship between the Celsius and Fahrenheit scales, where a difference of 100 degrees Celsius corresponds to a 180-degree difference in Fahrenheit (hence, the factor 9/5), and the 32-degree offset aligns the freezing point of water on both scales. The program inputs a Celsius value, applies the formula, and outputs the result .

Nested if conditions in the Java program enhance understanding of control flow logic by clearly depicting hierarchical decision processes. This method allows for tiered checks where each condition is contingent on the previous one not being satisfied, forming a logical, easy-to-follow structure. For example, the leap year program uses nested ifs to sequentially apply rules, streamlining logical progression and reducing conflicts. Although this approach improves clarity and organization, excessive nesting can lead to increased complexity and reduce readability, particularly in large-scale or high-complexity programs .

The significance of initializing a factorial calculation with a factor variable set to one lies in maintaining mathematical correctness and ensuring that the factorial computation starts from a multiplicative identity. In iterative algorithms, this initialization acts as a base case which avoids null results when multiplying sequential numbers. Setting it to zero, by contrast, would nullify all subsequent products. This approach ensures accurate computation of factorial values efficiently and prevents logical errors that might arise from other initialization choices .

The algorithm for calculating simple interest involves five steps: 1) Start the calculation process. 2) Input the principal amount, rate of interest, and time period using scanner. 3) Calculate the simple interest using the formula: (principal * rate * time) / 100. 4) Display the calculated simple interest. 5) End the process. The Java program implements this by first importing the Scanner class, then using scanner.nextDouble() to take input from the user for the principal, rate, and time. It calculates the interest with the formula and uses System.out.println to output the result .

The formula for calculating the area of a circle, area = 3.1416 * radius * radius, is implemented in the Java program by inputting the radius and multiplying it by itself and by the numerical approximation of π (3.1416). This straightforward method ensures precise area computations in basic applications. However, for scientific computing, using constants like Math.PI offered by Java is preferable for accuracy, as it provides a more precise value of π. This adjustment can make a significant difference when dealing with large-scale or critical precision calculations .

The Java program uses the modulus operator to determine whether a number is even or odd. The logic evaluates if the remainder of the number divided by two equals zero; if true, the number is even; otherwise, it is odd. This is a fundamental and efficient approach with O(1) complexity, easily handling typical integer inputs. A potential limitation might arise with very large integers or overflow scenarios, although such cases are unlikely in standard applications without specific mention or handling .

Using a Scanner for user input in Java programs is advantageous for its simplicity and direct method of capturing various data types. This approach is ideal for educational contexts and small-scale applications, allowing for interactive user engagement. However, the Scanner class might be limited by its blocking input method and potential exceptions if input is unexpected. For professional or complex applications, more robust input handling using buffered readers or graphical input methods may be required, ensuring resilience against input errors and more streamlined user interfaces .

The method used by the Java program involves comparing the first number with the subsequent numbers to determine the largest value. The program initializes the first number as the largest and then performs two conditional checks: if the second number is greater than the current largest, it updates the largest value, and a similar check is done for the third number. This algorithm is effective and straightforward for its purpose, as it efficiently uses conditional statements to single out the largest number among three, running in constant time O(1).

You might also like