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

Practice Questions

The document contains Java programs for various number-based tasks, including checking ISBN numbers, printing alternate prime numbers, calculating perfect squares, summing natural numbers, and finding the nth prime number. Each task is accompanied by a brief explanation of the concept and the corresponding Java code. The document serves as a practical guide for implementing these mathematical concepts in Java programming.

Uploaded by

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

Practice Questions

The document contains Java programs for various number-based tasks, including checking ISBN numbers, printing alternate prime numbers, calculating perfect squares, summing natural numbers, and finding the nth prime number. Each task is accompanied by a brief explanation of the concept and the corresponding Java code. The document serves as a practical guide for implementing these mathematical concepts in Java programming.

Uploaded by

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

NUMBER BASED TASH -01

Question no.- 1 ISBN Number Program


ISBN is another special number in Java. ISBN stands for the International
Standard Book Number
that is carried by almost each every book. The ISBN is a ten-digit unique
number. With the help of
the ISBN, we can easily find any book. The ISBN number is a legal number
when 1*Digit1 +
2*Digit2 + 3*Digit3 + 4*Digit4 + 5*Digit5 + 6*Digit6 + 7*Digit7 + 8*Digit8 +
9*Digit9 + 10*Digit10
is divisible by 11. The digits are taken from right to left. So, if the ten-digit
number is 7426985414,
Digit1 and Digit10 will be 4 and 7, respectively.

Syntax:-
import java.util.*;
import java.io.*;
import java.util.Scanner;
class ISBNNumberExample {
static boolean checkISBNNumber(long number) {
int sum = 0;
int i, t, intNumber, dNumber;
String strNumber;
strNumber = ""+number;
if (strNumber.length() != 10) {
return false;
}
for (i = 0; i < strNumber.length(); i++) {
intNumber = Integer.parseInt(strNumber.substring(i, i+1));
dNumber = i + 1;
t = dNumber * intNumber;
sum = sum + t;
}
// check whether the sum is divisible by 11 or not
if ((sum % 11) == 0) {
return true;
}
return false;
} // main() method start
public static void main(String args[]) {
long n1, n2;
try {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter first 10 digit ISBN number");
n1 = Long.parseLong(br.readLine()); System.out.println("Enter second
10 digit ISBN number");
//store user entered value into variable n2
n2 = Long.parseLong(br.readLine());
if (checkISBNNumber(n1))
System.out.println(n1 + " is a valid ISBN number");
else
System.out.println(n1 + " is not a valid ISBN number");
if (checkISBNNumber(n2))
System.out.println(n2 + " is a a valid ISBN number");
else
System.out.println(n2 + " is not a valid ISBN number");

}catch(java.lang.Exception e) {
System.out.println("Error while reading the data.");
}
}
}
Output:-

Question no.2
Prime Number: A prime number is a number p such that whenever p
divides ab, then either p divides a or p divides b. In other words, a number
that is divisible by itself only is called a prime number. For example, 2, 3, 5,
7, etc. are prime numbers.

prints the alternate prime numbers up to n


Enter Number : 40
Syntax:-
import java.util.Scanner;
public class AlternatePrimeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Number : ");
int n = scanner.nextInt();
System.out.print("Alternate prime numbers up to " + n + " are: ");
boolean printFlag = true; // Flag to alternate printing
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
if (printFlag) {
System.out.print(i + " ");
printFlag = false;
}
else {
printFlag = true;
}
}
}
scanner.close();
}
private static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
Output:-

Question no. -3
The perfect square or square number is a positive integer that is square
of an integer. In other words, when we multiply two same numbers
together,
then the product that we get is called the perfect square. In short, it is the
product of two positive equal integers or product of an integer with itself.
Syntax:-
import java.util.Scanner;
class Perfectsq{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
int sq ;
System.out.println("enter any number");
int num= sc.nextInt();
if(num > 0){
sq= num*num;
System.out.println("the perfect square:"+sq);
}
else{
System.out.println("the no. is not perfect square");
}
}
}
Output:-

Question no.-4
Q4. The natural numbers are the numbers that include all the positive
integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ......, n. When we
add these numbers together, we get the sum of natural numbers.
Syntax:-
import java.util.Scanner;
class Naturalnum{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter Number :");
int num= sc.nextInt();
int sum=0;
if(num > 0){
for(int i=1;i<=num;i++){
sum=sum+i;
}
System.out.println("Sum of First "+num+" Natural Numbers is
="+sum);
}

}
}
Syntax:-

Question no.-5
nth Prime Number Java
A number is prime if it is divisible by 1 and itself. In other words, a prime
number is a natural number with exactly two distinct natural number
divisors 1
and itself. For example, 2, 3, 5, 7, 11, etc. are the prime numbers. Note that
0
and 1 are not prime numbers. The number 2 is the only even prime number
because all the other even numbers are divisible by 2.
Syntax:-
import java.util.Scanner;
public class NthPrimeNum{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n to compute the nth primenum
ber: ");
int n = sc.nextInt();
int num=1, count=0, i;
while (count < n){
num=num+1;
for (i = 2; i <= num; i++){
if (num % i == 0){
break;
}
}
if (i == num)
{
count = count+1;
}
}
System.out.println("The " +n +"th prime number is: " + num);
}
}
Output:-

You might also like