Practice Questions
Practice Questions
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.
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:-